pynput icon indicating copy to clipboard operation
pynput copied to clipboard

listening to function keys on macOS

Open cltrudeau opened this issue 3 years ago • 1 comments
trafficstars

Description I'm trying to use GlobalHotKeys to catch F13 on macOS. My sample program correctly catches CTRL+R, but doesn't catch F13. I've done some debugging and admit I don't fully understand the code, but from what I can tell:

  • The '' is getting parsed correctly and the appropriate HotKey instance is being stored.
  • The key press is triggering a call to canonical()
  • canonical() is returning a _darwin.KeyCode
  • the HotKey._keys list contains a Key instance
  • the comparison of a Key and a _darwin.KeyCode is failing to match

Platform and pynput version macOS 11.6.1 (Big Sur) pynput==1.7.6

To Reproduce

from pynput import keyboard

def callme():
    print("I was called")

keymap = {
    '<ctrl>+r': callme,
    '<f13>': callme,
}

print('Starting...')

with keyboard.GlobalHotKeys(keymap) as m:
    m.join()

Pressing CTRL+R results in "I was called", pressing F13 does not

cltrudeau avatar Jan 16 '22 21:01 cltrudeau

They also don't work on Linux. At least not, if they are defined as, for example, "<f1>":

ghk = keyboard.GlobalHotKeys({"<f1>": lambda: print("x")})
ghk.start()

I can press F1 as much as I want, nothing happens. However, if I use this instead:

ghk = keyboard.GlobalHotKeys({"<65470>": lambda: print("asd")})
ghk.start()

Then F1 does work.

I did notice that the result of HotKey.parse is indeed different for the two options:

>>> keyboard.HotKey.parse("<f1>")
[<Key.f1: <65470>>]
>>> keyboard.HotKey.parse("<65470>")
[<65470>]
>>> type(keyboard.HotKey.parse("<65470>")[0])
<class 'pynput.keyboard._xorg.KeyCode'>
>>> type(keyboard.HotKey.parse("<f1>")[0])
<enum 'Key'>

Xandaros avatar Feb 15 '22 04:02 Xandaros