External Exam Download Resources Web Applications Games Recycle Bin

Executing on Keydown key "A"

Using the detection of keydown "A" from the previous example, to execute a process (in this case, opening Windows Notepad if the user presses the "A" key):

import ctypes #access Windows C functions
kernel32 = ctypes.windll.kernel32 #kernel level
user32 = ctypes.windll.user32  #user level

user32.ShowWindow(kernel32.GetConsoleWindow(), True) #showing console.. for now

key_pressed = user32.GetAsyncKeyState

import subprocess

while True:
  if( key_pressed(65) & 1 ): #if A pressed:
    subprocess.run(['C:\\Windows\\Notepad.exe'])