Python Forum

Full Version: Terminate a process when hotkey is pressed
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello All!

I want to make a simple program running in the background which when detects a certain hotkey being pressed, it terminates the currently focused window. I found 2 packages to do this and I thought I was able to do so with a few lines of code but I always get the same error and I can't see why, I believe it has to do something with the hotkey method in line 10.

My code:
import pygetwindow as gw
import keyboard as kb

def closeWindow():
    focused = gw.getActiveWindow()
    title = focused.title()
    focused.close()
    print(title + " was closed")

kb.add_hotkey('alt+f4', closeWindow)
while True:
    continue
The errors I get all the time the momment i press the hotkey:
Error:
Exception in thread Thread-2: Traceback (most recent call last): File "C:\Users\Ricsi\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 932, in _bootstrap_inner self.run() File "C:\Users\Ricsi\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "C:\Users\Ricsi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\keyboard\_generic.py", line 58, in process if self.pre_process_event(event): File "C:\Users\Ricsi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\keyboard\__init__.py", line 218, in pre_process_event callback(event) File "C:\Users\Ricsi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\keyboard\__init__.py", line 649, in <lambda> handler = lambda e: (event_type == KEY_DOWN and e.event_type == KEY_UP and e.scan_code in _logically_pressed_keys) or (event_type == e.event_type and callback()) File "D:/LocalRepos/Python/untitled/main.py", line 6, in closeWindow title = focused.title() TypeError: 'str' object is not callable
Thank you for the help in advance!