I think it should be simple question but I google long time and find all solution is in windows, so I give my solution in linux c++ hope next time I forget this question, I can find the answer
Asked
Active
Viewed 1,370 times
1 Answers
0
#include <X11/Xlib.h>
#include <iostream>
#include "X11/keysym.h"
/**
*
* @param ks like XK_Shift_L, see /usr/include/X11/keysymdef.h
* @return
*/
bool key_is_pressed(KeySym ks) {
Display *dpy = XOpenDisplay(":0");
char keys_return[32];
XQueryKeymap(dpy, keys_return);
KeyCode kc2 = XKeysymToKeycode(dpy, ks);
bool isPressed = !!(keys_return[kc2 >> 3] & (1 << (kc2 & 7)));
XCloseDisplay(dpy);
return isPressed;
}
bool ctrl_is_pressed() {
return key_is_pressed(XK_Control_L) || key_is_pressed(XK_Control_R);
}
int main(int argc, char **argv) {
std::cout << ctrl_is_pressed() << std::endl;
return (0);
};

chikadance
- 3,591
- 4
- 41
- 73
-
1Your answer depends on X11, not every linux system works with the keyboard using X11 drivers. – πάντα ῥεῖ Oct 14 '18 at 10:08