获取使用Gnome库(GDK)键盘功能键的状态只获取初始状态 [英] Getting keyboard modifiers state using Gnome libs (GDK) fetches initial state only

查看:411
本文介绍了获取使用Gnome库(GDK)键盘功能键的状态只获取初始状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图打通的目的GNOME GDK或者GTK库当前键盘功能键的状态来实现无障碍GNOME shell扩展,显示该状态。

I'm trying to get the current keyboard modifiers state through gnome GDK or GTK library in aim to implement an accessibility gnome shell extension that shows that state.

我知道如何使用的xlib获得thier状态,但没有完全的侏儒GJS结合。

I know how to get thier state using xlib, but there is not full binding for gnome gjs.

下code只得到初始状态。它不更新状态。

The code below get only the initial state. It does not update state.

/*
 * compiling: gcc `pkg-config --cflags gdk-3.0` -o gdk_mod gdk_mod.c `pkg-config --libs gdk-3.0`
 */

#include <gdk/gdk.h>

int main (int argc, char **argv) {

    gdk_init(&argc, &argv);

    GdkDisplay * disp;
    disp = gdk_display_open(NULL);
    if (disp!=NULL) g_printf("display connected!\n");

    GdkKeymap * kmap;
    kmap = gdk_keymap_get_for_display(disp);

    guint state;
    state = gdk_keymap_get_modifier_state(kmap);
    g_printf("mod state: %x\n", state);

    while (1) {
        g_usleep(1000000);
        //kmap = gdk_keymap_get_for_display(disp);
        state = gdk_keymap_get_modifier_state(kmap);
        g_printf("mod state: %x\n", state);
    }

}

下面与CAPS示例输出锁定功能则处于非活动状态,但没有任何变化:

Here an example output with CAPS lock active then inactive but no change:

$ ./gdk_mod 
display found!
mod state: 2
mod state: 2
mod state: 2
mod state: 2
mod state: 2
^C

目前使用的Kubuntu 15.04。

Currently using Kubuntu 15.04.

这有什么错我的code?

What's wrong with my code?

推荐答案

您将需要运行GTK +事件循环这个工作。事件循环是GLib库的一部分的主循环的。当你调用进入主循环(),这个主循环运行。我不知道这是否轮询事件,或有事件推到它,但它不会要求键盘状态的飞行就像你试图做。

You are going to need to run the GTK+ event loop for this to work. The event loop is part of GLib's main loop. When you call gtk_main(), this main loop is run. I don't know if it polls for events or has events pushed to it, but it won't ask for the keyboard state on the fly like you were trying to do.

设置最简单的方法GDK是通过GTK +使用 gtk_init()进入主循环()。你可以对自己使用GDK,但我不知道怎么办。你似乎已经想通了,它的工作原理。

The easiest way to set up GDK is to do it via GTK+ by using gtk_init() and gtk_main(). You can use GDK on its own, but I don't know how. You seemed to have figured it out, which works.

和,而不是调用 g_usleep(),其中只有几个街区你的程序,你可以连接一个周期性的超时进入主循环。这与 g_timeout_add进行()。您传递给 g_timeout_add函数()返回决定计时器是否应停止或不是一个布尔值,所以你不必担心你的补赛功能,耍贫嘴会为你做的。

And instead of calling g_usleep(), which just blocks your program, you can hook a periodic timeout into the main loop. This is done with g_timeout_add(). The function that you pass to g_timeout_add() returns a Boolean value that decides whether the timer should be stopped or not, so you don't have to worry about rescheduling your function, as GLib will do that for you.

这篇关于获取使用Gnome库(GDK)键盘功能键的状态只获取初始状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