[Programming] XFakeKey

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



// 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;
event.type = KeyPress;
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.
return 0;

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

[Linux] Private Key 不能登入的除錯方法與可能的問題點

最簡單的除錯方法是 $ ssh HOST -v 可能的問題點有: File/Dir Permission # 目錄是 700, 檔案是 600 Server 設定 RSAAuthentication yes PubkeyAuthentication yes...