capture input and send key events at the same time

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












I'm looking to create a simple input method program. Currently, I'm able to capture input from any window using passive key grabs (thanks user dirkt!).



Separately, I'm also able to send key presses using XTestFakeKeyEvent().



The issue is that I can't put these two parts together. When emitting a fake key event, it is received by my program instead of the intended application that has focus. This occurs even though my program does not have a passive grab on the keycode emitted by the fake event. I suspect this is because I'm getting events from the root window, but if I don't do that, I don't think I can capture input from other windows.



#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/extensions/XTest.h>
#include <stdio.h>

int main()

Display *display;
Window rootwindow;
XEvent event;

display = XOpenDisplay(NULL);
rootwindow = DefaultRootWindow(display);

XSelectInput(display, rootwindow, KeyPressMask);

unsigned int modifiers = Mod2Mask,
LockMask ;

int keys =
XKeysymToKeycode(display, XK_A),
XKeysymToKeycode(display, XK_B),
...
XKeysymToKeycode(display, XK_Z)
;

int l;
int m;
for(l = 0; l < sizeof(keys)/sizeof(int); l++)
for(m = 0; m < sizeof(modifiers)/sizeof(unsigned int); m++)
XGrabKey(display, keys[l], modifiers[m], rootwindow, False, GrabModeAsync, GrabModeAsync);



int repeat = 0;

while (1)

XNextEvent(display, &event);

if (event.type == KeyPress && event.xkey.send_event == 0)
printf("KeyPress: keycode %u state %u send-event %dn", event.xkey.keycode, event.xkey.state, event.xkey.send_event);
fflush(stdout);

if(repeat == 0) //to avoid infinite loops
repeat = 1;

XTestFakeKeyEvent(display, XKeysymToKeycode(display, XK_7), 1, CurrentTime);
XTestFakeKeyEvent(display, XKeysymToKeycode(display, XK_7), 0, CurrentTime);





XCloseDisplay(display);
return 0;







