如何在Python中生成键盘事件?

简短的摘要:

我正在尝试创build一个将键盘事件发送到计算机的程序,出于所有目的,模拟事件应被视为键盘上的实际按键。

原帖:

我正在寻找一种方法来使用python生成键盘事件。

假设函数接收到一个必须模拟按下的键,如下所示:

keyboardevent('a') #lower case 'a' keyboardevent('B') #upper case 'B' keyboardevent('->') # right arrow key def keyboardevent(key): #code that simulated 'key' being pressed on keyboard 

以上是显而易见的例子,但是我正在寻找的是一个库,模块,或者任何我可以用来模拟键盘事件的东西。

注意 :这不同于发送字符到记事本,或input文本到字段等。 我想要python脚本来模拟一个实际的键盘事件,电脑会认为是真的有一个键盘事件。

额外注意:

我不想发送击键到活动窗口 – 我希望系统相信键盘的按键被按下,细微的差别,因为一些活动窗口不接受某些组合键,或者如果我想使用键盘快捷键对于通过我的脚本的后台进程,他们不需要通过活动窗口

到目前为止,我已经看了这些东西:

为最前面的应用程序生成键盘事件

如何通过Python生成键盘按键事件?

哪一个关于苹果,根本没有帮助。

和这个:

在Python上模拟键盘和鼠标最简单的方法是什么?

这似乎可能是我所需要的,但我找不到它或任何文档的库。

我也search了更多的地方,但还没有find解决办法。

它可以使用ctypes完成:

 import ctypes from ctypes import wintypes import time user32 = ctypes.WinDLL('user32', use_last_error=True) INPUT_MOUSE = 0 INPUT_KEYBOARD = 1 INPUT_HARDWARE = 2 KEYEVENTF_EXTENDEDKEY = 0x0001 KEYEVENTF_KEYUP = 0x0002 KEYEVENTF_UNICODE = 0x0004 KEYEVENTF_SCANCODE = 0x0008 MAPVK_VK_TO_VSC = 0 # msdn.microsoft.com/en-us/library/dd375731 VK_TAB = 0x09 VK_MENU = 0x12 # C struct definitions wintypes.ULONG_PTR = wintypes.WPARAM class MOUSEINPUT(ctypes.Structure): _fields_ = (("dx", wintypes.LONG), ("dy", wintypes.LONG), ("mouseData", wintypes.DWORD), ("dwFlags", wintypes.DWORD), ("time", wintypes.DWORD), ("dwExtraInfo", wintypes.ULONG_PTR)) class KEYBDINPUT(ctypes.Structure): _fields_ = (("wVk", wintypes.WORD), ("wScan", wintypes.WORD), ("dwFlags", wintypes.DWORD), ("time", wintypes.DWORD), ("dwExtraInfo", wintypes.ULONG_PTR)) def __init__(self, *args, **kwds): super(KEYBDINPUT, self).__init__(*args, **kwds) # some programs use the scan code even if KEYEVENTF_SCANCODE # isn't set in dwFflags, so attempt to map the correct code. if not self.dwFlags & KEYEVENTF_UNICODE: self.wScan = user32.MapVirtualKeyExW(self.wVk, MAPVK_VK_TO_VSC, 0) class HARDWAREINPUT(ctypes.Structure): _fields_ = (("uMsg", wintypes.DWORD), ("wParamL", wintypes.WORD), ("wParamH", wintypes.WORD)) class INPUT(ctypes.Structure): class _INPUT(ctypes.Union): _fields_ = (("ki", KEYBDINPUT), ("mi", MOUSEINPUT), ("hi", HARDWAREINPUT)) _anonymous_ = ("_input",) _fields_ = (("type", wintypes.DWORD), ("_input", _INPUT)) LPINPUT = ctypes.POINTER(INPUT) def _check_count(result, func, args): if result == 0: raise ctypes.WinError(ctypes.get_last_error()) return args user32.SendInput.errcheck = _check_count user32.SendInput.argtypes = (wintypes.UINT, # nInputs LPINPUT, # pInputs ctypes.c_int) # cbSize # Functions def PressKey(hexKeyCode): x = INPUT(type=INPUT_KEYBOARD, ki=KEYBDINPUT(wVk=hexKeyCode)) user32.SendInput(1, ctypes.byref(x), ctypes.sizeof(x)) def ReleaseKey(hexKeyCode): x = INPUT(type=INPUT_KEYBOARD, ki=KEYBDINPUT(wVk=hexKeyCode, dwFlags=KEYEVENTF_KEYUP)) user32.SendInput(1, ctypes.byref(x), ctypes.sizeof(x)) def AltTab(): """Press Alt+Tab and hold Alt key for 2 seconds in order to see the overlay. """ PressKey(VK_MENU) # Alt PressKey(VK_TAB) # Tab ReleaseKey(VK_TAB) # Tab~ time.sleep(2) ReleaseKey(VK_MENU) # Alt~ if __name__ == "__main__": AltTab() 

