0

I want to use Hotkey to move the mouse into some position, It can be set by the user personally, these hotkeys are F1-F4, when pressed F11 to enter these hotkeys detect, It will check the position_list whether empty, If it's empty then run loop four times to the user setting position, but when the Qmessagebox executes in the loop, my Window has died, I don't understand what wrong.

This is my code:

from PySide6.QtWidgets import *
from PySide6.QtGui import *
import pyautogui

import sys
import keyboard

class Window_controller(QWidget):
    Hotkey = True
    position_list = []
    def __init__(self):
        super().__init__()
        self.layout = QVBoxLayout(self)
        self.label = QLabel(self)
        self.label.setText("123")
        self.label.setFont(QFont("",20))
        self.layout.addWidget(self.label)
        self.hotkey_active()

    def hotkey(self):
        if self.Hotkey == True:
            self.label.setText("Activated")
            while True:
                if keyboard.is_pressed("F1"):
                    self.F1()
                elif keyboard.is_pressed("F2"):
                    self.F2()
                elif keyboard.is_pressed("F3"):
                    self.F3()
                elif keyboard.is_pressed("F4"):
                    self.F4()
                elif keyboard.is_pressed("F12"):
                    self.Hotkey == True
                    self.label.setText("")
                    break

    def F1(self):
        if len(self.position_list) != 0:
            pyautogui.moveTo(x=self.position_list[0],y=self.position_list[1])
        else:
            self.set_position()

    def F2(self):
        if len(self.position_list) != 0:
            pyautogui.moveTo(x=self.position_list[2],y=self.position_list[3])
        else:
            self.set_position()

    def F3(self):
        if len(self.position_list) != 0:
            pyautogui.moveTo(x=self.position_list[4],y=self.position_list[5])
        else:
            self.set_position()

    def F4(self):
        if len(self.position_list) != 0:
            pyautogui.moveTo(x=self.position_list[6],y=self.position_list[7])
        else:
            self.set_position()
    def set_position(self):
        for key in ["F1", "F2", "F3", "F4"]:
            QMessageBox.information(self, str(key), str("Move you'r mouse to setting position"))
            self.position_list.append(pyautogui.position().x)
            self.position_list.append(pyautogui.position().y)
        print(self.position_list)

    def hotkey_active(self):
        keyboard.add_hotkey("F11", self.hotkey)
if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = Window_controller()
    win.show()
    sys.exit(app.exec())

When program run into for-loop stuck

Dylan
  • 1
  • 1
  • You cannot use while loops in UI toolkits, because they block the UI event loop. Besides, if you need to trigger functions based on key presses while the program is active, just override `keyPressEvent()` or create QShortcuts. – musicamante Apr 23 '23 at 16:12
  • musicamante Thanks for your response. But I need to use the Keyboard module because when the UI_Window is minimized, it still can work, so I use The Keyboard module . – Dylan May 15 '23 at 03:53
  • Then you should have explained that aspect in the first place. Qt doesn't provide global shortcuts, so you must achieve that using specific platform modules or alternate solutions. – musicamante May 15 '23 at 04:01

1 Answers1

-1

I found the solution finally.

If you need to use Qmessagebox to pause the program in the while loop, you can use Tkinter's Messagebox instead.

Example:

def messagebox_info(self, title, content):
    root = Tk()
    root.wm_attributes("-topmost", 1)
    root.withdraw()
    messagebox.showinfo(title, content)
    root.destroy()
Dylan
  • 1
  • 1
  • That's a terrible suggestion, especially because using two UI toolkits together is discouraged and unsupported, since each of them require their own event loop. Besides, even assuming your solution was valid (and it isn't), it would still completely block the main Qt event loop, preventing any user interaction and proper UI updates. – musicamante May 15 '23 at 03:59