鱼C论坛

 找回密码
 立即注册
查看: 1292|回复: 1

[已解决]如何中止已经注册的热键

[复制链接]
发表于 2022-8-29 10:37:52 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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


主程序:
  1. import os
  2. from threading import Thread

  3. from pyqt5_plugins.examplebutton import QtWidgets
  4. from PyQt5.QtWidgets import QApplication,QMainWindow
  5. from PyQt5.Qt import QThread
  6. import sys
  7. from main_GUI import Ui_Form


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

  19.     def start_rw(self):
  20.         from pynput.keyboard import Key, Controller
  21.         from system_hotkey import SystemHotkey
  22.         hk = SystemHotkey()
  23.         if self.checkBox.isChecked() == True:
  24.             print("开启")
  25.             rekeyboard = Controller()
  26.             hk.register(('q',), callback=lambda x: rekeyboard.type(self.lineEdit.text()))
  27.             hk.register(('w',), callback=lambda x: rekeyboard.type(self.lineEdit_2.text()))
  28.         if self.checkBox.isChecked() == False:
  29.             pass



  30.     def start(self):
  31.         thread = Thread(target=self.start_rw)
  32.         thread.start()
  33. if __name__ == '__main__':
  34.     app = QApplication(sys.argv)
  35.     myWin = MyMainWindow()
  36.     myWin.show()
  37.     sys.exit(app.exec_())
复制代码



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

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


  8. from PyQt5 import QtCore, QtGui, QtWidgets


  9. class Ui_Form(object):
  10.     def setupUi(self, Form):
  11.         Form.setObjectName("Form")
  12.         Form.resize(242, 202)
  13.         self.label = QtWidgets.QLabel(Form)
  14.         self.label.setGeometry(QtCore.QRect(30, 30, 81, 31))
  15.         font = QtGui.QFont()
  16.         font.setFamily("微软雅黑 Light")
  17.         font.setPointSize(16)
  18.         self.label.setFont(font)
  19.         self.label.setMouseTracking(True)
  20.         self.label.setAutoFillBackground(False)
  21.         self.label.setFrameShape(QtWidgets.QFrame.Box)
  22.         self.label.setLineWidth(1)
  23.         self.label.setMidLineWidth(0)
  24.         self.label.setTextFormat(QtCore.Qt.AutoText)
  25.         self.label.setAlignment(QtCore.Qt.AlignCenter)
  26.         self.label.setOpenExternalLinks(False)
  27.         self.label.setObjectName("label")
  28.         self.lineEdit = QtWidgets.QLineEdit(Form)
  29.         self.lineEdit.setGeometry(QtCore.QRect(30, 60, 81, 31))
  30.         font = QtGui.QFont()
  31.         font.setFamily("微软雅黑 Light")
  32.         font.setPointSize(16)
  33.         self.lineEdit.setFont(font)
  34.         self.lineEdit.setObjectName("lineEdit")
  35.         self.lineEdit_2 = QtWidgets.QLineEdit(Form)
  36.         self.lineEdit_2.setGeometry(QtCore.QRect(120, 60, 81, 31))
  37.         font = QtGui.QFont()
  38.         font.setFamily("微软雅黑 Light")
  39.         font.setPointSize(16)
  40.         self.lineEdit_2.setFont(font)
  41.         self.lineEdit_2.setLayoutDirection(QtCore.Qt.LeftToRight)
  42.         self.lineEdit_2.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter)
  43.         self.lineEdit_2.setCursorMoveStyle(QtCore.Qt.LogicalMoveStyle)
  44.         self.lineEdit_2.setObjectName("lineEdit_2")
  45.         self.label_2 = QtWidgets.QLabel(Form)
  46.         self.label_2.setGeometry(QtCore.QRect(120, 30, 81, 31))
  47.         font = QtGui.QFont()
  48.         font.setFamily("微软雅黑 Light")
  49.         font.setPointSize(16)
  50.         self.label_2.setFont(font)
  51.         self.label_2.setMouseTracking(True)
  52.         self.label_2.setAutoFillBackground(False)
  53.         self.label_2.setFrameShape(QtWidgets.QFrame.Box)
  54.         self.label_2.setLineWidth(1)
  55.         self.label_2.setMidLineWidth(0)
  56.         self.label_2.setTextFormat(QtCore.Qt.AutoText)
  57.         self.label_2.setAlignment(QtCore.Qt.AlignCenter)
  58.         self.label_2.setOpenExternalLinks(False)
  59.         self.label_2.setObjectName("label_2")
  60.         self.checkBox = QtWidgets.QCheckBox(Form)
  61.         self.checkBox.setGeometry(QtCore.QRect(110, 110, 91, 19))
  62.         self.checkBox.setObjectName("checkBox")

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

  66.     def retranslateUi(self, Form):
  67.         _translate = QtCore.QCoreApplication.translate
  68.         Form.setWindowTitle(_translate("Form", "Form"))
  69.         self.label.setText(_translate("Form", "Q"))
  70.         self.lineEdit.setText(_translate("Form", "哈哈"))
  71.         self.lineEdit_2.setText(_translate("Form", "你好"))
  72.         self.label_2.setText(_translate("Form", "W"))
  73.         self.checkBox.setText(_translate("Form", "CheckBox"))
复制代码


附上现阶段源码
源码下载
最佳答案
2022-8-29 19:55:10
有register 就有 unregister
在合适的地方取消注册的热键即可。
参考:
https://www.cnpython.com/pypi/system_hotke
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-8-29 19:55:10 | 显示全部楼层    本楼为最佳答案   
有register 就有 unregister
在合适的地方取消注册的热键即可。
参考:
https://www.cnpython.com/pypi/system_hotke
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-30 22:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表