检测C中哪个键被按下
我试图找到一种方法来找出在 C 中按下了哪个键。这将在图形环境中,用 GTK2 编写,但我不认为答案就在那里。我想我也许可以使用 Xlib 来做到这一点,但我还没有找到任何结论性的东西。
有人对如何做到这一点有任何建议吗?
我已经设法使用以下代码捕获按键:
GtkWidget *window;
void gtk_widget_set_events(window,GDK_KEY_RELEASE_MASK);
g_signal_connect(window,"key_release_event",G_CALLBACK(hello),NULL);
但是,我想确定按下了哪个键。从 Aditya Kumar 发布的链接中,我知道答案在于使用 GdkEventKey,因为它是一个具有 keyval 字段的结构,但我似乎无法获得正确的语法。获取这个号码的正确方法是什么?
这是我尝试过的方法:
static void hello( GtkWidget *widget,
guint data ){
g_print ("Hello World, %d was pressed\n",data);}
我尝试在捕获 key_release_event 时通过这样做来提供“数据”:
g_signal_connect(window,"key_release_event",G_CALLBACK(hello),GdkEventKey.keyval);
但是,我收到如下编译器错误:
hello.c:85:5: error: expected ‘)’ before ‘.’ token
hello.c:85:5: error: expected expression before ‘,’ token
I'm trying to find a way to find out which key is pressed down in C. This will be in a graphical environment, written in GTK2, but I don't think the answer lies there. I think I might be able to do this using Xlib, but I haven't been able to find anything conclusive on this.
Does anyone have any suggestions on how to do this?
I've managed to catch a keypress using the follow code:
GtkWidget *window;
void gtk_widget_set_events(window,GDK_KEY_RELEASE_MASK);
g_signal_connect(window,"key_release_event",G_CALLBACK(hello),NULL);
However, I would like to identify which key is pressed. From the link posted by Aditya Kumar, I know the answer lies with using GdkEventKey, since it is a structure which has a keyval field, but I cannot seem to get the syntax right. What is the correct way of getting this number?
This is a method I've tried:
static void hello( GtkWidget *widget,
guint data ){
g_print ("Hello World, %d was pressed\n",data);}
I tried supplying "data" by doing this when I catch the key_release_event:
g_signal_connect(window,"key_release_event",G_CALLBACK(hello),GdkEventKey.keyval);
However, I get a compiler error like so:
hello.c:85:5: error: expected ‘)’ before ‘.’ token
hello.c:85:5: error: expected expression before ‘,’ token
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您原来的语法是正确的。
其中 key_event 函数看起来像这样(注意我使用 gdk_keyval_name 将 keyval int 值转换为字符串以进行打印):
这是一个完整的示例程序:
You are correct with your original syntax.
Where the key_event function looks something like (note I am using the gdk_keyval_name to convert the keyval int value to a string for printing):
Here's a complete example program:
在查看 gdk 参考手册时,我认为您可以使用它捕获键盘事件,除非您特别想要一个“C”程序。
这是可以帮助您的链接。
http://www.gtk.org/api/2.6/gdk /gdk-Keyboard-Handling.html
while looking at the gdk reference manual i think you can capture the keyboard events using this unless you specifically want to have a 'C' program.
Here is the link to help you out.
http://www.gtk.org/api/2.6/gdk/gdk-Keyboard-Handling.html
event->keyval 是一个指向结构的指针,其中 keyval 包含按下的键的整数值,这已在上面的函数 gdk_keyval_name (event->keyval) 中使用,该函数获取键的实际名称。
event->keyval is a pointer to a struct, where keyval contains a integer value for the key pressed, this has been used above in a function gdk_keyval_name (event->keyval) which gets a actual name for the key.