简体   繁体   English

有没有一种方法可以在python中动态创建/修改函数

[英]Is there a way to create/modify function on the fly in python

I try to simulate keyboard with python and I don't know how to deal with multiple keyboard button press. 我尝试用python模拟键盘,但我不知道如何处理多个键盘按钮的按下。 Code below works perfectly fine with 1 or 2 keys pressed at the same time (fe 'ctrl + c'): 下面的代码通过同时按下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())

But what if there are 3 or more buttons pressed at the same time? 但是,如果同时按下3个或更多按钮怎么办? What is the most elegant way to do this? 最优雅的方法是什么? I could just add if '+' count == 2, if '+' count == 3 etc. but there must be better way to do it. 我可以只添加'+'count == 2,如果'+'count == 3等,但是必须有更好的方法来实现。 I would like my function to adjust to number of arguments. 我想让我的函数适应参数数量。

For example: 例如:

keyboard_sim('ctrl + shift + esc'): keyboard_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())

I am not familiar with the SendInput/Keyboard stuff you are using so I am assuming that they are custom and written by you. 我不熟悉您使用的SendInput / Keyboard内容,因此我假设它们是自定义的并由您编写。

Assuming that SendInput is defined like def SendInput(*args) (as suggested by @JETM) and that last_arg should actually be current_arg, you should be able to call it like this: 假设定义了SendInput就像def SendInput(*args) (如@JETM所建议),并且last_arg实际上应该是current_arg,则应该可以这样调用它:

arglist = current_arg.split('+')
# This will create a list of Keyboard objects
keys = [KeyBoard(globals()["VK_%s" % key.upper()]) for key in  arglist]
# *keys splits the list of Keyboard objects so that SendInput receives
# one entry in it's argument list for each Keyboard object in keys
SendInput(*keys)

Using this, within SendInput the args variable would be a list with one Keyboard object for each key. 使用此方法,在SendInput中,args变量将是一个列表,其中每个键都有一个Keyboard对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM