I'm trying to determine when keys are really going down and really going up. They Event stuff of course has these events, but, as it says in the docs for KeyDown:
"This event is sent repeatedly depending on the end user's keyboard repeat settings."
So I'm getting a whole slew of 'keydown' events, just by holding a key down. This is false for my purposes, because the key is already down.
I could make an array to hold key up/down status for all keys, then have it ignore key-down events until it sees a key-up for each key.
Am i right in assuming that if you changed the options on your keyboard to single press events and not press/hold that you would achieve the effect you want? eg in a word document if you hold a key 11111111111111 it repeats, with your change of settings it would only do it once no matter how long you held it down? Some of the answers here seem to mislead my understanding of the question. Seriously though, you're a pro and there are only a few people who have a higher rep than you, answers to your knowledge-filled question are hard to come by.
Thanks. Yes, I don't ask questions lightly. Yes, my computer is set to repeat, but I can't change that on everyone's machine. I was hoping there was another option (like Update/GetKeyDown, but more like GetAnyKeyDown with a parameter telling which key went down, or some option to set like 'Input.autoRepeat = false')
edit
Since the links i posted don’t work anymore (even the question id has changed, probably happend when UA switched to qato) here’s a working solution which is pretty straight forward
// C#
void OnGUI()
{
Event e = Event.current;
if (e.type == EventType.KeyDown)
{
if (Input.GetKeyDown(e.keyCode))
{
Debug.Log("Down: " + e.keyCode);
}
}
if (e.type == EventType.KeyDown)
{
Debug.Log("Up: " + e.keyCode);
}
}
You can’t prevent the KeyDown event from happening at the systems repeat rate. Even on windows you’ll get a WM_KEYDOWN for each repeated key. However the message contains a bit-flag if the key was down previously. Unfortunately this information isn’t transferred into the Event system.
However in combination with Input.GetKeyDown you can easily filter out the repeated events.
var KeyDown:boolean = false;
function Update () {
if (Input.GetKeyDown("c")) {
(if !KeyDown) {
doSomething
KeyDown = true; //turns off the loop
}
if (Input.GetKeyUp("c")) {
(if KeyDown) {
doSomething
KeyDown = flase; //turns off the loop.. again
}
}
That is a quick work around..
Would this even compile? I've not seen this syntax before.
though.. once you trigger the false statement; it will stop the lines of 'doSomething' .. so make a method and in that method, after it does what it needs todo, add KeyDown = true;
Yeah, this is basically what I meant, but since I need to monitor all keys, I'd have to have an array of KeyDown to check thus. I'm going ahead with that unless/until something simpler comes along. I'll vote for an option on the feedback forum (like that will ever happen!)
Goodluck man, is there any way you can go about fixing your keyboard issue, GetKeyDown should only fire on one frame. Otherwise, Im glad this helped and accept an answer so people can see its been solved. Matty
Even better though, you can use axes if these are game controls (especially if used constantly) with Edit > Project Settings > Input
Then:
Input.GetAxis("AxisName")
And when all else fails (which it should not here), you can always result to good old true or false (or as I like 1=true , 0=false) such as:
//Your trigger happens:
ItsGoing = 1;
if(ItsGoing == 1){
//Only do this when its 1, then reset back to 0
ItsGoing = 0;
}
Hope this helps!
The problem he was having is that his PC sends KeyDown every frame due to his PC setup, unlike normally it would only send on the first frame it happens.
var KeyDown:boolean = false;
function Update ()
{
if (Input.GetKeyDown("y"))
{
if (KeyDown == false)
{
Debug.Log ("Key is being pressed");
KeyDown = true;
}
}
if (Input.GetKeyUp("y"))
{
Debug.Log ("Key has been unpressed");
KeyDown = false;
}
}
but like you mentioned this will have to be specified for each key giving you possibly 20-30 variables and copies of this script, which doesnt sound like a day of fun making. I'l be watching via favorites to see if you get a hardcode solution.
The documentation for Event.KeyDown does mention that it’ll behave according to the repeat settings.
The documentatino for Input.GetKeyDown doesn’t say anything about that. Not sure if they just missed on that or whether that may actually work? (can you use polling based input check instead of event based?)
If all else fails, since you’re after keyboard input, if you’re on Windows you may be able to use native win32 input functions to get what you want.
From the docs here, it seems that the WM_KEYDOWN message provides the data to distinguish whether this is a real key down or a repeated one.
I am not certain whether GetKeyDown will behave the same or not, but it’s worth checking
Thanks, not sure what you mean though. I really need to monitor the whole keyboard, and I'm trying to avoid testing each and every key explicitly.
– DaveAAm i right in assuming that if you changed the options on your keyboard to single press events and not press/hold that you would achieve the effect you want? eg in a word document if you hold a key 11111111111111 it repeats, with your change of settings it would only do it once no matter how long you held it down? Some of the answers here seem to mislead my understanding of the question. Seriously though, you're a pro and there are only a few people who have a higher rep than you, answers to your knowledge-filled question are hard to come by.
– anon61858128Thanks. Yes, I don't ask questions lightly. Yes, my computer is set to repeat, but I can't change that on everyone's machine. I was hoping there was another option (like Update/GetKeyDown, but more like GetAnyKeyDown with a parameter telling which key went down, or some option to set like 'Input.autoRepeat = false')
– DaveAThanks Everyone! I really needed this one. Super helpful!
– zulubo