
// https://stackoverflow.com/questions/30994628/why-does-xgrabkey-return-badrequest

uses
xlib,
keysym,
x;

var
d :  PXDisplay;
root : TWindow;
keycode, rv : Int64;
evt : TXEvent;

begin
d := XOpenDisplay(nil);
root := DefaultRootWindow(d);
//keycode := XKeysymToKeycode(d, XK_Pause);
keycode := XKeysymToKeycode(d, XK_Scroll_Lock);

writeln('KeyCode: ', keycode);

rv := XGrabKey(d, keycode, AnyModifier, root, 1, GrabModeAsync, GrabModeAsync);
writeln('XGrabKey returned');
writeln(rv);

    while true do begin
        XNextEvent(d, @evt);
        writeln('Got event');
        writeln(evt._type);
case evt._type of
KeyPress:
WriteLn('>>> ',evt.xkey.keycode);
end;
    end;
end.