I would like to keep a script running in the background that allows me to launch a subprocess at the press of a hot key combo (i.e. Ctrl+F5). Is this possible with Python?
Python
Last Comment
yokai
8/22/2022 - Mon
RichieHindle
Here you go:
import os, sysfrom ctypes import *from ctypes.wintypes import *user32 = windll.user32WM_HOTKEY = 0x0312MOD_ALT = 0x0001MOD_CONTROL = 0x0002MOD_SHIFT = 0x0004class MSG(Structure): _fields_ = [('hwnd', c_int), ('message', c_uint), ('wParam', c_int), ('lParam', c_int), ('time', c_int), ('pt', POINT)]# Register a hotkey for Ctrl+Shift+Z.hotkeyId = 1if not user32.RegisterHotKey(None, hotkeyId, MOD_CONTROL | MOD_SHIFT, ord('Z')): sys.exit("Failed to register hotkey; maybe someone else registered it?")# Spin a message loop waiting for WM_HOTKEY.while 1: msg = MSG() while user32.GetMessageA(byref(msg), None, 0, 0) != 0: if msg.message == WM_HOTKEY and msg.wParam == hotkeyId: os.spawnl(os.P_NOWAIT, r"c:\windows\notepad.exe") user32.TranslateMessage(byref(msg)) user32.DispatchMessageA(byref(msg))
Open in new window