How to set a global hotkey without pyautogui getting in the way

So essentially I have a global hotkey registered in my Python program using the ‘keyboard’ library. It listens for the key event ‘ctrl+shift+x’ to restart the application. I have one script within my program that uses openCV for object detection in a game and during one of the interactions, it has to hold down the shift key using pyautogui to perform the action. During that, if I try to press my global hotkey, it won’t register unless I spam it. It’s most likely because of pyautogui holding down shift. I’ve tried different key combinations too that don’t utilize shift, but no progress there.

So I have this code in my primary interface:

class PrimaryInterface(QMainWindow):
    requestRestart = Signal()

    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle("-----")
        self.resize(450, 300)
        self.networkManager = QNetworkAccessManager(self)
        self.setupUI()
        self.checkVersionUpdate()

        self.listener_process = multiprocessing.Process(target=self.registerGlobalHotkey())
        self.listener_process.start()
        self.requestRestart.connect(self.restartApplication)

        def registerGlobalHotkey(self):
        keyboard.add_hotkey('ctrl+shift+x', self.requestRestart.emit)

    def restartApplication(self):
        # Ensures all operations are stopped before restart
        self.cleanupBeforeExit()
        QApplication.quit()
        QProcess.startDetached(sys.executable, sys.argv)

    def cleanupBeforeExit(self):
        # Cleanup code
        keyboard.unhook_all_hotkeys()

        self.networkManager.clearAccessCache() 

and this is part of my script that performs the automation with pyautogui:

   def dropItems(self, gameplay_screen, last_slot_image):
        if last_slot_image is None:
            print("No last slot image provided.")
            return
        
        # Define the entire inventory area
        inventory_x, inventory_y = 545, 203  # Top-left corner of the inventory
        inventory_width, inventory_height = 197, 264   # Size of the inventory area

        inventory_img = gameplay_screen[inventory_y:inventory_y+inventory_height, inventory_x:inventory_x+inventory_width]

        if last_slot_image.ndim == 3:
            last_slot_image = cv2.cvtColor(last_slot_image, cv2.COLOR_BGR2GRAY)
        if inventory_img.ndim == 3:
            inventory_img = cv2.cvtColor(inventory_img, cv2.COLOR_BGR2GRAY)

        # Perform template matching over the entire inventory area
        res = cv2.matchTemplate(inventory_img, last_slot_image, cv2.TM_CCOEFF_NORMED)
        threshold = 0.7 
        loc = np.where(res >= threshold)

        pyautogui.keyDown('shift')
        first_slot_processed = False

        try:
            for pt in zip(*loc[::-1]):  # Switch x and y positions
                game_location = (pt[0] + inventory_x + last_slot_image.shape[1]//2, pt[1] + inventory_y + last_slot_image.shape[0]//2)
                if not first_slot_processed:
                    self.gwm.smooth_move_and_random_click(game_location, radius=8, smooth_time_min=0.9, smooth_time_max=1.8)
                    first_slot_processed = True
                else:
                    self.gwm.smooth_move_and_random_click(game_location, radius=8, smooth_time_min=0.1, smooth_time_max=0.3)
                time.sleep(random.uniform(.02, .05))
        finally:
            pyautogui.keyUp('shift')

this function is part of my primary while loop in the same script. While it’s performing that function, I can’t smoothly press my global hotkey to exit out and restart my application.

New contributor

Zeriun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật