Keyboard sensor incompatible with UK Keyboard

Using a keyboard sensor placing key presses into a property seems to assume you’re using a US keyboard. I.e. US keyboard SHIFT+3 would enter a ‘#’, however on a UK keyboard it should enter a ‘£’. I can’t seem to make it enter the correct ‘£’ and I can’t find anything in the documentation about setting the input language. How can I fix this and get the right symbol?

do you need it for a game?
I just don’t understand…

how is it displayed?

or is shift+3 just not working at all?

when you map the key pressing shift+3 =nothing?

or an error?

Try it in Word or Notepad. Change the keyboard region to UK, then US. You’ll see that SHIFT+3 produces different symbols. Unfortunately the BGE keyboard sensor will only produce the US key mapping, meaning UK keyboards when typing will have the wrong symbols showing up. This is important for me because the apostrophe key is also different between the two keyboards.

you could probably try to get key input with only using python, there seems to be a few good examples here and you could also import locals()(here) but i’m not 100% sure, but I hope it helps.

Thanks for the suggestion! I have explored that path and found that Blenders console does the Python processing, not the GUI that we see, so msvcrt only picks up typed letters when you have the console selected.

In theory I could add a hook to pick up all key presses, regardless of the active window, but that would mimic a key logger so closely that I’d expect antivirus programs would be very upset about it.

The keyboard sensor reads raw key-codes not character-codes (e.g. ASCII or unicode). This is a pretty common behavior in games as you are more interested in the key press rather then the letter printed on the key.

Unfortunately when typing words you need it the other way around.
Usually the OS provides a translation of key-codes to characters-codes.

For what-ever reason the BGE has no implementation to allow this keyboard mapping. I was not even able to find a way to translate key-codes to characters with Python. If someone know how to do that it would be nice.

You could probably change the keys when the keyboard is active probably not the most efficient but:


for keys in keyboard.events:
    if key == bge.events.AKEY:
          keyString = "D"

and then send the keyString to whatever your using to print out your text but once again that might not be that efficient.

I was worried you’d say that…

I did look into the bge.events module but it was fairly limited on the symbols it had. Looks like I’ll have to prompt the user to select his/her keyboard layout and change the dictionary accordingly.

This is the method I’ve used so far for keyboard entry:
First, the keyboard map.


GameLogic.alphabet = {
32: " ",
45 : "-",
136: "'",


48 : "0", 49 : "1",
50 : "2", 51 : "3",
52 : "4", 53 : "5",
54 : "6", 55 : "7",
56 : "8", 57 : "9",


97 : "a", 98 : "b",
99 : "c", 100 : "d",
101 : "e", 102 : "f",
103 : "g", 104 : "h",
105 : "i", 106 : "j",
107 : "k", 108 : "l",
109 : "m", 110 : "n",
111 : "o", 112 : "p",
113 : "q", 114 : "r",
115 : "s", 116 : "t",
117 : "u", 118 : "v",
119 : "w", 120 : "x",
121 : "y", 122 : "z",


151 : "1", 147 : "2",
152 : "3", 148 : "4",
153 : "5", 149 : "6",
154 : "7", 150 : "8",
155 : "9", 158 : "0"
}

And the keyboard entry.


import GameLogic


cont = GameLogic.getCurrentController()
own = cont.owner
sen = cont.sensors
act = cont.actuators

if sen["allKey"].positive:
    current_key = -1
    for x in sen["allKey"].events:
        if x[1] == 1:
            current_key = x[0]
    if (current_key in GameLogic.alphabet) and (len(own["temp_text"]) <= 20):
        own["temp_text"] += GameLogic.alphabet[current_key]


That particular entry is all lower case, but it’s easy enough to add the shift keys using sensors and a second keyboard map with the upper case variants.

-edit-
sen[‘allKey’] is a keyboard sensor with ‘All Keys’ turned on.