Keyboard sensor problems: keypress frequency and unicode characters


I’m trying to create user text input dialog. I put a text object and connect a keyboard (all keys) sensor to its python handler.

I could manage to get keypresses written (appended) to the text object. Here is the python handler code:

import bge

c = bge.logic.getCurrentController()
t = c.owner

kb = c.sensors["Keyboard"]

key =[0][0]

press =[0][1] == 1
release =[0][1] == 3

key = chr(key)

if press: 
    t.text += key

print("key pressed: %s" % (key,))


Problem 1: Unicode characters could not be displayed:
1.1: If you try to write to the text object in edit mode for example “çalış öğün”, “çal öün” is written. What happens to the other characters?
1.2: if you wrote “çalış öğün” to the text object and “çal öün” is written on the display, press P to start game engine, there is “çalış öğün” is written on the display.
1.2: If you try to write the same example text in game mode, the python handler could not get many unicode characters’ and special characters’ events (like çışöğü@`#´µû€âû™+ etc…)

Problem 2: If you write too fast, it is not being recongized by event generator (the sensor).

How could I solve these problems?

  1. a) the keyboard sensor senses keyboard directly ignoring any keyboard mapping of the OS.

The BGE uses default keyboard mapping only (keycode to ascii). Which means if you coincidentally have a default (US) keyboard layout the translation will be incorrect. Therefore the BGE is more or less useless for textual input.

1.b) While the GUI support unicode characters - the BGE API (to fill the text attribute of the font object) does not.

You can use the Bitmap text which does as long as the required characters fit into the bitmap texture.

  1. I think there was a thread discussing this