2007/04/25

[Programming] XFakeKey

參考這裡的文章,調整了一點點,讓 XFakeKey 可以再隨意調用出自已想要的 keycode :

#include
#include

#include
#include

// A full list of available codes can be found in /usr/include/X11/keysymdef.h

// Function to create a keyboard 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.keycode = XKeysymToKeycode(display, keycode);
event.state = modifiers;
if(press)
event.type = KeyPress;
else
event.type = KeyRelease;

return event;
}

main(int argc, char *argv[])
{
int keycode;
// Obtain the X11 display.
Display *display = XOpenDisplay(0);
if(display == NULL)
return -1;

keycode = atoi(argv[1]);
if (keycode>65535||keycode<=0) return -1;
// Get the root window for the current display.
Window winRoot = XDefaultRootWindow(display);

// Find the window which has the current keyboard focus.
Window winFocus;
int revert;
XGetInputFocus(display, &winFocus, &revert);

// Send a fake key press event to the window.
XKeyEvent event = createKeyEvent(display, winFocus, winRoot, true, keycode, 0);
XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);

// Send a fake key release event to the window.
event = createKeyEvent(display, winFocus, winRoot, false, keycode, 0);
XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);

// Done.
XCloseDisplay(display);
return 0;
}


// 其實是因為 fvwm 的關係,所以想要用 key 綁音效的效果出來,但是 fvwm 的 FvwmFakeKey 有限制,所以還是自已來做 FakeKey 比較快。
張貼留言

[Windows] 好用的小工具: AutoHotKey

做為一個 Linux 的愛好者,轉移到 Windows 上面的時候,往往難以適應 Windows 調整快捷鍵的方式,所以 google 了一下,結果發現很多人愛用的 "Auto Hot key"。不多說,就來給一個範例說明如何使用 Auto Hot key 來...