Game Development Community

Its there something like "KeyPressed" in TScript?

by Ehrlich · in Torque Game Builder · 08/01/2007 (1:05 am) · 5 replies

Hello there again...

this may sound like a n00b question, but i've read some thru the reference guide, and havent found what im looking for.

im looking for a way to detect if certain (any key), its being pressed... somethin like this:

while (keypressed)
{
//some code here
}

problem is, i have a some keys binded to certain script functions, and they do work, but if i maintain the key pressed, the function keeps goin on and on and on... thats ok with me, but i'd like to stop that (w/o releasing the key) at some time.

like i said, i might have overlooked it in the reference guide, but i need to control somethin that way, but havent found the way yet... anyone has an idea of how to do that?

About the author

Computer Science Engineer, with interest in Graphicl Computing, unconventional I/O devices and Artificial Intelligence (for games)


#1
08/01/2007 (2:31 am)
No there is not.

TScript is event based, with what you plan to do there, you could cause serious problems to the codeflow.
what you would do is use the onKeyDown etc callbacks and put your code there according your needs (if you need to have something done continously, you could set a global variable and call a schedule, which repeats as long as the variable is set.
#2
08/01/2007 (2:50 am)
Damn... i should of thought on that callback... its just the one i was looking for...

yeah, i know TGB its event based, its just that sometimes u just cant stop thinkin the secuential programmin way... thx for the heads up.
#3
08/05/2007 (8:40 pm)
Well, the solution is simpler than that, you can bind a function for when a key is pressed, but also for when the key is released, example :
globalmap.bindCmd(keyboard, "a", "keypressed();", "keyreleased();");
#4
08/06/2007 (11:58 am)
Combine the two pieces of advice that Marc and Benjamin here have given you, and you should be able to mock up a reasonable 'while (keypressed)' substitute.

Start out by adding to onKeyDown. The basic idea here is you want to set a flag to signal that your 'while' is active. Do whatever you need to do here, but make sure that something gets passed so that the next part can pick up the pieces.

Finish in the onKeyUp. When the key is let up, check to make sure that the above 'while' flag was set, if not, just exit out. If so, unset the flag and introduce any wrap up to stop whatever action/s your While was supposed to do.

I know that's a little vague above, but I don't really know specifically what you're trying to do. I'm sure you should be able to figure out how to apply it to your project.

Best of luck!
#5
08/06/2007 (2:23 pm)
Its my solution ! :

define a global var like $Key_pressed ( key its ur key !! ) and with this code :

globalmap.bind(keyboard, "key" , change_key );

function change_key(%val)
{
if (%val) {
$$Key_pressed = true;
} else {
$$Key_pressed = false;
}
}

makes it true when u press it and false when u relese the key .

so every time u want check that key is pressed check $Key_pressed ...