moses-palmer / pynput

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

How to get the pressed key using GlobalHotkeys? #531

Open bd-charu opened 1 year ago

bd-charu commented 1 year ago

I need to get the pressed key of GlobalHotkeys so that I can filter key combinations. Using Listener objects you can do this by passing on_press and on_release parameters. But if I do it in GlobalHotkeys it's giving me an error. How do I do this?

# MyHotkeys.py
class MyHotkeys(GlobalHotKeys):
    def __init__(self, hotkeys):
        super().__init__(hotkeys, darwin_intercept=self.my_filter)
    def my_filter(self, t, e):
        pass

# main.py
def my_func():
    # How to get key here?
    print("hello world")

my_hotkeys = MyHotkeys ({"<ctrl>+i": my_func})
EvgeniyDoctor commented 1 year ago

@bd-charu hey, did you figure it out?

moses-palmer commented 1 year ago

Thank you for your report.

GlobalHotkeys derives from a keyboard listener and accepts the same arguments. Have you tried simply passing darwin_intercept=some_function to its constructor?