1

I am trying to write a function that will receive an ASCII character and will generate a keyboard event based on it. KeyboardEvent is the function I'm trying to implement:

from msvcrt import getch
key= ord(getch()) #getting the ASCII character for a key that was pressed
KeyboardEvent(key)
def KeyboardEvent(key):
    #code that simulates the key (that the ASCII character represents) being pressed on keyboard

I've tried looking for answers and the closest I've gotten to is this question Generate keyboard events However, the code that the answer suggested is not based on ASCII characters and doesn't meet my needs. Any help would be appreciated. Thank you in advance

Community
  • 1
  • 1
Noga
  • 121
  • 2
  • 8

1 Answers1

0

The answer you already found should work fine for ASCII, it does for me. In that code there are functions like:

def PressKey(hexKeyCode):
x = INPUT(type=INPUT_KEYBOARD,
          ki=KEYBDINPUT(wVk=hexKeyCode))
user32.SendInput(1, ctypes.byref(x), ctypes.sizeof(x))

and for regular ASCII keyboard characters you are safe to pass the value from your code

key=ord(getch())

straight to those functions, as the hexKeyCode parameter.

AS Mackay
  • 2,831
  • 9
  • 19
  • 25