Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/forums/c-sharp/)
-   -   Read !!! All !!! Keys that are Pressed (http://www.go4expert.com/forums/read-keys-pressed-t7811/)

Tomahawk78 12Dec2007 15:58

Read !!! All !!! Keys that are Pressed
 
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

Tomahawk78 14Dec2007 18:04

Re: Read !!! All !!! Keys that are Pressed
 
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 20Dec2007 01:24

Re: Read !!! All !!! Keys that are Pressed
 
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.


All times are GMT +5.5. The time now is 12:48.