Reputation: 401
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.
Upvotes: 7
Views: 7903
Reputation: 15134
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
Upvotes: 1