简短的摘要:
我正在尝试创建一个程序,将键盘事件发送到计算机,为了所有目的,模拟事件应被视为键盘上的实际击键.
原帖:
我正在寻找一种使用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
Run Code Online (Sandbox Code Playgroud)
以上显然是示例,但我正在寻找的是库,模块或其他,我可以用它来模拟键盘事件.
注意:这与将字符发送到记事本,或将文本输入字段等不同.我希望python脚本模拟一个实际的键盘事件,计算机会认为确实存在键盘事件.
额外注意:
我不想将键击发送到活动窗口 - 我希望系统能够相信键盘的按键被按下,微妙的区别,因为一些活动窗口不接受某些键组合,或者我想使用键盘快捷键对于通过我的脚本的后台进程,他们不需要通过活动窗口
到目前为止,我已经看过这些事情:
这些都是关于苹果的,并没有任何帮助.
还有这个:
这似乎可能是我需要的,但我找不到它的库或任何文档.
我也搜索了更多的地方,但还没有找到解决方案.
I have a simple python script, that has some functions that run in a loop (I'm taking sensor readings).
while True:
print "Doing a function"
Run Code Online (Sandbox Code Playgroud)
If the keyboard is pressed I'd like to print "key pressed".
What's the simplest way of doing this in Python? I've searched high and low. I've found out how to do it with pygame, but I'd rather do it without. If I do have to use pygame is it possible to not have a separate …
我想发送几乎这样的命令:
when keypress=="a" #if entered key is "a"
send {ALT+TAB} # send ALT and TAB simultaneously
sleep(2) #wait for 2 sec
send {"I love my Country",0.1} #send all strings at 0.1 sec wait
key_down("BACKSPACE",1) #hold down backspace key for 1 sec
send{ALT+F4} #send ALT and F4 simultaneously
Run Code Online (Sandbox Code Playgroud)
对于所有或特定应用程序,也是opengl游戏.
我尝试了SendKeys.py,但是没有同时输入的功能,也没有用于发送按住键命令的key_down方法.所有建议将不胜感激.