Python Forum
Terminate a process when hotkey is pressed
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Terminate a process when hotkey is pressed
#1
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  The game should now run and terminate once the board is full, but still can’t identif rango 0 1,429 Jul-22-2021, 03:24 AM
Last Post: rango
  how to make a hotkey for text adventure game myn2018 2 1,918 Jan-06-2021, 10:39 PM
Last Post: myn2018
  Getting button pressed number Moris526 4 2,373 Dec-14-2020, 01:41 AM
Last Post: Moris526
  How many times was the button pressed in pyglet rama27 0 1,888 Oct-10-2020, 10:26 AM
Last Post: rama27
  run different functions each time the same button is pressed? User3000 6 3,247 Jul-31-2020, 11:11 PM
Last Post: User3000
  Hotkey to restart code? DannyB 1 2,696 May-20-2020, 02:52 AM
Last Post: michael1789
  PySide2 Hotkey works only once in Maya LalaGhost 0 1,755 May-19-2020, 05:05 PM
Last Post: LalaGhost
  Is there a way to not terminate a running thread in python Contra_Boy 3 1,932 May-05-2020, 09:38 PM
Last Post: SheeppOSU
  How to terminate the caller fuction through callee? 100k 2 2,101 Nov-27-2019, 06:49 PM
Last Post: jefsummers
  How to terminate a list of inputs with a set value? SOS Manning 2 2,141 Sep-19-2019, 01:54 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020