Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple unicode keyboard automation for windows
# coding: utf-8
import ctypes
import time
import sys
LONG = ctypes.c_long
DWORD = ctypes.c_ulong
ULONG_PTR = ctypes.POINTER(DWORD)
WORD = ctypes.c_ushort
INPUT_MOUSE = 0
INPUT_KEYBOARD = 1
INPUT_HARDWARE = 2
KEYEVENTF_EXTENDEDKEY = 0x0001
KEYEVENTF_KEYUP = 0x0002
KEYEVENTF_SCANCODE = 0x0008
KEYEVENTF_UNICODE = 0x0004
VK_RETURN = 0x0D # ENTER key
UNICODE_TO_VK = {
'\r': VK_RETURN,
'\n': VK_RETURN,
}
class MOUSEINPUT(ctypes.Structure):
_fields_ = (('dx', LONG),
('dy', LONG),
('mouseData', DWORD),
('dwFlags', DWORD),
('time', DWORD),
('dwExtraInfo', ULONG_PTR))
class KEYBDINPUT(ctypes.Structure):
_fields_ = (('wVk', WORD),
('wScan', WORD),
('dwFlags', DWORD),
('time', DWORD),
('dwExtraInfo', ULONG_PTR))
class HARDWAREINPUT(ctypes.Structure):
_fields_ = (('uMsg', DWORD),
('wParamL', WORD),
('wParamH', WORD))
class _INPUTunion(ctypes.Union):
_fields_ = (('mi', MOUSEINPUT),
('ki', KEYBDINPUT),
('hi', HARDWAREINPUT))
class INPUT(ctypes.Structure):
_fields_ = (('type', DWORD),
('union', _INPUTunion))
def send_input(*inputs):
nInputs = len(inputs)
LPINPUT = INPUT * nInputs
pInputs = LPINPUT(*inputs)
cbSize = ctypes.c_int(ctypes.sizeof(INPUT))
return ctypes.windll.user32.SendInput(nInputs, pInputs, cbSize)
def input_structure(structure):
if isinstance(structure, MOUSEINPUT):
return INPUT(INPUT_MOUSE, _INPUTunion(mi=structure))
if isinstance(structure, KEYBDINPUT):
return INPUT(INPUT_KEYBOARD, _INPUTunion(ki=structure))
if isinstance(structure, HARDWAREINPUT):
return INPUT(INPUT_HARDWARE, _INPUTunion(hi=structure))
raise TypeError('Cannot create INPUT structure!')
def keyboard_input_unicode(code, flags=0):
flags = KEYEVENTF_UNICODE | flags
return KEYBDINPUT(0, code, flags, 0, None)
def keyboard_input_vk(code, flags=0):
return KEYBDINPUT(code, code, flags, 0, None)
def keyboard_event_unicode(code, flags=0):
return input_structure(keyboard_input_unicode(code))
def keyboard_event_vk(code, flags=0):
return input_structure(keyboard_input_vk(code, flags))
def press_vk(code):
send_input(keyboard_event_vk(code, flags=0))
send_input(keyboard_event_vk(code, KEYEVENTF_KEYUP))
def press(character):
if character in UNICODE_TO_VK:
return press_vk(UNICODE_TO_VK[character])
code = ord(character)
send_input(keyboard_event_unicode(code))
send_input(keyboard_event_unicode(code, KEYEVENTF_KEYUP))
def main():
time.sleep(3)
for char in u'O\nשש2E6UXoשש2E^uXh#:SHn&HQ':
press(char)
time.sleep(0.5)
if __name__ == '__main__':
main()
@arpruss

This comment has been minimized.

Copy link

@arpruss arpruss commented Jun 15, 2017

What license is this code under? Ideally, I'd like to use it in an MIT licensed project, but I can go for another open source license.

@samuel-gamelin

This comment has been minimized.

Copy link

@samuel-gamelin samuel-gamelin commented Apr 21, 2020

@ubershmekel Excellent work. Same question, wondering what license this code was under.

@ubershmekel

This comment has been minimized.

Copy link
Owner Author

@ubershmekel ubershmekel commented Apr 21, 2020

I think I modified a stackoverflow answer from here: https://stackoverflow.com/questions/11906925/python-simulate-keydown but I can't really remember. If it's up to me then take this as public domain or MIT. Whatever works for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.