Reputation: 6754
Is it a way in Python to convert any unicode string to key codes (on a keyboard) which is required the string to be typed?
Say, if English 'h' and Russian 'р' are both typed by one key then these keys must have the same codes.
In result I need string representation as array of the key codes.
Upvotes: 1
Views: 1439
Reputation: 5354
Given that there is a large number of keyboard layouts out there, you would need a way to have an answer to the question "which keyboard map to use?". For example, on a US keyboard, "Q" is scancode 24, but on a French one, the same letter is on a different scan code (and "A" -> 24 instead).
You could limit this to the keyboard layout(s) installed on the host that you run this on. Then, for any character that can be typed on that host, you can find which key it came from (assuming you don't have two layouts that have the same letters, but on different locations - e.g., US (qwerty) and French or Dvorak).
The way you get the key maps is specific to the OS, e.g., if this is a desktop with XOrg driving display and keyboard, you could run xmodmap -pk
and read its output. It has the list of all keycodes and all characters they map to (one line per key code, with several characters that it maps to). Use the subprocess
module to run xmodmap
or whatever utility your OS has to get the keymaps and read it into your program.
Upvotes: 2
Reputation: 2223
Use getch to get key reference then use function ord to get the number equivalent to this caracter
ord(getch())
Keep in mind the number and button probabily changed based on user keyboard
Upvotes: -1