I'm trying to send a key press event to the X11 display but the events are not getting sent.
Here's my current attempt:
void sendEvent(int scanCode, bool isPressed) {
unsigned long focusedWindow;
int focusRevert;
int mask = isPressed ? KeyPressMask : KeyReleaseMask;
XGetInputFocus(display, &focusedWindow, &focusRevert);
XKeyEvent event;
memset(&event, 0, sizeof(XKeyEvent));
event.keycode = scanCode + 8;
event.type = isPressed ? KeyPress : KeyRelease;
event.root = focusedWindow;
event.display = display;
XSendEvent(display, focusedWindow, 1, mask, (XEvent *)&event);
XSync(display, 0);
}
I tried debugging, XSendEvent
return value is 1 which is for success, but the events didn't registered, as for example I tried sending a CapsLock key event, but seems like the toggle state of the key was as it was (no changes).
I also tried to add a sleep so if anything asynchronous happens before function exits, I can catch up.
So I'm totally confused what is the problem in the code, and why is it not sending the event correctly.