1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Read !!! All !!! Keys that are Pressed

Discussion in 'C#' started by Tomahawk78, Dec 12, 2007.

  1. Tomahawk78

    Tomahawk78 New Member

    Joined:
    Dec 12, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    I need help with a very specific problem:

    I want to read all keys that are pressed.
    With the code i used i read only max 2 keys (Number/Letters/Return/Space/...) :)
    Additional to those 2 keys i can read no more of theses keys. The only keys i am able to read additional are (CTRL(left/right) / ALT(left/right)/SHIFT(left/right)) ... :(

    So how am i able to read a key-combination like (wheather it makes sence or not):
    'W'+'A' (moving northwest) +'R' (run quickly) + 'J' (jump)

    This is the code i used:
    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    Code:
      kbState = Keyboard.GetState();
      Keys[] pressedKeys = (Keys[])kbState.GetPressedKeys();
      if (pressedKeys != null)
      {
        foreach (Keys k in pressedKeys)
        {
           switch (k)
           { 
              case Keys.W : classObjectX.moveUp(); break;
              case Keys.A  : classObjectX.moveLeft(); break;
              case Keys.S  : classObjectX.moveDown(); break;
              case Keys.D  : classObjectX.moveRight(); break;
              case Keys.J   : classObjectX.jump(); break;
              case Keys.R  : classObjectX.run(); break; // run fast not only walk
              case Keys.Escape  : 
                       { ProgrammState.Pause(); 
                         ...
                       }break;        
              ...
           }
    
    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    Thx! Tom
     
    Last edited by a moderator: Dec 12, 2007
  2. Tomahawk78

    Tomahawk78 New Member

    Joined:
    Dec 12, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Ok 4 all who have same problems or are interested in why on many keyboards only 2 keystates can be read:

    It is a feature in the hardware. It depends on the keyboards hardware how many pressed keys are recognized. Older keyboards support many keys pressed. In the latest development however (especially on the Laptopsector) only two keypressed are recognized.

    There are still keyboards that recognize more ... but do you want to let your customers let buy new keyboards? I don't

    Can anybody confirm this? Or found a way to enable more keys?
     
  3. babarpapa

    babarpapa New Member

    Joined:
    Dec 19, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    To make sure that you cater for all hardware just work around it. Keep a history of what buttons are currently being pressed E.g. if you receive
    KeyPressDown J KeyPressDown K with no keypress up in the middle you know the user is pressing both these buttons.
     

Share This Page