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

Tomahawk78's Avatar, Join Date: Dec 2007
Newbie Member
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:
  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  :; break; // run fast not only walk
          case Keys.Escape  : 
                   { ProgrammState.Pause(); 
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>

Thx! Tom

Last edited by shabbir; 12Dec2007 at 19:08.. Reason: Code block
Tomahawk78's Avatar, Join Date: Dec 2007
Newbie Member
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?
babarpapa's Avatar
Newbie Member
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.