Search code examples
pythondynamic-function

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. Code below works perfectly fine with 1 or 2 keys pressed at the same time (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? 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. I would like my function to adjust to number of arguments.

For example:

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

Solution

  • I am not familiar with the SendInput/Keyboard stuff you are using so I am assuming that they are custom and written by you.

    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:

    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.