Skip to content Skip to sidebar Skip to footer

Python Module (keyboard) Is Not Recognizing 'fn' Key

I want to use the function keys as a part of a keyboard shortcut with python (3.6) and it is failing to recognize the fn key event. import keyboard keyboard.press_and_release('fn+d

Solution 1:

The keyboard project doesn't support the fn key, no. The keyboard.all_modifiers set shows you what it can support.

On my Mac that produces:

>>> import keyboard
>>> keyboard.all_modifiers
{'alt', 'ctrl', 'windows', 'shift'}

Take into account that the Fn key may not be visible to the OS. From Wikipedia:

The Fn key is a form of meta-modifier key, in that it causes the operating system to see altered scancodes when other keys on the keyboard are pressed. This allows the keyboard to directly emulate a full-sized keyboard, so the operating system can use standard keymaps designed for a full-sized keyboard. However, because the operating system has no notion of the Fn key, the key can not normally be remapped in software, unlike all other standard keyboard keys.

(bold emphasis mine).

Apple Macs are the exception to this, I'm reasonably sure Windows is not.

Mac support is still experimental, from the project description:

Works with Windows and Linux (requires sudo), with experimental OS X support (thanks @glitchassassin!).

(bold emphasis mine).

There already is an open issue with the project (#221, Unable to detect the top row of keys on Mac keyboard as function keys) tracking this.

For Windows and Linux, try to capture the keycode that fn+down actually translates to for the OS (with a keyboard.hook() callback, see this example), then execute that keycode.

If fn+down controls your volume, just use "volume down" or integer value 0xae as the scan code:

keyboard.send(0xae)

Post a Comment for "Python Module (keyboard) Is Not Recognizing 'fn' Key"