I have this piece of code taken from here: https://stackoverflow.com/questions/18281412/check-keypress-in-c-on-linux
#include <X11/Xlib.h>
#include <iostream>
#include "X11/keysym.h"
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;
}
int main(int argc, char **argv) {
while (1) {
if (key_is_pressed(XK_Control_L)) {
std::cout << "left control pressed" << std::endl;
}
}
return 0;
};
compiled with
g++ main.cpp -lX11
when I run the program the desired string doesn't get printed when I press left control