Pyside(或PyQt)全局快捷方式 [英] Pyside (or PyQt) Global shortcuts

查看:105
本文介绍了Pyside(或PyQt)全局快捷方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的应用程序创建一个快捷方式,当我的应用程序没有焦点时它仍然会寻找输入. QShortcut和QApplicationShortcut似乎无法满足我的需求.

这就是我所拥有的.我希望能够在另一个应用程序具有焦点的情况下触发self.onplaybutton().

self.shortcut = QtGui.QShortcut(self.mainWindow)
self.shortcut.setKey('A')
self.shortcut.activated.connect(lambda: self.onplaybutton())

解决方案

目前,Qt尚未提供用于全局快捷方式的API,因此您不能直接使用PySide或PyQt来做到这一点.

相反,您将不得不使用外部库,唯一可用的似乎是 libqxt ,其中具有 QxtGlobalShortcut 类. >

幸运的是, pygs 项目为QxtGlobalShortcut类提供了PyQt绑定,因此可以提供至少可以得到想要的东西.


更新:

要获得PyQt5支持,请使用 PyQxtGlobalShortcut ,它是pygs的一个分支.该项目自2016年以来未进行过更新,但是 libqxt 似乎仍在积极维护中,因此可能可以毫不费力地将其更新. (PS:实际上,在此处).

I am looking to create a shortcut for my application that will still look for input while my application does not have focus. QShortcut and QApplicationShortcut don't seem to do what I need.

This is what I have. I would like to be able to trigger self.onplaybutton() while a different application has focus.

self.shortcut = QtGui.QShortcut(self.mainWindow)
self.shortcut.setKey('A')
self.shortcut.activated.connect(lambda: self.onplaybutton())

解决方案

At the moment, Qt doesn't provide an API for global shortcuts, so you can't do this directly with either PySide or PyQt.

Instead, you will have to use an external library, and the only thing available seems to be libqxt, which has a QxtGlobalShortcut class.

Fortunately, the pygs project provides a PyQt bindings for the QxtGlobalShortcut class, so that provides at least way to get what you want.


UPDATE:

For PyQt5 support, there's PyQxtGlobalShortcut, which is a fork of pygs. This project hasn't been updated since 2016, but libqxt still seems to be actively maintained, so it probably could be brought up to date without too much difficulty. (PS: in fact, there's a more recent fork here).

这篇关于Pyside(或PyQt)全局快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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