python - 有没有办法在 python 中动态创建/修改函数

标签 python dynamic-function

我尝试用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 个或更多按钮怎么办?最优雅的方法是什么?我可以添加 if '+' count == 2、if '+' count == 3 等等,但必须有更好的方法来做到这一点。我希望我的函数能够根据参数数量进行调整。

例如:

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())

最佳答案

我不熟悉您正在使用的 SendInput/Keyboard 内容,因此我假设它们是自定义的并由您编写。

假设 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)

使用此方法,在 SendInput 中,args 变量将是一个列表,其中每个键都有一个 Keyboard 对象。

关于python - 有没有办法在 python 中动态创建/修改函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35512165/

相关文章:

python - 异步套接字编程的最佳解决方案

python - 连续出现的项目数

angular - 动态函数调用 Angular 2

c++ - 运行时的动态函数调用(va_list)

Python pandas - DataFrame groupby 和重构

python - App Engine 开发服务器 : bad runtime process port ['' ] No module named google. appengine.dist27.threading

python - 列出 python 中的赋值行为

Php - 理解 create_function() - 传递简单变量

函数的 Python setattr() 采用初始函数名称