How to simultaneous read keys on keyboard? [closed]

十年热恋 提交于 2019-12-08 10:49:22

问题


My question behave to my children age, when I got a new game to play, and was very impressed.

The game was able to see and react to more than 4,6 sometimes 8 keys pressed at same time. How is this possible? There is a limit for this? Or if I press "a,s,d,f,u,v,shift,0,uparrow,rightcltr,return and backspace" all thogheter, a program can "read it"? Some introduction about it (in C, bashscript, javascript, or phyton) will be highly appreciated. Thanks for any effort here.


回答1:


You can certainly react to multiple simultaneous keypresses by tracking events that occur when keys are pressed down and separate events that occur when keys are released. For example, in X11 these events are KeyPress and KeyRelease, and in web browsers they are KeyDown and KeyUp Javascript events. But I think you will find that most keyboards have a physical limitation on how many keypresses can be electrically detected at the same time, so your example "a,s,d,f,u,v,shift,0,uparrow,rightcltr,return and backspace" might be too many keys.




回答2:


If we are speaking about Windows, GetKeyboardState() copies the status of the 256 virtual keys to the specified buffer.

BYTE keys[256];

if(GetKeyboardState(keys))
{

   //check if A key is pressed
   if((keys[VK_A]&0xF0) && !(prevKeys[VK_A]&0xF0)) 
   {
     DoAPressed();
   }
   //check if S key is pressed too
   if((keys[VK_S]&0xF0) && !(prevKeys[VK_S]&0xF0)) 
   {
     DoSPressed();
   }
   // the same goes for all keys you want to check
}


来源:https://stackoverflow.com/questions/9864383/how-to-simultaneous-read-keys-on-keyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!