Key press / release event?

is there an event you can set like on press’k’ and release’k’ where an event would happen on press of the letter k and then on release of it … kinda like mouse up and down states?

someone … anyone?

You can’t do the letter exactly, you can only do the keyCode…

So for “k” (lowercase) it would be…

_root.onEnterFrame = function() {
	if (Key.isDown(75)) {
		trace("k pressed");
	}
};

If you need this for other letters, I wrote a script a while back to help find out. Just copy and paste this code in a blank keyframe in a new document.

keyListener = new Object();
keyListener.onKeyDown = function() {
	keypressed = Key.getCode();
	trace(keypressed);
};
Key.addListener(keyListener);

When you test the movie, you can press a key on the keyboard and it will give you its value.

I asked more or less the same question here yesterday, but I don’t think I had a positive answer. It should be doable with a bunch of flags though. But I don’t think there’s an event to detect a “release’k”.

pom :cowboy:

Yeah I saw your question Ilyas. And I had no clue as to the answer of that one (if it was the missile one you were talking about).

Hey Ilyas, I kinda got a bit bored as I was sitting here, so I just decided to whip up something in AS.

I was determined to find a way to detect what letter was pressed instead of determing the keycode and going by that.

I think I found it. You are the AS master :wink: so I was wondering if you could look it over for me.

keyListener = new Object();
keyListener.onKeyDown = function() {
	keyPressed = String.fromCharCode(Key.getCode());
	keypressed == "K" ? trace("k pressed") : trace(keyPressed+" was pressed");
};
Key.addListener(keyListener);

Just add that to a frame.

I added the if/else statement in there (i know it is in ?: form, but I got used to that in Javascript and it kinda carried into Flash :slight_smile: just so you could see how it would look.