我尝试用python来模拟键盘,但我不知道如何处理多个键盘按钮的按下。如果同时按下1或2个键(fe'ctrl+c'),下面的代码可以正常工作:
if '+' in current_arg:
current_arg = current_arg.split('+')
current_arg[0] = current_arg[0].strip()
current_arg[1] = current_arg[1].strip()
SendInput(Keyboard(globals()["VK_%s" % current_arg[0].upper()]),
Keyboard(globals()["VK_%s" % current_arg[1].upper()]))
time.sleep(input_time_down())
if len(last_arg) > 1 and type(last_arg) == list:
SendInput(Keyboard(globals()["VK_%s" % last_arg[0].upper()], KEYEVENTF_KEYUP),
Keyboard(globals()["VK_%s" % last_arg[1].upper()], KEYEVENTF_KEYUP))
time.sleep(input_time_down())
else:
SendInput(Keyboard(globals()["VK_%s" % last_arg.upper()], KEYEVENTF_KEYUP))
time.sleep(input_time_down())
但如果同时按下3个或更多按钮呢?最优雅的方法是什么?我可以加上如果'+'计数==2,如果'+'计数==3等等,但一定有更好的方法。我想我的函数调整到参数的数量。你知道吗
例如:
键盘sim('ctrl+shift+esc'):
if '+' in current_arg:
current_arg = current_arg.split('+')
current_arg[0] = current_arg[0].strip()
### function adds another current_arg for each argument
current_arg[1] = current_arg[1].strip()
current_arg[2] = current_arg[2].strip()
SendInput(Keyboard(globals()["VK_%s" % current_arg[0].upper()]),
### function adds another Keyboard for each argument
Keyboard(globals()["VK_%s" % current_arg[1].upper()]))
Keyboard(globals()["VK_%s" % current_arg[2].upper()]))
time.sleep(input_time_down())
if len(last_arg) > 1 and type(last_arg) == list:
### function adds another Keyboard KEYEVENTF for each argument
SendInput(Keyboard(globals()["VK_%s" % last_arg[0].upper()], KEYEVENTF_KEYUP),
Keyboard(globals()["VK_%s" % last_arg[1].upper()], KEYEVENTF_KEYUP))
Keyboard(globals()["VK_%s" % last_arg[2].upper()], KEYEVENTF_KEYUP))
time.sleep(input_time_down())
else:
### this is added so I won't get error if there is single key pressed
SendInput(Keyboard(globals()["VK_%s" % last_arg.upper()], KEYEVENTF_KEYUP))
time.sleep(input_time_down())
我不熟悉你正在使用的SendInput/键盘的东西,所以我假设它们是由你定制和编写的。你知道吗
假设
SendInput
的定义类似于def SendInput(*args)
(正如@JETM所建议的),并且最后一个\u arg实际上应该是当前的\u arg,您应该可以这样调用它:在SendInput中,args变量将是一个列表,每个键有一个键盘对象。你知道吗
相关问题 更多 >
编程相关推荐