boppreh / keyboard

Hook and simulate global keyboard events on Windows and Linux.
MIT License
3.77k stars 433 forks source link

How to get non-localized English-only event names? #533

Open josephernest opened 2 years ago

josephernest commented 2 years ago

Congrats for this library! With

import keyboard
event = keyboard.read_event()
print(event.name)

I get

haut     # I would prefer: 'up' (or 'up arrow')
maj      # I would prefer: 'shift'
windows gauche   # i would prefer: 'windows' or 'left windows'

How to disable localization and get only English event names, even on a French Windows?

josephernest commented 2 years ago

It seems it comes from https://github.com/boppreh/keyboard/blob/master/keyboard/_winkeyboard.py#L372

    name_ret = GetKeyNameText(scan_code << 16 | is_extended << 24, name_buffer, 1024)

and GetKeyNameTextW seems to be always in localized lang.

Linked topic: https://stackoverflow.com/questions/7416779/force-getkeynametext-to-english

boppreh commented 2 years ago

I agree with it, and I'm working on it on a separate branch (see for example https://github.com/boppreh/keyboard/commit/275b3b466cb71b904aa02c8911e242e5a4b180af ).

The challenge is getting all of these types of keys right:

There are some 4 different ways of getting key names in Windows, and each of them gives good results for some keys, but awful results for others.

Regarding localized keys, it's good to at least keep track of them, so that if the user creates a hotkey 'maj+haut' the library knows what to do with it.

josephernest commented 2 years ago

Thanks for your answer and congrats again for your lib @boppreh.

Would you know a quick hack to set English everywhere at least for keyboard.read_event().name?