如果您的应用程序创建了 X11 窗口,或在 X11 窗口内运行(例如在终端模拟器中),您可以捕获针对该特定窗口的 X11 事件。
在应用程序运行的情况下,使用xwininfo 确定窗口的标识符:运行xwininfo,单击目标窗口,然后查找
xwininfo: Window id: ...
在输出中。
然后运行xev,拦截发送到目标窗口的事件:
xev -id ... -event keyboard
用上面确定的窗口 id 替换 ...。
结果比较冗长,但可以进行后处理:
KeyPress event, serial 18, synthetic NO, window 0x4000003,
root 0x187, subw 0x0, time 141181493, (369,-10), root:(369,54),
state 0x0, keycode 116 (keysym 0xff54, Down), same_screen YES,
XLookupString gives 0 bytes:
XmbLookupString gives 0 bytes:
XFilterEvent returns: False
KeyRelease event, serial 21, synthetic NO, window 0x4000003,
root 0x187, subw 0x0, time 141181653, (369,-10), root:(369,54),
state 0x0, keycode 116 (keysym 0xff54, Down), same_screen YES,
XLookupString gives 0 bytes:
XFilterEvent returns: False
KeyRelease event, serial 21, synthetic NO, window 0x4000003,
root 0x187, subw 0x0, time 141182188, (369,-10), root:(369,54),
state 0x0, keycode 116 (keysym 0xff54, Down), same_screen YES,
XLookupString gives 0 bytes:
XFilterEvent returns: False
这不适用于所有 X11 应用程序,但它适用于 Dwarf Fortress。
在 X11 下,窗口管理器不处理单个应用程序的键盘输入。