跨平台Qt应用程序中的全局热键 [英] Global hotkeys in a cross-platform Qt application

查看:120
本文介绍了跨平台Qt应用程序中的全局热键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Qt在C ++中创建一个跨平台实用程序,为此我需要快捷键(或热键,不确定它们之间的区别).从本质上讲,该应用程序将运行,并且仅在系统任务栏中显示为图标,并且在您按某些快捷键(例如Ctrl-Shift-f4或类似的东西)时会执行操作.

我的印象是,除非应用程序处于焦点位置,否则Qt无法提供处理快捷键的方法,而在我看来,这是无法实现的.因此,这是可以解决的(但是,如果 是一个可行的选择,请提示我.)

我已经找到了很多示例/文档来说明如何使用Linux的Xlib/Xcb,用于Windows的win32 api和用于osx的carbon来执行此操作,但是我很难找到一种方法来做到这一点在Qt应用程序范围内适用.

满足我需要的方式是什么?

解决方案

我正在研究这个未解决的老问题,因为在使用QML时,我遇到了同样的问题.通过快捷方式 QML类型,您可以指定 context 属性,但是您仍然需要一个有针对性的应用程序或窗口

但是,我发现了一个解决此问题的库: QHotkey .在Github上将自己描述为:

桌面Qt应用程序的全局快捷键/热键.

QHotkey是一个类,可用于创建热键/全局快捷方式,也就是可在任何地方使用的快捷方式,而与应用程序状态无关.这意味着您的应用程序可以处于活动状态,非活动状态,最小化或根本不可见,并且仍会收到快捷方式.

QHockey可通过 qpm 打包使用,并且可以直接从C ++使用.

I'm creating a cross platform utility, in C++ using Qt, for which I need to have shortcut keys (or hotkeys, not really sure about the difference). Essentially the application will run and only be visible as an icon in the system tray, and do stuff when you press certain shortcut keys (eg, Ctrl-Shift-f4 or something).

I am under the impression that Qt doesn't provide a way to handle shortcut keys unless the application is in focus, which, in my case it won't be. So, that's out (if however that is a viable option, please clue me in).

I've found plenty of examples/documentation explaining how to do this using Xlib/Xcb for linux, win32 api for windows, and carbon for osx, but I'm having a hard time finding a way to do this that would be applicable within the scope of a Qt application.

What would be a way to accomplish what I need?

解决方案

I'm digging up this old non-answered question because, using QML, I encountered the same issue. The Shortcut QML type allow you to specify a context property but you still need a focused application or window.

However, I found a library resolving this issue : QHotkey. Describing itself on Github as :

A global shortcut/hotkey for Desktop Qt-Applications.

The QHotkey is a class that can be used to create hotkeys/global shortcuts, aka shortcuts that work everywhere, independent of the application state. This means your application can be active, inactive, minimized or not visible at all and still receive the shortcuts.

QHockey is available as a package through qpm and can be used directly from C++.

这篇关于跨平台Qt应用程序中的全局热键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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