Game Development Community

Reading key input...

by Nic Cusworth · in Torque Game Builder · 02/19/2007 (8:42 am) · 7 replies

I hardly dare ask but... I want to read what the player is typing on the keyboard. For example: the player finds a locked door and has to type in the code printed on the door. How do I get TGB to listen for the keyboard and tell me what's been typed?

I'm shaking at the prospect of having to write a function that binds each key individually :(

Nic.

#1
02/19/2007 (11:30 am)
I've never used the anykey before, but check out the docs and go to Reference->Input Interaction. In the Misc Events table, it has the anykey event. Give that a whirl :)
#2
02/19/2007 (9:01 pm)
@Nic, you could write a for-loop that binds all alphanumeric values for you ... just create a string like so:

"0123456789ABCDEFGHIJK ...."

Then write a loop that uses getSubStr ...

Alternatively, you could probably use a GUI TextEdit ctrl -- when the player tries to open the door, you simply popup an "enter your code" window, that contains a GUI Text Edit control, and set the focus to it (make it the first responder?)
#3
02/20/2007 (9:21 am)
Well - this is how I ended up doing it. Dropped this in my game.cs...

moveMap.bindCmd(keyboard, "a", "keyRead(A);","");
	moveMap.bindCmd(keyboard, "b", "keyRead(B);","");
	moveMap.bindCmd(keyboard, "c", "keyRead(C);","");
	moveMap.bindCmd(keyboard, "d", "keyRead(D);","");
	moveMap.bindCmd(keyboard, "e", "keyRead(E);","");
	moveMap.bindCmd(keyboard, "f", "keyRead(F);","");
	moveMap.bindCmd(keyboard, "g", "keyRead(G);","");
	moveMap.bindCmd(keyboard, "h", "keyRead(H);","");
	moveMap.bindCmd(keyboard, "i", "keyRead(I);","");
	moveMap.bindCmd(keyboard, "j", "keyRead(J);","");
	moveMap.bindCmd(keyboard, "k", "keyRead(K);","");
	moveMap.bindCmd(keyboard, "l", "keyRead(L);","");
	moveMap.bindCmd(keyboard, "m", "keyRead(M);","");
	moveMap.bindCmd(keyboard, "n", "keyRead(N);","");
	moveMap.bindCmd(keyboard, "o", "keyRead(O);","");
	moveMap.bindCmd(keyboard, "p", "keyRead(P);","");
	moveMap.bindCmd(keyboard, "q", "keyRead(Q);","");
	moveMap.bindCmd(keyboard, "r", "keyRead(R);","");
	moveMap.bindCmd(keyboard, "s", "keyRead(S);","");
	moveMap.bindCmd(keyboard, "t", "keyRead(T);","");
	moveMap.bindCmd(keyboard, "u", "keyRead(U);","");
	moveMap.bindCmd(keyboard, "v", "keyRead(V);","");
	moveMap.bindCmd(keyboard, "w", "keyRead(W);","");
	moveMap.bindCmd(keyboard, "x", "keyRead(X);","");
	moveMap.bindCmd(keyboard, "y", "keyRead(Y);","");
	moveMap.bindCmd(keyboard, "z", "keyRead(Z);","");

The passed parameter to the keyRead function lets me know what's been pressed. Seems a very long way round to do this - but it works....

Nic.
#4
02/20/2007 (12:11 pm)
Was the anyKey event not sufficient for your purposes?
#5
02/20/2007 (5:52 pm)
Another alternative to this, would be to build your own 'OnKeyPress' Console Method in the C++ -- the ActionMap catches all the keypresses, and then checks to see if it should do something about the keypress ... you could bypass this and just call an 'onKeyPress' console function ... ;)
#6
02/21/2007 (7:38 am)
The proper solution for this is to use a GuiTextEdit Control as mentioned by David...it does all the work for you, and assigns the inputted information to any global variable you like.
#7
02/21/2007 (9:09 am)
Well - I had to do the hack cos I was on deadline and I can never find any documentation for the Gui. It works, it's simple.

Nic.