Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Keys and Disabling

Keys and Disabling

Scheduled Pinned Locked Moved C#
tutorialgame-devquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SRJ92
    wrote on last edited by
    #1

    hey, i want to know how to disable certain keys from being pressed if another one has been pressed before, i am making a game which moves the player accress the screen, and there are certain restrictions to move the player, hence my question... i have programmed my keys below :

        private void Form1\_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Up)
            {
                //tim\_Player.Enabled = true;
                Player.Direction = 1;
            }
            if (e.KeyCode == Keys.Right)
            {
                //tim\_Player.Enabled = true;
                Player.Direction = 3;
    
            }
            if (e.KeyCode == Keys.Down)
            {
                //tim\_Player.Enabled = true;
                Player.Direction = 2;
            }
            if (e.KeyCode == Keys.Left)
            {
                //tim\_Player.Enabled = true;
                Player.Direction = 4;
                
            }
        }
    

    I want to disable the "Keys.Up" if the "Keys.Down" is pressed but if another key is pressed, for example, the "Keys.Left" then the "Keys.Up" is re-enabled ..... Thanks in advance :P

    OriginalGriffO L I S 4 Replies Last reply
    0
    • S SRJ92

      hey, i want to know how to disable certain keys from being pressed if another one has been pressed before, i am making a game which moves the player accress the screen, and there are certain restrictions to move the player, hence my question... i have programmed my keys below :

          private void Form1\_KeyDown(object sender, KeyEventArgs e)
          {
              if (e.KeyCode == Keys.Up)
              {
                  //tim\_Player.Enabled = true;
                  Player.Direction = 1;
              }
              if (e.KeyCode == Keys.Right)
              {
                  //tim\_Player.Enabled = true;
                  Player.Direction = 3;
      
              }
              if (e.KeyCode == Keys.Down)
              {
                  //tim\_Player.Enabled = true;
                  Player.Direction = 2;
              }
              if (e.KeyCode == Keys.Left)
              {
                  //tim\_Player.Enabled = true;
                  Player.Direction = 4;
                  
              }
          }
      

      I want to disable the "Keys.Up" if the "Keys.Down" is pressed but if another key is pressed, for example, the "Keys.Left" then the "Keys.Up" is re-enabled ..... Thanks in advance :P

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      To disable a key, set e.Handled = true before you return. See here[^]

      Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • S SRJ92

        hey, i want to know how to disable certain keys from being pressed if another one has been pressed before, i am making a game which moves the player accress the screen, and there are certain restrictions to move the player, hence my question... i have programmed my keys below :

            private void Form1\_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Up)
                {
                    //tim\_Player.Enabled = true;
                    Player.Direction = 1;
                }
                if (e.KeyCode == Keys.Right)
                {
                    //tim\_Player.Enabled = true;
                    Player.Direction = 3;
        
                }
                if (e.KeyCode == Keys.Down)
                {
                    //tim\_Player.Enabled = true;
                    Player.Direction = 2;
                }
                if (e.KeyCode == Keys.Left)
                {
                    //tim\_Player.Enabled = true;
                    Player.Direction = 4;
                    
                }
            }
        

        I want to disable the "Keys.Up" if the "Keys.Down" is pressed but if another key is pressed, for example, the "Keys.Left" then the "Keys.Up" is re-enabled ..... Thanks in advance :P

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        same code as you have - perfect, on whatever key you want to hit, have a boolean, check if you want it to be enabled/disabled based on that boolean, put return False where you want the key to do nothing otherwise leave out the return false

        MVC

        1 Reply Last reply
        0
        • S SRJ92

          hey, i want to know how to disable certain keys from being pressed if another one has been pressed before, i am making a game which moves the player accress the screen, and there are certain restrictions to move the player, hence my question... i have programmed my keys below :

              private void Form1\_KeyDown(object sender, KeyEventArgs e)
              {
                  if (e.KeyCode == Keys.Up)
                  {
                      //tim\_Player.Enabled = true;
                      Player.Direction = 1;
                  }
                  if (e.KeyCode == Keys.Right)
                  {
                      //tim\_Player.Enabled = true;
                      Player.Direction = 3;
          
                  }
                  if (e.KeyCode == Keys.Down)
                  {
                      //tim\_Player.Enabled = true;
                      Player.Direction = 2;
                  }
                  if (e.KeyCode == Keys.Left)
                  {
                      //tim\_Player.Enabled = true;
                      Player.Direction = 4;
                      
                  }
              }
          

          I want to disable the "Keys.Up" if the "Keys.Down" is pressed but if another key is pressed, for example, the "Keys.Left" then the "Keys.Up" is re-enabled ..... Thanks in advance :P

          I Offline
          I Offline
          Ian Shlasko
          wrote on last edited by
          #4

          Don't mean to step on the other guys who answered, but it looks like you're trying to have Up and Down cancel each other out (And Left+Right)... This'll take a little more than what you have now, but not very much. The easiest way would probably be to just keep track of which keys are currently pressed. Now, you can use API calls to query them directly, but if you want to stick to simple events, here's one way to do it: 1) Create a dictionary mapping keys to a boolean 2) Whenever a key you're watching is pressed OR released (Handle the KeyUp event too!), set its value in that dictionary to true (Pressed) or false (Not pressed)... If you want to make this more readable and intuitive, you can even use an enum instead of a boolean. 3) Whenever you make a change to the dictionary, run a separate function to test the current key states... Then you can check for things like if (IsKeyPressed[Keys.Left] && !IsKeyPressed[Keys.Right])... There are plenty of other ways to do this, but this is one of the simplest... If you want to be more efficient, you can use a single flags-type enum value instead of a dictionary, and use binary operations to set and unset the various components:

          [Flags]
          enum ArrowKeyStates {
          None = 0,

          Left = 1,
          Up = 2,
          Right = 4,
          Down = 8,

          // Then a few more to make testing for diagonals easy...
          UpLeft = 3,
          UpRight = 6,
          DownLeft = 9,
          DownRight = 12
          }

          // To set a key as pressed, use a binary OR:
          MyKeyState |= ArrowKeyStates.Left;

          // To set a key as released, use a binary XOR:
          MyKeyState ^= ArrowKeyStates.Left;

          Proud to have finally moved to the A-Ark. Which one are you in?
          Author of the Guardians Saga (Sci-Fi/Fantasy novels)

          1 Reply Last reply
          0
          • S SRJ92

            hey, i want to know how to disable certain keys from being pressed if another one has been pressed before, i am making a game which moves the player accress the screen, and there are certain restrictions to move the player, hence my question... i have programmed my keys below :

                private void Form1\_KeyDown(object sender, KeyEventArgs e)
                {
                    if (e.KeyCode == Keys.Up)
                    {
                        //tim\_Player.Enabled = true;
                        Player.Direction = 1;
                    }
                    if (e.KeyCode == Keys.Right)
                    {
                        //tim\_Player.Enabled = true;
                        Player.Direction = 3;
            
                    }
                    if (e.KeyCode == Keys.Down)
                    {
                        //tim\_Player.Enabled = true;
                        Player.Direction = 2;
                    }
                    if (e.KeyCode == Keys.Left)
                    {
                        //tim\_Player.Enabled = true;
                        Player.Direction = 4;
                        
                    }
                }
            

            I want to disable the "Keys.Up" if the "Keys.Down" is pressed but if another key is pressed, for example, the "Keys.Left" then the "Keys.Up" is re-enabled ..... Thanks in advance :P

            S Offline
            S Offline
            SRJ92
            wrote on last edited by
            #5

            thanks everyone, helped me a lot, :D

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups