本文介绍了GDK信号,按键和按键遮罩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图捕获用户键,然后在GUI窗口上按Ctrl + d退出.我的代码如下:

I am trying to catch user key press Ctrl+d on a GUI window to quit. My code looks like this:

static gboolean
callback(GtkWidget   *widget,
         GdkEventKey *event,
         gpointer    data)
{
    if(event->state == GDK_CONTROL_MASK && event->keyval == 'd')
        gtk_main_quit();

    return FASLE;
}

这适用于我的笔记本电脑(Ubuntu 11.04,gcc 4.5.2,libgtk 2.24.4).但是,当我在较新的系统(Ubuntu 12.10,gcc 4.7.2,libgtk 2.24.13)上执行相同的操作时,它不起作用.

This works on my laptop(Ubuntu 11.04, gcc 4.5.2, libgtk 2.24.4). But when I do the same thing on a newer system(Ubuntu 12.10, gcc 4.7.2, libgtk 2.24.13), it doesn't work.

我在if语句之前添加了g_print("%u\n", event->state);,它表明当我按Ctrl时,event->state是20而不是4或1<<文档中的2.如果将GDK_CONTROL_MASK更改为20,它将在较新的系统上运行,而不是在旧系统上.有人请告诉我为什么会这样以及如何解决.

I added g_print("%u\n", event->state); before the if statement, it shows that when I press Ctrl, the event->state is 20 instead of 4 or 1 << 2 in the documentation. If I change the GDK_CONTROL_MASK to 20, it works on the newer system but not the old one. Someone please tell me why this happen and how to fix it.

推荐答案

event->state位图,这意味着值20并不意味着"20而不是4",而是同时4和16".根据标题,值16(1 << 4)对应于MOD2修饰符,该修饰符可能对应于笔记本电脑上的键.

event->state is a bitmap, which means that a value of 20 doesn't mean "20 instead of 4", but "4 and 16 at the same time". According to the headers, the value 16 (1 << 4) corresponds to the MOD2 modifier, which might correspond to the key present on laptops.

一个简单的解决方法是使用&运算符来检查控制权,而忽略其他修饰符:

A simple fix is to use the & operator to check for control while ignoring other modifiers:

    if (event->state & GDK_CONTROL_MASK && event->keyval == 'd')

在两个系统上都可以使用.

which will work on both systems.

这篇关于GDK信号,按键和按键遮罩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 10:33