Simulating a key press event in Python 2.7
I wrote this code more than 1 year ago so it is not perfect but it works:
from win32api import keybd_event
import time
import random
Combs = {
'A': [
'SHIFT',
'a'],
'B': [
'SHIFT',
'b'],
'C': [
'SHIFT',
'c'],
'D': [
'SHIFT',
'd'],
'E': [
'SHIFT',
'e'],
'F': [
'SHIFT',
'f'],
'G': [
'SHIFT',
'g'],
'H': [
'SHIFT',
'h'],
'I': [
'SHIFT',
'i'],
'J': [
'SHIFT',
'j'],
'K': [
'SHIFT',
'k'],
'L': [
'SHIFT',
'l'],
'M': [
'SHIFT',
'm'],
'N': [
'SHIFT',
'n'],
'O': [
'SHIFT',
'o'],
'P': [
'SHIFT',
'p'],
'R': [
'SHIFT',
'r'],
'S': [
'SHIFT',
's'],
'T': [
'SHIFT',
't'],
'U': [
'SHIFT',
'u'],
'W': [
'SHIFT',
'w'],
'X': [
'SHIFT',
'x'],
'Y': [
'SHIFT',
'y'],
'Z': [
'SHIFT',
'z'],
'V': [
'SHIFT',
'v'],
'Q': [
'SHIFT',
'q'],
'?': [
'SHIFT',
'/'],
'>': [
'SHIFT',
'.'],
'<': [
'SHIFT',
','],
'"': [
'SHIFT',
"'"],
':': [
'SHIFT',
';'],
'|': [
'SHIFT',
'\\'],
'}': [
'SHIFT',
']'],
'{': [
'SHIFT',
'['],
'+': [
'SHIFT',
'='],
'_': [
'SHIFT',
'-'],
'!': [
'SHIFT',
'1'],
'@': [
'SHIFT',
'2'],
'#': [
'SHIFT',
'3'],
'$': [
'SHIFT',
'4'],
'%': [
'SHIFT',
'5'],
'^': [
'SHIFT',
'6'],
'&': [
'SHIFT',
'7'],
'*': [
'SHIFT',
'8'],
'(': [
'SHIFT',
'9'],
')': [
'SHIFT',
'0'] }
Base = {
'0': 48,
'1': 49,
'2': 50,
'3': 51,
'4': 52,
'5': 53,
'6': 54,
'7': 55,
'8': 56,
'9': 57,
'a': 65,
'b': 66,
'c': 67,
'd': 68,
'e': 69,
'f': 70,
'g': 71,
'h': 72,
'i': 73,
'j': 74,
'k': 75,
'l': 76,
'm': 77,
'n': 78,
'o': 79,
'p': 80,
'q': 81,
'r': 82,
's': 83,
't': 84,
'u': 85,
'v': 86,
'w': 87,
'x': 88,
'y': 89,
'z': 90,
'.': 190,
'-': 189,
',': 188,
'=': 187,
'/': 191,
';': 186,
'[': 219,
']': 221,
'\\': 220,
"'": 222,
'ALT': 18,
'TAB': 9,
'CAPSLOCK': 20,
'ENTER': 13,
'BS': 8,
'CTRL': 17,
'ESC': 27,
' ': 32,
'END': 35,
'DOWN': 40,
'LEFT': 37,
'UP': 38,
'RIGHT': 39,
'SELECT': 41,
'PRINTSCR': 44,
'INS': 45,
'DEL': 46,
'LWIN': 91,
'RWIN': 92,
'LSHIFT': 160,
'SHIFT': 161,
'LCTRL': 162,
'RCTRL': 163,
'VOLUP': 175,
'DOLDOWN': 174,
'NUMLOCK': 144,
'SCROLL': 145 }
def KeyUp(Key):
keybd_event(Key, 0, 2, 0)
def KeyDown(Key):
keybd_event(Key, 0, 1, 0)
def Press(Key, speed=1):
rest_time = 0.05/speed
if Key in Base:
Key = Base[Key]
KeyDown(Key)
time.sleep(rest_time)
KeyUp(Key)
return True
if Key in Combs:
KeyDown(Base[Combs[Key][0]])
time.sleep(rest_time)
KeyDown(Base[Combs[Key][1]])
time.sleep(rest_time)
KeyUp(Base[Combs[Key][1]])
time.sleep(rest_time)
KeyUp(Base[Combs[Key][0]])
return True
return False
def Write(Str, speed = 1):
for s in Str:
Press(s, speed)
time.sleep((0.1 + random.random()/10.0) / float(speed))
Example:
>>> Write('Hello, World!', speed=3)
Hello, World!
>>> Press('ENTER')
If you want to implement some more keys then you can find their codes here. And just add these keys to the Base dictionary.
Piotr Dabkowski
Check out my JavaScript to Python translator: https://github.com/PiotrDabkowski/Js2Py Full implementation of ECMA 5.1 in 100% pure Python.
Updated on July 09, 2022Comments
-
Piotr Dabkowski almost 2 years
What I want to do is to press any keyboard key from the Python script level on Windows. I have tried SendKeys but it works only on python 2.6. Other methods that I have tried including
import win32com.client win32com.client.Dispatch("WScript.Shell").SendKeys('String to be typed')
allow only to type strings from the script level but dont allow to press ENTER and other 'special' keys.
Therefore my question is: How can I simulate any keyboard key press event from python script level including 'special' ones like ENTER, CTRL, ESC etc.
It would be also very helpful if it is possible to hold a key pressed down for any specified time and press a combination of keys like Alt+F4.
-
Amit Gold about 8 yearsQuestion: what about function keys? (f1, f2,f3...)
-
Piotr Dabkowski about 8 yearsJust add these keys to Base dictionary. For example F1 is 0x70. See the link to the remaining keys in my updated answers.