hexKeyCode是由Windows API定义的虚拟键盘映射。 代码列表可在MSDN上获得: 虚拟键码(Windows)

我知道这是一个老问题,但它仍然是谷歌的顶部。

对于python3和python2你都可以使用pyautoguipip install pyautogui

 from pyautogui import press, typewrite, hotkey press('a') typewrite('quick brown fox') hotkey('ctrl', 'w') 

user648852的想法至less对我来说是非常适合OS X的,下面是代码:

 #!/usr/bin/env python import time from Quartz.CoreGraphics import CGEventCreateKeyboardEvent from Quartz.CoreGraphics import CGEventPost # Python releases things automatically, using CFRelease will result in a scary error #from Quartz.CoreGraphics import CFRelease from Quartz.CoreGraphics import kCGHIDEventTap # From http://stackoverflow.com/questions/281133/controlling-the-mouse-from-python-in-os-x # and from https://developer.apple.com/library/mac/documentation/Carbon/Reference/QuartzEventServicesRef/index.html#//apple_ref/c/func/CGEventCreateKeyboardEvent def KeyDown(k): keyCode, shiftKey = toKeyCode(k) time.sleep(0.0001) if shiftKey: CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, 0x38, True)) time.sleep(0.0001) CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, keyCode, True)) time.sleep(0.0001) if shiftKey: CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, 0x38, False)) time.sleep(0.0001) def KeyUp(k): keyCode, shiftKey = toKeyCode(k) time.sleep(0.0001) CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, keyCode, False)) time.sleep(0.0001) def KeyPress(k): keyCode, shiftKey = toKeyCode(k) time.sleep(0.0001) if shiftKey: CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, 0x38, True)) time.sleep(0.0001) CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, keyCode, True)) time.sleep(0.0001) CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, keyCode, False)) time.sleep(0.0001) if shiftKey: CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, 0x38, False)) time.sleep(0.0001) # From http://stackoverflow.com/questions/3202629/where-can-i-find-a-list-of-mac-virtual-key-codes def toKeyCode(c): shiftKey = False # Letter if c.isalpha(): if not c.islower(): shiftKey = True c = c.lower() if c in shiftChars: shiftKey = True c = shiftChars[c] if c in keyCodeMap: keyCode = keyCodeMap[c] else: keyCode = ord(c) return keyCode, shiftKey shiftChars = { '~': '`', '!': '1', '@': '2', '#': '3', '$': '4', '%': '5', '^': '6', '&': '7', '*': '8', '(': '9', ')': '0', '_': '-', '+': '=', '{': '[', '}': ']', '|': '\\', ':': ';', '"': '\'', '<': ',', '>': '.', '?': '/' } keyCodeMap = { 'a' : 0x00, 's' : 0x01, 'd' : 0x02, 'f' : 0x03, 'h' : 0x04, 'g' : 0x05, 'z' : 0x06, 'x' : 0x07, 'c' : 0x08, 'v' : 0x09, 'b' : 0x0B, 'q' : 0x0C, 'w' : 0x0D, 'e' : 0x0E, 'r' : 0x0F, 'y' : 0x10, 't' : 0x11, '1' : 0x12, '2' : 0x13, '3' : 0x14, '4' : 0x15, '6' : 0x16, '5' : 0x17, '=' : 0x18, '9' : 0x19, '7' : 0x1A, '-' : 0x1B, '8' : 0x1C, '0' : 0x1D, ']' : 0x1E, 'o' : 0x1F, 'u' : 0x20, '[' : 0x21, 'i' : 0x22, 'p' : 0x23, 'l' : 0x25, 'j' : 0x26, '\'' : 0x27, 'k' : 0x28, ';' : 0x29, '\\' : 0x2A, ',' : 0x2B, '/' : 0x2C, 'n' : 0x2D, 'm' : 0x2E, '.' : 0x2F, '`' : 0x32, 'k.' : 0x41, 'k*' : 0x43, 'k+' : 0x45, 'kclear' : 0x47, 'k/' : 0x4B, 'k\n' : 0x4C, 'k-' : 0x4E, 'k=' : 0x51, 'k0' : 0x52, 'k1' : 0x53, 'k2' : 0x54, 'k3' : 0x55, 'k4' : 0x56, 'k5' : 0x57, 'k6' : 0x58, 'k7' : 0x59, 'k8' : 0x5B, 'k9' : 0x5C, # keycodes for keys that are independent of keyboard layout '\n' : 0x24, '\t' : 0x30, ' ' : 0x31, 'del' : 0x33, 'delete' : 0x33, 'esc' : 0x35, 'escape' : 0x35, 'cmd' : 0x37, 'command' : 0x37, 'shift' : 0x38, 'caps lock' : 0x39, 'option' : 0x3A, 'ctrl' : 0x3B, 'control' : 0x3B, 'right shift' : 0x3C, 'rshift' : 0x3C, 'right option' : 0x3D, 'roption' : 0x3D, 'right control' : 0x3E, 'rcontrol' : 0x3E, 'fun' : 0x3F, 'function' : 0x3F, 'f17' : 0x40, 'volume up' : 0x48, 'volume down' : 0x49, 'mute' : 0x4A, 'f18' : 0x4F, 'f19' : 0x50, 'f20' : 0x5A, 'f5' : 0x60, 'f6' : 0x61, 'f7' : 0x62, 'f3' : 0x63, 'f8' : 0x64, 'f9' : 0x65, 'f11' : 0x67, 'f13' : 0x69, 'f16' : 0x6A, 'f14' : 0x6B, 'f10' : 0x6D, 'f12' : 0x6F, 'f15' : 0x71, 'help' : 0x72, 'home' : 0x73, 'pgup' : 0x74, 'page up' : 0x74, 'forward delete' : 0x75, 'f4' : 0x76, 'end' : 0x77, 'f2' : 0x78, 'page down' : 0x79, 'pgdn' : 0x79, 'f1' : 0x7A, 'left' : 0x7B, 'right' : 0x7C, 'down' : 0x7D, 'up' : 0x7E } 

