Skip to content

Instantly share code, notes, and snippets.

@pchome

pchome/key.c Secret

Created July 3, 2019 22:50
Show Gist options
  • Save pchome/d921830565d91514654093593a4a0483 to your computer and use it in GitHub Desktop.
Save pchome/d921830565d91514654093593a4a0483 to your computer and use it in GitHub Desktop.
Shift + d to trigger more "d"s
#include <unistd.h>
#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Intrinsic.h>
#include <X11/extensions/XTest.h>
int main() {
Display *dpy = XOpenDisplay(NULL);
KeyCode keycode = XKeysymToKeycode(dpy, XK_D);
XEvent ev;
Window window = DefaultRootWindow(dpy);
Bool keyPressed = False;
unsigned int key_modifier = ShiftMask;
unsigned int modifiers = AnyModifier;
XGrabKey(dpy, keycode, modifiers, window, False, GrabModeAsync, GrabModeAsync);
XSelectInput(dpy, window, KeyPressMask);
// Wait for hotkey
while(True) {
XNextEvent(dpy, &ev);
switch(ev.type) {
case KeyPress:
// Check Shift + d hotkey
if ((ev.xkey.state & key_modifier) == (key_modifier)) {
printf("Hotkey pressed!\n");
keyPressed = True;
XUngrabKey(dpy, keycode, modifiers, window);
}
default: break;
}
if(keyPressed)
break;
}
printf("Charging ...\n");
sleep(2);
for (int i=0; i<10; i++) {
XTestFakeKeyEvent(dpy, keycode, True, 0);
XTestFakeKeyEvent(dpy, keycode, False, 0);
XFlush(dpy);
sleep(1);
}
XCloseDisplay(dpy);
printf(" ... Done!\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment