Key event strings to readable

Key event strings to readable/showable text. (from ZEROKEY to 0, ONEKEY to 1, etc)

*** @monster made me aware of the fact that we can’t map special keys (ä,ü, ç, etc.) so keep in mind that this is basically only useful for qwerty (us) keyboards, other keyboards will work as well, but not all keys would be recognized, due to blender can’t read out the keyboard mappings ***


def key_event_readable(key_event_name):
        
    text_to_number = {  
                        #keyboard
                        'ZEROKEY'           :"0",
                        'ONEKEY'            :"1",
                        'TWOKEY'            :"2",
                        'THREEKEY'          :"3",
                        'FOURKEY'           :"4",
                        'FIVEKEY'           :"5",
                        'SIXKEY'            :"6",
                        'SEVENKEY'          :"7",
                        'EIGHTKEY'          :"8",
                        'NINEKEY'           :"9",
                        
                        'ACCENTGRAVEKEY'    :"`",
                        'BACKSPACEKEY'      :"<--",
                        'COMMAKEY'          :",",
                        'EQUALKEY'          :"=",
                        'LEFTBRACKETKEY'    :"[",
                        'RIGHTBRACKETKEY'   :"]",
                        'MINUSKEY'          :"-",
                        'PERIODKEY'         :".",
                        'SEMICOLONKEY'      :";",
                        'SLASHKEY'          :"/",
                        'BACKSLASHKEY'      :"\\",
                        'QUOTEKEY'          :"'",
                        'RETKEY'            :"Return",
                        
                        'PADPLUSKEY'        :"Pad +",
                        'PADSLASHKEY'       :"Pad /",
                        'PADASTERKEY'       :"Pad *",
                        'PADPERIOD'         :"Pad .",
                        'PADMINUS'          :"Pad -",


                        #mouse
                        'WHEELUPMOUSE'      :"Scroll up",
                        'WHEELDOWNMOUSE'    :"Scroll down",
                        'LEFTMOUSE'         :"LB",
                        'RIGHTMOUSE'        :"RB",
                        'MIDDLEMOUSE'       :"MMB"
                    }
                                
    if key_event_name in text_to_number:
        return text_to_number[key_event_name]
    elif key_event_name.endswith('KEY'):
        return key_event_name[:-3]
    elif key_event_name.endswith('MOUSE'):
        return key_event_name[:-5]
    else: 
        return key_event_name

(not all keys are placed inhere, like controlKey are missing etc, you can add them)

find a place to save that dictionary. creating it all the time is inefficient.

Just make it a standalone script import it where you need it, you never have to touch it again. Atleast thats how i’m using it.

Isn’t bge.events.EventToString doing that already?

Be aware you are dealing with key-codes, not with mapped keys. The BGE API does not provide the mapped keys. A mapped key means you get the character that is printed onto the physical key (which is identified by a key-code).

This means your dictionary maps keys on your keyboard only (eventToString - maps US keyboards only).

Nevertheless your disctionary can easily be reconfigured to work with other keyboard layouts.

1 Like

No, the eventToString turns the key event like: 120 to it’s key name, so for example Wkey.
It will not turn the event to it’s shorten off keybord name like just W.

However EventToCharacter will do this for you. so you could use that instead of using the dict, but my dict has an additional option tough, the ability to convert the keys into images. This option is not build in in blender and it’s what i needed.

Hmm, are you sure? aren’t the events the same for every keyboard? (only it’s key location is placed on an other spot?)

Unfortunately yes. This is pretty annoying as I can type “äüö” on a german keyboard, but it shows me something completely different. Most letter keys have the same mapping. Y and Z are swapped. Special character keys like #± are different.

The OS knows about that (otherwise you wouldn’t be able to type a simple email), but I haven’t found a way to ask the OS what the mapping is. Neither in BGE API not in Python.

As it is like that we have to live with it. Just keep it in mind :slight_smile:

Ok, that’s not what i wanted to hear haha.
Hmm, i use this due to the sensor does not register everything either, or using spacebar will only show an empty space, etc. Well and of course to convert the keys into images. But now i need to rethink about this method if it does not work with all keyboards.

edit

So there is no option to read other keyboards?

I haven’t found a method. I would be happy If there is one. I mean there must be something. Blender reads the keyboard mapping (when you enter text). There is no obvious access or I’m just blind.

Ok did some research and found a way to convert the special keys into either there keycode or name with ord() and chr().

    own['Text'] = chr(228) + ' = ' + str(ord('ä'))
    #outputs: ä = 228

If logic.keyboard.active_events can detect the code 228, and i think so due to you said it types something totally else. But i can’t test this one.

here is a test setup with chr, ord, to char, to string, and keycode itself, both reads out different values for a lot of keys, so it stays a struggle. But it means that chr() know’s the mapping.
keyboard_input_test.blend (481.2 KB)

Right at the beginning I was thinking the same.

But this converts from extended ASCII to number and vice versa. It has no direct relationship to key codes.

More worse, it depends on the used character set (only the first 128 characters of th ASCII character set are always the same). That is a typical problem on cross system and multilanguage environments.

You need the OS to convert the key press (key code) to an ASCII character first. This finally is the key board mapping (converting the code of the pressed key into a character that matches what is printed on top of the key). After that you can work with the character as you want (including converting to other formats),

Hmm, ok. ill add some info about this in the first post so people know it as well. Thanks for the explanation.