keyboard icon indicating copy to clipboard operation
keyboard copied to clipboard

Keyboard module does not have "option" key on OSX

Open theshinyknight opened this issue 3 years ago • 5 comments

It is not possible to use keyboard on OSX for any shortcut that require the Option key.

Such key seems to not exist

theshinyknight avatar May 03 '21 08:05 theshinyknight

Check the files, it is there. https://github.com/boppreh/keyboard/blob/master/keyboard/_darwinkeyboard.py

lnfjobs avatar May 03 '21 19:05 lnfjobs

Thanks for the reply; so why do I get this error about the tuple being out of range?

    keyboard.press_and_release('cmd+option+i')
/Users/user/Library/Python/3.8/lib/python/site-packages/keyboard/__init__.py:383: in send
    _os_keyboard.press(scan_codes[0])
E   IndexError: tuple index out of range

I am just calling keyboard.press_and_release('cmd+option+i'); the same exact call using Shift instead of option works fine.

The value in the step variable in the init.py before the error is ((55,), (), (34,)), while when I call cmd+shift+i, the value is ((55,), (60,), (34,)); so for some reason the code for Option is not used.

Tracing the code, seems that the function recognize the key as ALT and normalize as Left Alt, but can't find the correct code (58) and return the empty value. I used the tuple with codes and the keypress is correctly performed. Looks like a bug to me.

theshinyknight avatar May 05 '21 06:05 theshinyknight

I ran into the same issue. "option" is normalized to "left alt", but there's no code for "left alt", only code for "alt". I had to hack the source code, and map "option" to "option" to get it to work as highlighted in blue: image

YatingWang avatar May 15 '21 03:05 YatingWang

It looks like alt is not registered in _darwinkeyboard.py, but option is, but the canonical_names mapping translates option into alt. Regardless of which name you use, it's translated into the name that isn't registered.

I was able to hack around this by sending the actual keycode for option. Unfortunately you can't use the string hotkey representation to do this, so you have to send a list of keys to press.

desired: keyboard.send('option+y') workaround: keyboard.send([0x3a, 'y'])

zymergen-luke avatar Dec 10 '21 18:12 zymergen-luke

Duplicate of https://github.com/boppreh/keyboard/issues/331

adabru avatar May 04 '22 05:05 adabru