moses-palmer / pynput

Sends virtual input commands
GNU Lesser General Public License v3.0
1.79k stars 248 forks source link

call two instead of one function for a globalhotkey #449

Closed Marcelbgit closed 2 years ago

Marcelbgit commented 2 years ago

Dear developers,

hopefully you can help me out. I tried to use a sort of this code to call a function with a hotkey. But by using one globalhotkey I need to call two functions separately instead of one (this is because I can't merge them because then one of them doens't work properly). How can I add a additional function to a hotkey? For example '++r': function_1, function_3 in keyboard.GlobalHotkeys

from pynput import keyboard

def function_1():
    print('Function 1 activated')

def function_2():
    print('Function 2 activated')

with keyboard.GlobalHotKeys({
        '<alt>+<ctrl>+r': function_1,
        '<alt>+<ctrl>+t': function_1,
        '<alt>+<ctrl>+y': function_2}) as h:
    h.join()

cheers, Marcel

moses-palmer commented 2 years ago

Thank you for your report,

I am not sure that I understand---can't you just compose the functions?

from pynput import keyboard

def function_1():
    print('Function 1 activated')

def function_2():
    print('Function 2 activated')

def callback():
    function_1()
    function_2()

with keyboard.GlobalHotKeys({
        '<alt>+<ctrl>+r': callback}) as h:
    h.join()

If I have misunderstood, please reopen!