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.
Zeriun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.