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] git-bash 底下的工具

因為工作轉到 Windows 平台上的關係,所以很多工具改到 Windows 上面運作,跟著在 TortoiseGit 底下使用 git-bash 來維護自己的專案原始碼。結果就是裝了前面提過的 auto-hotkey 使用熱鍵來提昇自己的平台操作速度; 但除了 hotkey...