Game Development Community

Implement keyboard key that responds as long as it is held down

by Yujiao Guo · in Torque Game Engine · 03/08/2008 (1:19 pm) · 3 replies

Hi:


Anyone has any idea about implementing keyboard key that responds as long as it is held down?
For example, the following code works everytime you press the key "t". However, I want to advance the progress bar as long as the key is pressed down and not released, but as soon as it is released the progress is set to ZERO.



function advance(%val)
{
TeaBagLoadingProgress.advance();
}
moveMap.bind(keyboard, "t", advance);


function TeaBagLoadingProgress::advance(%this)
{
if((%this.progress += 0.01) >= 1) return;
TeaBagLoadingProgress.setValue(%this.progress);
}


new GuiProgressCtrl(TeaBagLoadingProgress) {
profile = "GuiProgressProfile";
horizSizing = "right";
vertSizing = "top";
position = "128 262";
extent = "262 25";
minExtent = "8 8";
visible = "1";
progress = "0";
helpTag = "0";

new GuiTextCtrl(TeaBagLoadingProgressTxt) {
profile = "GuiProgressTextProfile";
horizSizing = "right";
vertSizing = "bottom";
position = "-4 3";
extent = "262 20";
minExtent = "8 8";
visible = "1";
text = "Fat Cat Studio. Enjoy!";
maxLength = "255";
helpTag = "0";
};
};
========================
yujiaoguo.blogspot.com

#1
03/10/2008 (6:06 am)
Why not just assign the %val to a global variable? That way as long as the key is held down the global will equal "1", and when you release the key the global will equal "0".

AKA:
function advance(%val)
{
$advance = %val;
TeaBagLoadingProgress.advance();
}

I don't see why that wouldn't work, maybe not the best solution though...


Nathan
#2
03/10/2008 (1:37 pm)
I really appraciate your help :)
Thanks Nathan!
========================
yujiaoguo.blogspot.com
#3
03/10/2008 (2:07 pm)
No problem. And I assume torque script supports recursion, so inside "TeaBagLoadingProgress::advance" I'd just check to make sure the global variable is "1" and then call itself.


Nathan