实现gnome桌面的全局热键

gtk+-2.0本身不支持全局热键,但通过libgtkhotkey库可以实现这一功能。Mikkel Kamstrup Erlandsen创建的libgtkhotkey是跨平台的,适用于Fedora等系统。安装后,通过注册hotkey、连接回调函数以及捕捉'activated'信号来设置和响应全局热键。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

不过有个叫 Mikkel Kamstrup Erlandsen <mikkel.kamstrup@gmail.com> 的人写了一个

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();

    //...

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值