Any way to key JUST keydown and JUST keyup events?

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.

Is there an easier way?

Late Update: It's buggy too, so best option at this point is a hybrid, see http://answers.unity3d.com/questions/51293/mac-keys-not-showing-up-or-very-strangely

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 :wink:

// 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..

here is a more elegant solution:

	void OnGUI()
	{
		Event e = Event.current;
		if (e.isKey && e.type == EventType.KeyUp){
			switch(e.keyCode){
				case KeyCode.Space: Debug(); break;
				case KeyCode.UpArrow: ShiftUp(); break;	
			}
		}
	}

Hey,

You can use :

if(Input.GetKeyDown("c")){ 
            //do stuff
    }

if(Input.GetKeyUp("c")){
           //do stuff
   }

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!

i think what mattyk means is

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.

Coming in late to this party :slight_smile:

  1. The documentation for Event.KeyDown does mention that it’ll behave according to the repeat settings.
  2. 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?)
  3. 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 :slight_smile: