PyQt/全局热键/HotKey.py

87 lines
2.8 KiB
Python
Raw Normal View History

2017-12-12 00:47:29 +08:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2017年12月11日
@author: Irony."[讽刺]
@site: http://alyl.vip, http://orzorz.vip, https://coding.net/u/892768447, https://github.com/892768447
@email: 892768447@qq.com
@file: HotKey
@description:
'''
import sys
2018-04-22 09:30:50 +08:00
from PyQt5.QtCore import pyqtSignal, Qt
2018-04-22 09:19:30 +08:00
from PyQt5.QtWidgets import QWidget, QApplication, QVBoxLayout, QTextBrowser, QPushButton,\
QMessageBox
2018-04-17 10:34:40 +08:00
import keyboard
2017-12-12 00:47:29 +08:00
__Author__ = "By: Irony.\"[讽刺]\nQQ: 892768447\nEmail: 892768447@qq.com"
__Copyright__ = "Copyright (c) 2017 Irony.\"[讽刺]"
__Version__ = "Version 1.0"
class Window(QWidget):
2018-04-22 09:19:30 +08:00
dialogShow = pyqtSignal()
2017-12-12 00:47:29 +08:00
def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs)
layout = QVBoxLayout(self)
2018-04-22 09:19:30 +08:00
self.dialogShow.connect(self.onShowDialog, type=Qt.QueuedConnection)
2017-12-12 00:47:29 +08:00
self.logView = QTextBrowser(self)
self.logView.append("点击右上角关闭按钮会隐藏窗口,通过热键Alt+S来显示")
self.logView.append("等待热键中")
2018-04-17 10:34:40 +08:00
layout.addWidget(QPushButton(
"退出整个程序", self, clicked=self.onQuit))
2017-12-12 00:47:29 +08:00
layout.addWidget(self.logView)
2018-04-17 10:34:40 +08:00
keyboard.add_hotkey('alt+s', self.onShow, suppress=False) # 显示界面
keyboard.add_hotkey('ctrl+s', self.onHide, suppress=False) # 隐藏界面
keyboard.add_hotkey('shift+s', self.onQuit, suppress=False) # 退出程序
# 拦截系统的快捷键,suppress=True表示拦截,不传递到其它程序
keyboard.add_hotkey(
'win+s', lambda: self.logView.append('按下了win+s'), suppress=True)
keyboard.add_hotkey(
'win+r', lambda: self.logView.append('按下了win+r'), suppress=True)
# 这个东西千万不能拦截掉,要出问题滴
keyboard.add_hotkey(
'ctrl+alt+del', lambda: self.logView.append('😏😏我知道你按了任务管理器😏😏'))
# 这个函数类似while True由于这里有界面GUI的loop事件可以达到类似的效果
# keyboard.wait()#Block forever, like `while True`.==
def onShow(self):
"""显示"""
self.logView.append('按下alt+s')
self.show()
self.showNormal()
2018-04-22 09:19:30 +08:00
self.dialogShow.emit()
def onShowDialog(self):
QMessageBox.information(self, '对话框', '按下alt+s键')
2018-04-17 10:34:40 +08:00
def onHide(self):
"""隐藏"""
self.logView.append('按下ctrl+s')
self.hide()
2017-12-12 00:47:29 +08:00
def onQuit(self):
2018-04-17 10:34:40 +08:00
"""退出函数"""
keyboard.unhook_all_hotkeys() # 取消所有热键
2017-12-12 00:47:29 +08:00
QApplication.instance().quit()
def closeEvent(self, event):
# 忽略关闭窗口,直接隐藏
self.hide()
return event.ignore()
if __name__ == "__main__":
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())