慕岚 发表于 2022-8-29 10:37:52

如何中止已经注册的热键

附源码,
目前已经实现了,注册热键和输出文字的功能,但是卡在了中止任务这里
我想实现的是按下F1后开启功能,再次按下F1之后则关闭功能,尝试了很多办法,已经注册的热键没办法修改,不知道各路大神有没有解决的办法
在线交流,求大神帮忙给个思路


主程序:
import os
from threading import Thread

from pyqt5_plugins.examplebutton import QtWidgets
from PyQt5.QtWidgets import QApplication,QMainWindow
from PyQt5.Qt import QThread
import sys
from main_GUI import Ui_Form


class MyMainWindow(QMainWindow, Ui_Form,QThread):
    def __init__(self, parent=None):
      super(MyMainWindow, self).__init__(parent)
      self.setupUi(self)
    def closeEvent(self, event):
      reply = QtWidgets.QMessageBox.question(self, '关闭', "是否要关闭程序?",QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No,QtWidgets.QMessageBox.No)
      if reply == QtWidgets.QMessageBox.Yes:
            event.accept()
            os._exit(0)
      else:
            event.ignore()

    def start_rw(self):
      from pynput.keyboard import Key, Controller
      from system_hotkey import SystemHotkey
      hk = SystemHotkey()
      if self.checkBox.isChecked() == True:
            print("开启")
            rekeyboard = Controller()
            hk.register(('q',), callback=lambda x: rekeyboard.type(self.lineEdit.text()))
            hk.register(('w',), callback=lambda x: rekeyboard.type(self.lineEdit_2.text()))
      if self.checkBox.isChecked() == False:
            pass



    def start(self):
      thread = Thread(target=self.start_rw)
      thread.start()
if __name__ == '__main__':
    app = QApplication(sys.argv)
    myWin = MyMainWindow()
    myWin.show()
    sys.exit(app.exec_())


PYQT5程序,文件名:main_gui.py
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'main_GUI.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
      Form.setObjectName("Form")
      Form.resize(242, 202)
      self.label = QtWidgets.QLabel(Form)
      self.label.setGeometry(QtCore.QRect(30, 30, 81, 31))
      font = QtGui.QFont()
      font.setFamily("微软雅黑 Light")
      font.setPointSize(16)
      self.label.setFont(font)
      self.label.setMouseTracking(True)
      self.label.setAutoFillBackground(False)
      self.label.setFrameShape(QtWidgets.QFrame.Box)
      self.label.setLineWidth(1)
      self.label.setMidLineWidth(0)
      self.label.setTextFormat(QtCore.Qt.AutoText)
      self.label.setAlignment(QtCore.Qt.AlignCenter)
      self.label.setOpenExternalLinks(False)
      self.label.setObjectName("label")
      self.lineEdit = QtWidgets.QLineEdit(Form)
      self.lineEdit.setGeometry(QtCore.QRect(30, 60, 81, 31))
      font = QtGui.QFont()
      font.setFamily("微软雅黑 Light")
      font.setPointSize(16)
      self.lineEdit.setFont(font)
      self.lineEdit.setObjectName("lineEdit")
      self.lineEdit_2 = QtWidgets.QLineEdit(Form)
      self.lineEdit_2.setGeometry(QtCore.QRect(120, 60, 81, 31))
      font = QtGui.QFont()
      font.setFamily("微软雅黑 Light")
      font.setPointSize(16)
      self.lineEdit_2.setFont(font)
      self.lineEdit_2.setLayoutDirection(QtCore.Qt.LeftToRight)
      self.lineEdit_2.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter)
      self.lineEdit_2.setCursorMoveStyle(QtCore.Qt.LogicalMoveStyle)
      self.lineEdit_2.setObjectName("lineEdit_2")
      self.label_2 = QtWidgets.QLabel(Form)
      self.label_2.setGeometry(QtCore.QRect(120, 30, 81, 31))
      font = QtGui.QFont()
      font.setFamily("微软雅黑 Light")
      font.setPointSize(16)
      self.label_2.setFont(font)
      self.label_2.setMouseTracking(True)
      self.label_2.setAutoFillBackground(False)
      self.label_2.setFrameShape(QtWidgets.QFrame.Box)
      self.label_2.setLineWidth(1)
      self.label_2.setMidLineWidth(0)
      self.label_2.setTextFormat(QtCore.Qt.AutoText)
      self.label_2.setAlignment(QtCore.Qt.AlignCenter)
      self.label_2.setOpenExternalLinks(False)
      self.label_2.setObjectName("label_2")
      self.checkBox = QtWidgets.QCheckBox(Form)
      self.checkBox.setGeometry(QtCore.QRect(110, 110, 91, 19))
      self.checkBox.setObjectName("checkBox")

      self.retranslateUi(Form)
      self.checkBox.clicked.connect(Form.start)
      QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
      _translate = QtCore.QCoreApplication.translate
      Form.setWindowTitle(_translate("Form", "Form"))
      self.label.setText(_translate("Form", "Q"))
      self.lineEdit.setText(_translate("Form", "哈哈"))
      self.lineEdit_2.setText(_translate("Form", "你好"))
      self.label_2.setText(_translate("Form", "W"))
      self.checkBox.setText(_translate("Form", "CheckBox"))


附上现阶段源码
源码下载

ba21 发表于 2022-8-29 19:55:10

有register 就有 unregister
在合适的地方取消注册的热键即可。
参考:
https://www.cnpython.com/pypi/system_hotke
页: [1]
查看完整版本: 如何中止已经注册的热键