仅Windows:您可以使用Ironpython或允许cPython在Windows上访问.NET框架的库 。 然后使用.NET的sendkeys类或更一般的send来模拟按键。

仅适用于OS X:使用PyObjC然后使用CGEventCreateKeyboardEvent调用。

完全公开:我只在Python X上做过这个,但是我使用了.NET的sendkeys(用C#),这很好。

每个平台都将有不同的方法来生成键盘事件。 这是因为他们都需要使用系统库(和系统扩展)。 对于跨平台解决scheme,您需要采取每种解决scheme,然后将其包装到平台检查中,以执行正确的方法。

对于Windows,您可能可以使用pywin32扩展。 win32api.keybd_event

win32api.keybd_event

keybd_event(bVk,bScan,dwFlags,dwExtraInfo)

模拟键盘事件

参数

bVk:BYTE – 虚拟键码
bScan:BYTE – 硬件扫描码
dwFlags = 0:DWORD – 指定各种function选项的标志
dwExtraInfo = 0:DWORD – 与按键相关的其他数据

您将需要调查pywin32如何正确使用它,因为我从来没有使用它。

MACOS

下面是@Phylliida答案的更完整版本,其代码示例如下:

 #!/usr/bin/python # Script simulating keyboard events in macOS. # See: https://stackoverflow.com/q/13564851/55075 import sys import time from Quartz.CoreGraphics import CGEventCreateKeyboardEvent from Quartz.CoreGraphics import CGEventPost from Quartz.CoreGraphics import kCGHIDEventTap #from Quartz.CoreGraphics import CFRelease # Python releases things automatically. class Keyboard(): shiftChars = { '~': '`', '!': '1', '@': '2', '#': '3', '$': '4', '%': '5', '^': '6', '&': '7', '*': '8', '(': '9', ')': '0', '_': '-', '+': '=', '{': '[', '}': ']', '|': '\\', ':': ';', '"': '\'', '<': ',', '>': '.', '?': '/' } keyCodeMap = { 'a' : 0x00, 's' : 0x01, 'd' : 0x02, 'f' : 0x03, 'h' : 0x04, 'g' : 0x05, 'z' : 0x06, 'x' : 0x07, 'c' : 0x08, 'v' : 0x09, 'b' : 0x0B, 'q' : 0x0C, 'w' : 0x0D, 'e' : 0x0E, 'r' : 0x0F, 'y' : 0x10, 't' : 0x11, '1' : 0x12, '2' : 0x13, '3' : 0x14, '4' : 0x15, '6' : 0x16, '5' : 0x17, '=' : 0x18, '9' : 0x19, '7' : 0x1A, '-' : 0x1B, '8' : 0x1C, '0' : 0x1D, ']' : 0x1E, 'o' : 0x1F, 'u' : 0x20, '[' : 0x21, 'i' : 0x22, 'p' : 0x23, 'l' : 0x25, 'j' : 0x26, '\'' : 0x27, 'k' : 0x28, ';' : 0x29, '\\' : 0x2A, ',' : 0x2B, '/' : 0x2C, 'n' : 0x2D, 'm' : 0x2E, '.' : 0x2F, '`' : 0x32, 'k.' : 0x41, 'k*' : 0x43, 'k+' : 0x45, 'kclear' : 0x47, 'k/' : 0x4B, 'k\n' : 0x4C, 'k-' : 0x4E, 'k=' : 0x51, 'k0' : 0x52, 'k1' : 0x53, 'k2' : 0x54, 'k3' : 0x55, 'k4' : 0x56, 'k5' : 0x57, 'k6' : 0x58, 'k7' : 0x59, 'k8' : 0x5B, 'k9' : 0x5C, # keycodes for keys that are independent of keyboard layout '\n' : 0x24, '\t' : 0x30, ' ' : 0x31, 'del' : 0x33, 'delete' : 0x33, 'esc' : 0x35, 'escape' : 0x35, 'cmd' : 0x37, 'command' : 0x37, 'shift' : 0x38, 'caps lock' : 0x39, 'option' : 0x3A, 'ctrl' : 0x3B, 'control' : 0x3B, 'right shift' : 0x3C, 'rshift' : 0x3C, 'right option' : 0x3D, 'roption' : 0x3D, 'right control' : 0x3E, 'rcontrol' : 0x3E, 'fun' : 0x3F, 'function' : 0x3F, 'f17' : 0x40, 'volume up' : 0x48, 'volume down' : 0x49, 'mute' : 0x4A, 'f18' : 0x4F, 'f19' : 0x50, 'f20' : 0x5A, 'f5' : 0x60, 'f6' : 0x61, 'f7' : 0x62, 'f3' : 0x63, 'f8' : 0x64, 'f9' : 0x65, 'f11' : 0x67, 'f13' : 0x69, 'f16' : 0x6A, 'f14' : 0x6B, 'f10' : 0x6D, 'f12' : 0x6F, 'f15' : 0x71, 'help' : 0x72, 'home' : 0x73, 'pgup' : 0x74, 'page up' : 0x74, 'forward delete' : 0x75, 'f4' : 0x76, 'end' : 0x77, 'f2' : 0x78, 'page down' : 0x79, 'pgdn' : 0x79, 'f1' : 0x7A, 'left' : 0x7B, 'right' : 0x7C, 'down' : 0x7D, 'up' : 0x7E } # See: https://stackoverflow.com/q/3202629/55075 def toKeyCode(self, c): shiftKey = False # Letter if c.isalpha(): if not c.islower(): shiftKey = True c = c.lower() if c in Keyboard.shiftChars: shiftKey = True c = Keyboard.shiftChars[c] if c in Keyboard.keyCodeMap: keyCode = Keyboard.keyCodeMap[c] else: keyCode = ord(c) return keyCode, shiftKey def KeyDown(self, k): keyCode, shiftKey = self.toKeyCode(k) time.sleep(0.0001) if shiftKey: CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, 0x38, True)) time.sleep(0.0001) CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, keyCode, True)) time.sleep(0.0001) if shiftKey: CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, 0x38, False)) time.sleep(0.0001) def KeyUp(self, k): keyCode, shiftKey = self.toKeyCode(k) time.sleep(0.0001) CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, keyCode, False)) time.sleep(0.0001) def KeyPress(self, k): keyCode, shiftKey = self.toKeyCode(k) time.sleep(0.0001) if shiftKey: CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, 0x38, True)) time.sleep(0.0001) CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, keyCode, True)) time.sleep(0.0001) CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, keyCode, False)) time.sleep(0.0001) if shiftKey: CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, 0x38, False)) time.sleep(0.0001) def Type(self, text): for key in text: self.KeyDown(key) self.KeyUp(key) 

这里是使用上面的类的演示代码:

 # DEMO if __name__ == '__main__': keyboard = Keyboard() if sys.platform == "darwin": keyboard.Type('Hello World!') elif sys.platform == "win32": print("Error: Platform not supported!") 

这将模拟inputHello World! 当前窗口上的文本。

您可以将上面的代码作为shell脚本运行。 检查指向keyboard.py文件的链接。