在Python 2.7模拟的关键preSS事件 [英] Simulating a key press event in Python 2.7
问题描述
我想要做的是preSS从Windows上的Python脚本级别的任何键盘键。我已经试过的SendKeys,但它仅适用于Python 2.6中。我曾尝试包括其他方法
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')
只允许从脚本级别类型字符串,但不容许preSS ENTER等特殊键。
allow only to type strings from the script level but dont allow to press ENTER and other 'special' keys.
所以我的问题是:我怎么能模拟从Python脚本级别的任何键盘键preSS活动,其中包括像'特别'的人ENTER键,Ctrl键,ESC等。
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.
如果能够保持pssed下来PSS像Alt + F4键键的组合的任何指定的时间和$ P $密钥$ P $这将是也非常有帮助。
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.
推荐答案
我写这篇code超过1年前,所以它不是完美的,但它的工作原理:
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))
例如:
>>> Write('Hello, World!', speed=3)
Hello, World!
>>> Press('ENTER')
如果你想实现一些键,那么你可以找到自己的codeS的此处。而刚刚加入这个关键基础字典。
If you want to implement some more keys then you can find their codes here. And just add this key to Base dictionary.
这篇关于在Python 2.7模拟的关键preSS事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!