Key press

7 posts

Flag Post

How can i make it so that when i press down a key rather than the action happening every frame, it simply happens once per click?

Thanks in advance.

 
Flag Post

you press the button really really really really really fast. I have a vague grasp on that subject, I would confuse everyone and myself by trying to help you, sorry…

 
Flag Post
on(keyPress(KEYCODE)){
//actions
}
 
Flag Post

Here is what I do; I put in a timer which increases by 1 each frame, and instead of

if(Key.keyisdown(KEYCODE))

{actions}

I put

if(Key.keyisdown(KEYCODE) && timer > 10)

{

timer = 0

actions

}

Disregard the capitalization; I think I got it wrong.

Anyway, this makes the button take effect every ten frames. So unless the person is holding the button for 10 frames (in which case they are probably doing it on purpouse) they only get 1 event.

 
Flag Post

This isn’t the exact actionscript, it’s a concept for you to look into; all of this stuff should be in the AS help files somewhere…

Basically a MovieClip or Button has an onKeyDown and onKeyUp handler.

this.onKeyDown = function() {
  if( allowable ) {
    actions
  }
  allowable = false
}

this.onKeyUp = function() {
  allowable = true
}

Hope it helps.

 
Flag Post

To add to what IndieFlashArcade said, you must also add whatever the onKeyDown or onKeyUp event is being called from as a listener for the Key class. Using his example,

this.onKeyDown = function() {
	if( allowable ) {
		actions
	}
	allowable = false;
};

this.onKeyUp = function() {
	allowable = true;
};

Key.addListener(this);

Additionally, if you want to check for a specific key, you can use the getCode() method of the Key class, which returns the key code of the key pressed as a number. Again, using his example, it would be something like…

this.onKeyDown = function() {
	if( allowable && Key.getCode() == KEYCODE ) {
		actions
	}
	allowable = false;
};

…where you replace KEYCODE with the key code of the desired key. Most key codes you would find yourself using are listed here.

 
Flag Post

Thanks, facelesscoward, I guess making that addListener call is pretty essential to note.

I think that I would do the Key.getCode test as a switch statement. I hate to spend too much time on such a simple topic, but for the sake of it, here is a better, more complete version:

var allowable:Boolean = true;

this.onKeyDown = function() {
    if( allowable ) {
        switch(Key.getCode()) {
          case 65:                        //'a' key pressed
            //do something
            break;
          case 66:                        //'b' key pressed
            //do something
            break;
        }
    }
    allowable = false;
};

this.onKeyUp = function() {
    allowable = true;
};

Key.addListener(this);

You could probably find a better way, even, but that should solve the problem without messing with intervals.