XKeyEvent createKeyEvent(Display *display, Window &win,
Window &winRoot, bool press,
int keycode, int modifiers)
{
XKeyEvent event;
event.display = display;
event.window = win;
event.root = winRoot;
event.subwindow = None;
event.time = CurrentTime;
event.x = 1;
event.y = 1;
event.x_root = 1;
event.y_root = 1;
event.same_screen = TRUE;
event.state = modifiers;
event.keycode = XKeysymToKeycode(display,keycode);
if(press)
event.type = KeyPress;
else
event.type = KeyRelease;
return event;
}
XKeyEvent createKeyEvent(Display *display, Window &win,
Window &winRoot, bool press,
int keycode, int modifiers)
{
XKeyEvent event;
event.display = display;
event.window = win;
event.root = winRoot;
event.subwindow = None;
event.time = CurrentTime;
event.x = 1;
event.y = 1;
event.x_root = 1;
event.y_root = 1;
event.same_screen = TRUE;
event.state = modifiers;
event.keycode = XKeysymToKeycode(display,keycode);
if(press)
event.type = KeyPress;
else
event.type = KeyRelease;
return event;
}
To copy to clipboard, switch view to plain text mode
int tsKeyboardWidget::SendKeys(int keycode)
{
//modifier=pubModifier->text().toInt();
// Obtain the X11 display.
Display *display = XOpenDisplay(NULL);
if(display == NULL)
return -1;
// Get the root window for the current display.
Window winRoot;// = XRootWindow(display,1);
// Find the window which has the current keyboard focus.
Window winFocus;
int revert=RevertToParent;
//int RevertToParent=0;
XGetInputFocus(display, &winFocus,&revert);
winFocus=winFocus+1;
// Send a fake key press event to the window.
XSelectInput(display, winFocus,FocusChangeMask|KeyPressMask|KeyReleaseMask);
XMapWindow(display, winFocus);
XKeyEvent event = createKeyEvent(display, winFocus, winRoot, true, keycode, modifier);
XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);
event = createKeyEvent(display, winFocus, winRoot, false, keycode, modifier);
XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);
XCloseDisplay(display);
return 0;
}
int tsKeyboardWidget::SendKeys(int keycode)
{
//modifier=pubModifier->text().toInt();
// Obtain the X11 display.
Display *display = XOpenDisplay(NULL);
if(display == NULL)
return -1;
// Get the root window for the current display.
Window winRoot;// = XRootWindow(display,1);
// Find the window which has the current keyboard focus.
Window winFocus;
int revert=RevertToParent;
//int RevertToParent=0;
XGetInputFocus(display, &winFocus,&revert);
winFocus=winFocus+1;
// Send a fake key press event to the window.
XSelectInput(display, winFocus,FocusChangeMask|KeyPressMask|KeyReleaseMask);
XMapWindow(display, winFocus);
XKeyEvent event = createKeyEvent(display, winFocus, winRoot, true, keycode, modifier);
XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);
event = createKeyEvent(display, winFocus, winRoot, false, keycode, modifier);
XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);
XCloseDisplay(display);
return 0;
}
To copy to clipboard, switch view to plain text mode
Bookmarks