pynput icon indicating copy to clipboard operation
pynput copied to clipboard

How to get the pressed key using GlobalHotkeys?

Open bd-charu opened this issue 2 years ago • 2 comments

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

bd-charu avatar Jan 10 '23 13:01 bd-charu

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

EvgeniyDoctor avatar Mar 08 '23 20:03 EvgeniyDoctor

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?

moses-palmer avatar Mar 14 '23 19:03 moses-palmer