好的,如果您在 X11 下并且想要获取 kbd,则需要进行抓取。如果你不是,我唯一的好答案是来自终端的 ncurses。
以下是您如何从键盘上抓取所有内容并再次释放:
/* 演示代码,需要更多的错误检查,编译
* 使用“gcc nameofthisfile.c -lX11”。
/* 奇怪的降价格式如下。啊!*/
#include <X11/Xlib.h>
int main(int argc, char **argv)
{
显示 *dpy;
XEvent EV;
字符 *s;
无符号整数 kc;
诠释退出= 0;
if (NULL==(dpy=XOpenDisplay(NULL))) {
错误(argv[0]);
退出(1);
}
/*
* 您可能希望将指针扭曲到您知道的某个地方
* 与任何会耗尽事件的东西无关。
* (void)XWarpPointer(dpy, None, DefaultRootWindow(dpy), 0, 0, 0, 0, x, y);
*/
XGrabKeyboard(dpy, DefaultRootWindow(dpy),
真,GrabModeAsync,GrabModeAsync,当前时间);
printf("键盘被抓!按 'q' 退出!\n"
"如果这个工作被杀死或者你卡住了,使用 Ctrl-Alt-F1\n"
“切换到控制台(如果可能)并运行\n”
"松开键盘。\n");
/* 一个非常简单的事件循环:从“man XEvent”开始了解更多信息。*/
/* 另请参阅“apropos XGrab”,了解锁定访问权限的各种方法
*某些类型的信息。出入服务器 */
对于 (;!quit;) {
XNextEvent(dpy, &ev);
开关(ev.type){
案例按键:
kc = ((XKeyPressedEvent*)&ev)->keycode;
s = XKeysymToString(XKeycodeToKeysym(dpy, kc, 0));
/* s 为 NULL 或静态无接触返回字符串。*/
if (s) printf("KEY:%s\n", s);
if (!strcmp(s, "q")) quit=~0;
休息;
案例曝光:
/* 通常,最好将剩余的暴露暴露在
* 避免访问 Blinky's Fun Club。*/
while (XCheckTypedEvent(dpy, Expose, &ev)) /* 空体 */ ;
休息;
案例按钮按下:
案例按钮释放:
案例密钥发布:
案例 MotionNotify:
案例配置通知:
默认:
休息;
}
}
XUngrabKeyboard(dpy, CurrentTime);
如果(XCloseDisplay(dpy)){
错误(argv[0]);
退出(1);
}
返回0;
}
Run this from a terminal and all kbd events should hit it. I'm testing it under Xorg
but it uses venerable, stable Xlib mechanisms.
Hope this helps.
BE CAREFUL with grabs under X. When you're new to them, sometimes it's a good
idea to start a time delay process that will ungrab the server when you're
testing code and let it sit and run and ungrab every couple of minutes.
It saves having to kill or switch away from the server to externally reset state.
From here, I'll leave it to you to decide how to multiplex renderes. Read
the XGrabKeyboard docs and XEvent docs to get started.
If you have small windows exposed at the screen corners, you could jam
the pointer into one corner to select a controller. XWarpPointer can
shove the pointer to one of them as well from code.
One more point: you can grab the pointer as well, and other resources. If you had one controller running on the box in front of which you sit, you could use keyboard and mouse input to switch it between open sockets with different renderers. You shouldn't need to resize the output window to less than full screen anymore with this approach, ever. With more work, you could actually drop alpha-blended overlays on top using the SHAPE and COMPOSITE extensions to get a nice overlay feature in response to user input (which might count as gilding the lily).