1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| import time import win32api import win32con import win32gui class Virtual_Keyboard(): def __init__(self,hwnd): self.hwnd = hwnd self.key_map = { "0": 48, "1": 49, "2": 50, "3": 51, "4": 52, "5": 53, "6": 54, "7": 55, "8": 56, "9": 57, 'F1': 112, 'F2': 113, 'F3': 114, 'F4': 115, 'F5': 116, 'F6': 117, 'F7': 118, 'F8': 119, 'F9': 120, 'F10': 121, 'F11': 122, 'F12': 123, 'F13': 124, 'F14': 125, 'F15': 126, 'F16': 127, "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, 'BACKSPACE': 8, 'TAB': 9, 'TABLE': 9, 'CLEAR': 12, 'ENTER': 13, 'SHIFT': 16, 'CTRL': 17, 'CONTROL': 17, 'ALT': 18, 'ALTER': 18, 'PAUSE': 19, 'BREAK': 19, 'CAPSLK': 20, 'CAPSLOCK': 20, 'ESC': 27, 'SPACE': 32, 'SPACEBAR': 32, 'PGUP': 33, 'PAGEUP': 33, 'PGDN': 34, 'PAGEDOWN': 34, 'END': 35, 'HOME': 36, 'LEFT': 37, 'UP': 38, 'RIGHT': 39, 'DOWN': 40, 'SELECT': 41, 'PRTSC': 42, 'printSCREEN': 42, 'SYSRQ': 42, 'SYSTEMREQUEST': 42, 'EXECUTE': 43, 'SNAPSHOT': 44, 'INSERT': 45, 'DELETE': 46, 'HELP': 47, 'WIN': 91, 'WINDOWS': 91, 'NMLK': 144, 'NUMLK': 144, 'NUMLOCK': 144, 'SCRLK': 145, '[': 219, ']': 221, '+': 107, '-': 109}
def pressKey(self, key: str, interval=0.1): self.keyDown(key) time.sleep(interval) self.keyUp(key)
def pressKeys(self, keys, interval=0.1): for i in range (0,len(keys),1): self.keyDown(keys[i]) time.sleep(interval) for i in range (0,len(keys),1): self.keyUp(keys[i]) def keyDown(self, key: str): key_code = self.key_map[key.upper()] win32api.SendMessage(self.hwnd, win32con.WM_KEYDOWN, key_code, 0) def keyUp(self, key: str): key_code = self.key_map[key.upper()] win32api.SendMessage(self.hwnd, win32con.WM_KEYUP, key_code, 0)
def inputText(self, text: str): print('input text: ' + text) for ch in text: win32gui.SendMessage(self.hwnd, win32con.WM_CHAR, ord(ch), 0) time.sleep(0.05)
def pressKey_2(self, key: str, interval=0.1): self.keyDown_2(key) time.sleep(interval) self.keyUp_2(key)
def pressKeys_2(self, keys, interval=0.1): for i in range (0,len(keys),1): self.keyDown_2(keys[i]) time.sleep(interval) for i in range (0,len(keys),1): self.keyUp_2(keys[i])
def keyDown_2(self, key: str): print('keyDown_2: ' + str(key)) key_code = self.key_map[key.upper()] win32api.PostMessage(self.hwnd, win32con.WM_KEYDOWN, key_code, 0) def keyUp_2(self, key: str): print('keyUp_2: ' + str(key)) key_code = self.key_map[key.upper()] win32api.PostMessage(self.hwnd, win32con.WM_KEYUP, key_code, 0)
def inputText_2(self, text: str): print('input_2 text: ' + text) for ch in text: win32gui.PostMessage(self.hwnd, win32con.WM_CHAR, ord(ch), 0) time.sleep(0.05)
|