Simple chat window, using getKeys, getCurrentlyPressedKeys, keymap, dictionary

I’m making a simple script to create a chat window, similar to bzoo, only simpler, for simpletons like myself.

Problem: getCurrentlyPressedKeys returns values that aren’t compatible with strings. Codes are all wrong

Solution: I made a key mapping dictionary.

First, complete this tutorial until you can see your text appear in-game:
http://www.blending-online.co.uk/8501/17001.html

Set the value of your Text property to “Press P to play” Now, attach this script to the plane you added earlier. Bind it to a sensor for “all keys”. Name it getkeys.py

import GameLogic as gl
co=gl.getCurrentController()
own=co.getOwner()
sens=co.getSensor("sensor")

key=sens.getCurrentlyPressedKeys()
if key<>None:
    if key[0][1]==1 and key[0][0]==13:
        own.Text=">"
    if own.Text=="Press P to play":
        own.Text=">"
    letter=shift=0

    for j in key:
        if j[0]==128 or j[0]==129:
            shift=1
        else:
            if j[1]==1:
                letter=j[0]
    changed={44:[0,16],45:[0,50],46:[0,16],47:[0,-16],
    48:[0,-7],49:[0,-16],50:[0,14],51:[0,-16],52:[0,-16],
    53:[0,-16],54:[0,40],55:[0,-17],56:[0,-14],57:[0,-17],
    129:[0,-34],136:[-97,-5],135:[-76,-1],137:[-41,30],
    138:[-91,16],139:[-47,32],140:[-79,-18],141:[-50,32],
    142:[-49,32]}
    if letter==133:
        own.Text=own.Text[:len(own.Text)-1]
        letter=0
    elif letter in changed:
        letter+=changed[letter][0]+changed[letter][1]*shift
    elif letter > 32:
        letter-=32*shift
    if letter > 13:
        own.Text+=chr(letter)

Todo list:

Let me know if any of this interests you or check http://nerds.tk for source downloads, if they are ever ready for consumption…

  1. Create script to process keyboard input (done)
  2. Have letters appear on screen as you type (done)
  3. Create chat overlay window (todo)
    http://ltc.smm.org/visualize/book/print/61
  4. Build flexible threaded socket server for chat and games (done!)
  5. Create animated characters for game world (3 done).
  6. Log several clients into LAN game and see each other walking around (done!)

Wow. Your code is very well-written, but that functionality is already coded into the Keyboard Sensor. I started to write a tutorial about this a while ago and never published it…

http://www.blendenzo.com/tutInputText.html

I never got around to adding a section about making a flashing cursor or a section about context sensitive text input (like only accepting input when the specific text box is “in focus”).

(The tutorial is “Blender web plugin enhanced” and like all Blender web plugin content, it is best viewed in Internet Explorer.)

Yeah, it’ll be a lot easier if you use that functionality. But here’s a tip for anytime you need to use getPressedKeys or getCurrentlyPressedKeys:

if j == [[64,1]]:

That way you don’t have to worry about checking to see if it’s “none”.

Thanks, blendenzo. That’s quite a compliment coming from you. I wasn’t aware of how to use the log toggle property (or even what to ask) so thanks again. I’m just learning python so I’m trying to use concepts like lists and dictionaries while they are still fresh in my mind.

Well, I tried the logtoggle property and it works, except I can’t input some characters like the questionmark ? don’t show up when I type so I still have to use the script.