share|improve this question






















  • You may want to have a look at how xbindkeys does it (and xte/xdotool which you'd typically use there to send key events).
    – Stéphane Chazelas
    Apr 23 at 14:44














up vote
2
down vote

favorite












I'm looking to create a simple input method program. Currently, I'm able to capture input from any window using passive key grabs (thanks user dirkt!).



Separately, I'm also able to send key presses using XTestFakeKeyEvent().



The issue is that I can't put these two parts together. When emitting a fake key event, it is received by my program instead of the intended application that has focus. This occurs even though my program does not have a passive grab on the keycode emitted by the fake event. I suspect this is because I'm getting events from the root window, but if I don't do that, I don't think I can capture input from other windows.



#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/extensions/XTest.h>
#include <stdio.h>

int main()

Display *display;
Window rootwindow;
XEvent event;

display = XOpenDisplay(NULL);
rootwindow = DefaultRootWindow(display);

XSelectInput(display, rootwindow, KeyPressMask);

unsigned int modifiers = Mod2Mask,
LockMask ;

int keys =
XKeysymToKeycode(display, XK_A),
XKeysymToKeycode(display, XK_B),
...
XKeysymToKeycode(display, XK_Z)
;

int l;
int m;
for(l = 0; l < sizeof(keys)/sizeof(int); l++)
for(m = 0; m < sizeof(modifiers)/sizeof(unsigned int); m++)
XGrabKey(display, keys[l], modifiers[m], rootwindow, False, GrabModeAsync, GrabModeAsync);



int repeat = 0;

while (1)

XNextEvent(display, &event);

if (event.type == KeyPress && event.xkey.send_event == 0)
printf("KeyPress: keycode %u state %u send-event %dn", event.xkey.keycode, event.xkey.state, event.xkey.send_event);
fflush(stdout);

if(repeat == 0) //to avoid infinite loops
repeat = 1;

XTestFakeKeyEvent(display, XKeysymToKeycode(display, XK_7), 1, CurrentTime);
XTestFakeKeyEvent(display, XKeysymToKeycode(display, XK_7), 0, CurrentTime);





XCloseDisplay(display);
return 0;







share|improve this question






















  • You may want to have a look at how xbindkeys does it (and xte/xdotool which you'd typically use there to send key events).
    – Stéphane Chazelas
    Apr 23 at 14:44












up vote
2
down vote

favorite









up vote
2
down vote

favorite











I'm looking to create a simple input method program. Currently, I'm able to capture input from any window using passive key grabs (thanks user dirkt!).



Separately, I'm also able to send key presses using XTestFakeKeyEvent().



The issue is that I can't put these two parts together. When emitting a fake key event, it is received by my program instead of the intended application that has focus. This occurs even though my program does not have a passive grab on the keycode emitted by the fake event. I suspect this is because I'm getting events from the root window, but if I don't do that, I don't think I can capture input from other windows.



#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/extensions/XTest.h>
#include <stdio.h>

int main()

Display *display;
Window rootwindow;
XEvent event;

display = XOpenDisplay(NULL);
rootwindow = DefaultRootWindow(display);

XSelectInput(display, rootwindow, KeyPressMask);

unsigned int modifiers = Mod2Mask,
LockMask ;

int keys =
XKeysymToKeycode(display, XK_A),
XKeysymToKeycode(display, XK_B),
...
XKeysymToKeycode(display, XK_Z)
;

int l;
int m;
for(l = 0; l < sizeof(keys)/sizeof(int); l++)
for(m = 0; m < sizeof(modifiers)/sizeof(unsigned int); m++)
XGrabKey(display, keys[l], modifiers[m], rootwindow, False, GrabModeAsync, GrabModeAsync);



int repeat = 0;

while (1)

XNextEvent(display, &event);

if (event.type == KeyPress && event.xkey.send_event == 0)
printf("KeyPress: keycode %u state %u send-event %dn", event.xkey.keycode, event.xkey.state, event.xkey.send_event);
fflush(stdout);

if(repeat == 0) //to avoid infinite loops
repeat = 1;

XTestFakeKeyEvent(display, XKeysymToKeycode(display, XK_7), 1, CurrentTime);
XTestFakeKeyEvent(display, XKeysymToKeycode(display, XK_7), 0, CurrentTime);





XCloseDisplay(display);
return 0;







share|improve this question














I'm looking to create a simple input method program. Currently, I'm able to capture input from any window using passive key grabs (thanks user dirkt!).



Separately, I'm also able to send key presses using XTestFakeKeyEvent().



The issue is that I can't put these two parts together. When emitting a fake key event, it is received by my program instead of the intended application that has focus. This occurs even though my program does not have a passive grab on the keycode emitted by the fake event. I suspect this is because I'm getting events from the root window, but if I don't do that, I don't think I can capture input from other windows.



#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/extensions/XTest.h>
#include <stdio.h>

int main()

Display *display;
Window rootwindow;
XEvent event;

display = XOpenDisplay(NULL);
rootwindow = DefaultRootWindow(display);

XSelectInput(display, rootwindow, KeyPressMask);

unsigned int modifiers = Mod2Mask,
LockMask ;

int keys =
XKeysymToKeycode(display, XK_A),
XKeysymToKeycode(display, XK_B),
...
XKeysymToKeycode(display, XK_Z)
;

int l;
int m;
for(l = 0; l < sizeof(keys)/sizeof(int); l++)
for(m = 0; m < sizeof(modifiers)/sizeof(unsigned int); m++)
XGrabKey(display, keys[l], modifiers[m], rootwindow, False, GrabModeAsync, GrabModeAsync);



int repeat = 0;

while (1)

XNextEvent(display, &event);

if (event.type == KeyPress && event.xkey.send_event == 0)
printf("KeyPress: keycode %u state %u send-event %dn", event.xkey.keycode, event.xkey.state, event.xkey.send_event);
fflush(stdout);

if(repeat == 0) //to avoid infinite loops
repeat = 1;

XTestFakeKeyEvent(display, XKeysymToKeycode(display, XK_7), 1, CurrentTime);
XTestFakeKeyEvent(display, XKeysymToKeycode(display, XK_7), 0, CurrentTime);





XCloseDisplay(display);
return 0;









share|improve this question













share|improve this question




share|improve this question








edited Apr 23 at 14:42









Stéphane Chazelas

283k53521854




283k53521854










asked Nov 5 '17 at 7:37









Elfalem

211




211











  • You may want to have a look at how xbindkeys does it (and xte/xdotool which you'd typically use there to send key events).
    – Stéphane Chazelas
    Apr 23 at 14:44
















  • You may want to have a look at how xbindkeys does it (and xte/xdotool which you'd typically use there to send key events).
    – Stéphane Chazelas
    Apr 23 at 14:44















You may want to have a look at how xbindkeys does it (and xte/xdotool which you'd typically use there to send key events).
– Stéphane Chazelas
Apr 23 at 14:44




You may want to have a look at how xbindkeys does it (and xte/xdotool which you'd typically use there to send key events).
– Stéphane Chazelas
Apr 23 at 14:44















active

oldest

votes











Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f402602%2fcapture-input-and-send-key-events-at-the-same-time%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f402602%2fcapture-input-and-send-key-events-at-the-same-time%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?