I was trying to create a simple program in python that can detect if a specific key on keyboard is pressed. As the response, I want my code to pressed a specific key on keyboard. I tested this code by pressing the W, A ,S ,D keys on a blank notepad document. I was expecting the result to be like wr, ar, sr, and dr. But the actual result that I got was rw, ra, rs ,rd. Can someone help me on this please?
import pythoncom, pyHook
import win32api, win32con
import time
import sys
import win32com.client
def OnKeyboardEvent(event):
if(event.Key=='A' or event.Key=='W' or event.Key=='D' or event.Key=='S'):
#key down for the R button
win32api.keybd_event(0x52,0,2,0)
#key up for the R button
win32api.keybd_event(0x52,0,0,0)
# create a hook manager
hm = pyHook.HookManager()
# watch for all mouse events
hm.KeyDown = OnKeyboardEvent
# set the hook
hm.HookKeyboard()
# wait forever
pythoncom.PumpMessages()