Hello. I am learning blender and python and have a problem.
For my project, a little word game for children, I want to get the norwegian characters æ ø å from the keyboard sensor. In norwegian keyboards they are usually at the place of the ; ’ [ keys on us-keyboards. But as far I can tell (and that is not very far) those æøå returns nothing in my string from the sensor (keyboard sensor is set up with ‘all keys’, a string property, and Log Toggle on.
So no output in debug mode from those characters while other characters work). Is there some easy fix for this? (please:)
The BGE directly accesses the keycode from keyboard. It does not matter what is printed on the key. This is because a game needs to avoid the delayed processing as done by the OS.
When entering text you would expect exactly that. But the BGE does not support keycode to character conversion via Keyboard layout (which is OS dependent). I haven’t found a way via Python. If someone knows please let me know. It must be possible as Blender can do it too.
But the keys in question, the æøå, does not return any code at all.
I can get the integer keycode and and a keystring from most of keys with the following code:
co = bge.logic.getCurrentController()
own = co.owner
# 'Keyboard' is a keyboard sensor
sensor = co.sensors["Keyboard"]
for key,status in sensor.events:
# key[0] == bge.events.keycode, key[1] = status
if status == bge.logic.KX_INPUT_JUST_ACTIVATED:
string = bge.events.EventToString(key)
print(key)
print(string)
If the (æøå )keys had returned something it would be ok to operate on that int code or string array as needed. But with no feedback my hope is fading. Is this bug?
No, nothing happens when I press these keys. While pressing other keys I get feedback, pressing ‘a’ gives int 97 and AKEY, space gives int 32 and string SPACEKEY, and so on. But pressing the æ/ø/å keys gives nothing.
This sounds a bit like the keyboard is not fully supported. I haven’t heard of that, but I’m not that familar anyway. You might want to try other applications that support direct access to the keyboard e.g pygame, panda3d or even non-Python systems like jMonkey.
I’ve got a French keyboard and a lot of keys don’t seem to be supported :
nothing is printed if I press :
&, é, ", (, è, _, ç, à, ), $, ^, *, ù, !, :, <
I’ve tried to find solutions elsewhere but nothing for the moment
Keycodes are the code the hardware is transferring. A keycode identifies the physical key on the device e.g. keycode 1 means -> key 1. It does not matter if it is a blue key, red key, large key small key, what is printed or if it even exist.
Character sets define a list of printable characters such as ASCII, EBCDIC, UTF.
Keycode and Character sets have no relation ship at all. One describes a hardware key the other describes a printable character.
When you as user press a hardware key you expect that an application prints a character with the meaning of this key. The meaning is usually printed on the hardware key. How does the application know what the keycode means your keyboard is sending to the PC?
The OS receives the keycode from the hardware and translates it into a character (or command). It also deals with continued pressing of keys (character repeat) and key combinations <shift+a>. This is called Keyboard mapping. If it is not set correctly (e.g. you have a german keyboard but your mapping is set to french) the translation will be incorrect. Some keys match what is printed on others do not.
A text-accepting application (such as notepad) asks the OS to get the already mapped character code. The application does not need to care the mapping or how to deal with character repeat, as the OS does that already)
A game application does not want the delay on key repeat as this would break the direct control the user is expecting. Therefore it acts on the keycodes before the OS is processing them. The application needs to deal with the keyboard mapping if needed (e.g. on text input)
To answer your question: It does not matter what is printed on your key - the keycode is what is important. The BGE is does not do that and assumes US keyboard mapping for unknown reasons (as a Dutch product the developers should have known better).
Therefore the resulting characters are plain wrong when not using a US keyboard.
Unfortunately Python seams to miss access to do the translation either. I haven’t found a solution to it by now.
Problem solved!
I’ve used PyUSB as noted in the previous post.
Than I had to map each key number of my keyboard, recognised by PyUSB, with a correct character (that can take a few minutes but it’s done one and for all).
And here we go!
Thanks to that, I’ve managed to even display ALT+ key and SHIFT + key special character.
PS: You need to change idVendor and id Product of your keyboard of course, and also the special map of your keyboard numbers : this one is for AZERTY)
An useful link too : the unicode chart : http://www.ssec.wisc.edu/~tomw/java/unicode.html#x0080
Hi
I’ve programming a little code to demonstrate the use of PyUSB to recognize special characters, even if Blender doesn’t do it natively:
it’s a learning typing program (the kind of program where you learn typing fast at the keyboard by typing the characters that the program show you):
when you type the good character (even &, é, è, à etc…), the character turns green, red otherwise.