Key string to Key code dictionary

So trying to load a key from a text file works fine. Adding it to another string property works fine. Converting it back to a keycode (so that you can change the key in a keyboard sensor), is an absolute nightmare.

Let my loss of 20minutes be your gain:

key_dict = {'A':97, 'B':98, 'C':99, 'D':100, 'E':101, 'F':102, 'G':103, 'H':104, 'I':105, 'J':106, 'K':107, 'L':108, 'M':109, 'N':110, 'O':111, 'P':112, 'Q':113, 'R':114, 'S':115, 'T':116, 'U':117, 'V':118, 'W':119, 'X':120, 'Y':121, 'Z':122, 'LEFTARROW':143, 'DOWNARROW':144, 'RIGHTARROW':145, 'UPARROW':146, 'F1':162, 'F2':163, 'F3':164, 'F4':165, 'F5':166, 'F6':167, 'F7':168, 'F8':169, 'F9':170, 'F10':171, 'F11':182, 'F12':183, 'ZERO':48, 'ONE':49, 'TWO':50, 'THREE':51, 'FOUR':52, 'FIVE':53, 'SIX':54, 'SEVEN':55, 'EIGHT':56, 'NINE':57, 'PAD0':158, 'PAD1':151, 'PAD2':147, 'PAD3':152, 'PAD4':148, 'PAD5':153, 'PAD6':149, 'PAD7':154, 'PAD8':150, 'PAD9':155, 'PADPERIOD':156, 'PADSLASH':157, 'PADASTER':42, 'PADMI':159, 'PADPLUS':161, 'PADEN':160, 'ACCENTGRAVE':137, 'BACKSLASH':139, 'BACKSPACE':133, 'COMMA':44, 'DEL':134, 'END':186, 'EQUAL':140, 'ESC':130, 'HOME':176, 'INSERT':175, 'LEFTBRACKET':141, 'LINEFEED':132, 'MINUS':45, 'PAGEDOWN':178, 'PAGEUP':177, 'PAUSE':174, 'PERIOD':46, 'QUOTE':136, 'RIGHTBRACKET':142, 'RET':13, 'SEMICOLON':135, 'SLASH':138, 'TAB':131, 'LEFTCTRL':124, 'LEFTALT':125, 'RIGHTALT':126, 'RIGHTCTRL':127, 'RIGHTSHIFT':128, 'LEFTSHIFT':129, 'SPACE':32}



How to use:
***This dictionary assumes when you wrote the key to a file, you removed the last 3 letters.
So if I wanted to save the w key:

get the key

wkey = bge.event.WKEY

remove the last 3 letters (usually ‘KEY’)

wkey = wkey[:-3]

Write wkey to file

A couple keys such as the PADMINUS don’t have KEY at the end, this means that they are reduced to PADMI instead (this has been accounted for in the dictionary).

To retrieve a keycode simply put:

keycode = key_dict[keystring]

Where ‘keystring’ is the loaded ‘key’ from the file. In this example case you would load in ‘W’:

keycode = key_dict['W']

Any duplicates or issues or anything else let me know and I will update it.

Pretty sure you could generate that on the fly with something like:


keycodes = {}
for e in dir(bge.events):
    if e[0] != '_':
        keycodes[e] = getattr(bge.events, e)

Also available is the bge.events.EventToString(keycode) which returns the stringname (eg 97 to ‘AKEY’) and bge.events.EventToCharacter(event, shift) which does the same but as if you’d typed it (ie SPACEKEY goes to ’ ')

The only system that can translate keycode to the character printed on the keys is the OS. Unfortunately both the BGE and Python do not provide access to the keyboard mapper. Therefore you always get the US keyboard mapping … which makes it useless anywhere else.

Yeah I think getattr() was what I was looking for, I will probably replace the current dictionary with this method.

Is there a different set of codes that can be referenced when selecting another keyboard layout? If so then this keymapping dictionary might not be useless after all.

It is the mapping. You can change it in your OS. e.g. you can change the keyboard mapping from EN (English) to DE (Germany).

The key labeled “Y” on EN-Keyboards is labeled “Z” at DE-Keyboards. The key (end it’s key-code) is the same integer number. Additional the DE-Keyboard has ÄÖÜ keys. The EN-Keyboard has some special characters mapped instead.

While your “keymapping dictionary” can be used for that purpose, I expect support from the OS, as the OS knows what keyboard mapping is set up. (When I press Ö in Blender I get “ö” (and “Ö” with shift) in Blender, but not in the BGE).

As long as there is no support, your solution it can help. But it needs options to establish other mappings.