[Solved] How do you do "If a key, any key, on the keyboard is pressed"?

Hello there, 


I've been looking up the javascript for it and thinking that I'm gonna have to delve into the core.js file for this under Input. 


Still, I have to ask here if there's a simple way to do this. I just want an "If (Input.somethinganythingPressed) { }" so I can shorten and simplify my code in many places.
Loop through Input._currentState and see if there's any true values in there. 
Perhaps this is not the best way to go about it (and it might not even work) but have you tried "Input._pressedTime > 0"?


Again, I might just be talking nonsense here, but it might work.
Loop through Input._currentState and see if there's any true values in there. 


Perhaps this is not the best way to go about it (and it might not even work) but have you tried "Input._pressedTime > 0"?


Again, I might just be talking nonsense here, but it might work.



I tried both. Input._pressedTime > 0 doesn't work. I also tried this code that I found online for checking:


Function:


// Speed up calls to hasOwnProperty
var hasOwnProperty = Object.prototype.hasOwnProperty;

function isEmpty(obj) {

// null and undefined are "empty"
if (obj == null) return true;

// Assume if it has a length property with a non-zero value
// that that property is correct.
if (obj.length > 0) return false;
if (obj.length === 0) return true;

// Otherwise, does it have any properties of its own?
// Note that this doesn't handle
// toString and valueOf enumeration bugs in IE < 9
for (var key in obj) {
if (hasOwnProperty.call(obj, key)) return false;
}

return true;
}




Then call it in an if statement like so: 


if (isEmpty(Input._currentState) == false) { }




That didn't work either. 


I don't know how to loop through the _currentState either...
What I'd do to get a feel of how everything is set up is to do console.log() all over the place.


For example, if I'm not sure what _currentState is, then I'd either make an event that runs the script "console.log(Input._currentState)" or have something that logs currentState each time a button is pressed. For all I know, _currentState might be an integer (it's probably not).


But as for looping, do you mean you're not sure how to loop, or you're not sure what you'd be getting by looping through it? If it's the latter, try the console.log thing and see if anything occurs to you.
Well I hit F8 to bring up the console, typed in Input._currentState, and the info that it showed made me believe that it was an object. So I tried using that function above that I found online. 


Zalerinian is suggesting above that the object (if Input._currentState is an object...) be looped through...but there has to be a simple way of just seeing if there are any entries at all inside of the object. Maybe not...


 
What I'd do to get a feel of how everything is set up is to do console.log() all over the place.


For example, if I'm not sure what _currentState is, then I'd either make an event that runs the script "console.log(Input._currentState)" or have something that logs currentState each time a button is pressed. For all I know, _currentState might be an integer (it's probably not).


But as for looping, do you mean you're not sure how to loop, or you're not sure what you'd be getting by looping through it? If it's the latter, try the console.log thing and see if anything occurs to you.



I have to double post to reply because it's so soon after and I don't want to necropost. Your code of Input._pressedTime > 0 is working it's just not working when I implement it into my code. Thank you, and I'm gonna search for what's wrong with my own script.


EDIT: So _pressedTime doesn't work, because it counts up the milliseconds, then never resets to 0 when no key is pressed. You would need it to reset to 0 if no key is pressed.


Input._currentState looks like this - > Object { right: true } (if right is pressed), so ...


What I need is like 


if (Input._currentState is not empty)


And that's exactly what the loop above that I found on stackoverflow.com did. Here's the link: 


http://stackoverflow.com/questions/135448/how-do-i-check-if-an-object-has-a-property-in-javascript


Okay, big update on this problem and for anyone that wants to solve this...


You can use Mjshi's suggestion of "Input._pressedTime > 0", however you have to reset it back to 0 because it doesn't reset by itself. For example, if you press the up key and hold it and release it when the counter is at 100 (stands for milliseconds that the key was held), you will have to do "Input._pressedTime = 0" BEFORE you check for "Input._pressedTime > 0" in a conditional statement within an event, because it will just stay at 100 milliseconds.


Also, for anyone reading to solve this problem, you can use touchInput._pressedTime as well.


Thanks guys and happy coding. ; p


P.S, what I did in the end is like the following (warning, it is not aliased):


//Add _mousePressed to right click also.
TouchInput._onRightButtonDown = function(event) {
var x = Graphics.pageToCanvasX(event.pageX);
var y = Graphics.pageToCanvasY(event.pageY);
if (Graphics.isInsideCanvas(x, y)) {
this._mousePressed = true;
this._onCancel(x, y);
}
};
TouchInput._onMouseUp = function(event) {
if (event.button === 0) {
var x = Graphics.pageToCanvasX(event.pageX);
var y = Graphics.pageToCanvasY(event.pageY);
this._mousePressed = false;
this._onRelease(x, y);
}
if (event.button === 2) {
this._mousePressed = false;
this._onRelease(x, y);
}
};


this was thrown inside of a .js file in the plugin folder. This allows you to check for both left and right clicks using TouchInput.isTriggered() and TouchInput.isCancelled().


In my event I put this in the page before the conditional is checked: 


Script : TouchInput._pressedTime = 0;


Script : Input._pressedTime = 0;


Then on the page where it checks for input it's: 


If : Script : Input._pressedTime > 0 || TouchInput._pressedTime > 0


    do specific key checks in here. Now in here you can go "If, then, else" where as       before you would have to have tons of conditions. 


End

Original thread: https://forums.rpgmakerweb.com/threads/solved-how-do-you-do-if-a-key-any-key-on-the-keyboard-is-pressed.61862/