实现gnome桌面的全局热键

gtk+-2.0 没有提供全局热键的功能,而需要使用GConf和 metacity才能得到该功能。

不过有个叫 Mikkel Kamstrup Erlandsen <[email protected]> 的人写了一个

libgtkhotkey ,可以提供该功能,而且是跨平台的。


在Fedora系统上,安装libgtkhotkey

$ su

# yum install libgtkhotkey-devel

基本用法

#include <gtkhotkey.h>

链接参数: `pkg-config --cflags --libs gtkhotkey-1.0`

使用方法(编程)

1、注册hotkey

// used to register the hotkey
static void register_hotkey() {
        GtkHotkeyInfo *hotkey_info = gtk_hotkey_info_new("gtkxrandr", "projecter", "<Super>F3", NULL);

        if ( hotkey_info == NULL ) {
                printf("error 1\n");
        }

        if ( FALSE == gtk_hotkey_info_bind(hotkey_info, NULL) ) {
                printf("[error] can not bind hotkey\n");
        }
        else {
                g_signal_connect(hotkey_info, "activated", G_CALLBACK(hotkey_handler), NULL );
        }
}

2、在gtk中捕捉 actived 信号
// called when the user pressed the combination key.
static void hotkey_handler(GtkHotkeyInfo* hotkey, guint event_time, gpointer user_data) {
        // add your code here
}


3.在初始化的时候调用 register_hotkey

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

    //...

    register_hotkey();

    //...

}



你可能感兴趣的:(实现gnome桌面的全局热键)