Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the Linux equivalent of Window's GetAsyncKeyState?

I'm using c++, and I need to determine if a shift key is pressed. In windows, I can just use GetAsyncKeyState. How can I do this in Linux? I cannot run as root.

like image 602
ajfbiw.s Avatar asked Nov 20 '16 05:11

ajfbiw.s


1 Answers

Under X Windows, you can load the X Keyboard extension and call XkbGetState() to get the current keyboard state. If the shift key is being pressed, the base_mods field of the XkbStateRec structure set by XkbGetState() will have the ShiftMask bit set. Full documentation here: https://www.x.org/releases/X11R7.7/doc/libX11/XKB/xkblib.html#Determining_Keyboard_State

That is a synchronous call, but you can instead request to be sent XkbEvent events whenever certain aspects of the keyboard state change, by calling XkbSelectEventDetails(). There are several other ways you could receive keyboard events asynchronously, but here, you are interested in when the Shift key is pressed or released, which is reported as a bitmask in a field of modifiers.

Alternatively: https://stackoverflow.com/a/4225290/4474419

like image 53
Davislor Avatar answered Nov 18 '22 01:11

Davislor