View Full Version : Keyboard Help.
MC123
04-28-2007, 04:32 AM
Hey guys, I was wondering if there was a way to sense, from the main timeline, if a key is pressed.
If you can help thanks.
~Mc
Logic
04-28-2007, 07:13 AM
Have you looked at the Key class? Open the Help Menu in Flash and find the Key class in the ActionScript 2.0 language reference. This should give you everything you need.
Specifically, what you would use would be:
var keyList:Object = new Object();
keyList.onKeyDown = function()
{
if (Key.isDown(##))
{
//...some code here
}
}
Key.addListener(keyList);
That's the listener method. You can also do it through the MovieClip.onEnterFrame() method:
this.onEnterFrame = function()
{
if (Key.isDown(##))
{
//...some code here
}
}
Replace the ## signs in those conditions with the key code for the key you want to detect a press of. Look up the key codes in the Flash Help Menu.
onClipEvent(keyDown){
//something happens
}
If you wanted to do it on a MovieClip. This checks for any key being pressed down.
Master Of Web
05-03-2007, 08:19 PM
Yeah, all you have to do is this:
onEnterFrame = function(){
if (Key.isDown(key.BLAH)){
//code that makes something happen...
}
}
But I guess that was pretty much the same as Logic's explanation, so...
truimagz
05-03-2007, 11:00 PM
you shoul look at MATTS key generator.
I'd have to upload is somewhere else, my host is having abnormal problems and is down.
arkhan
05-04-2007, 05:34 AM
var keyList:Object = new Object();
keyList.onKeyDown = function(){
K=Key.getAscii();
trace(K+":"+chr(K));
}
Key.addListener(keyList);
this is simple enought...you make a listener object with the functions you want to keep track of..like onKeyDown, onKeyUp.
in the function you probably want to know witch key was presed..so, depending on what you want you use getAscii or getCode..Ascii will diference keys like ' from " and 's' from 'S' and so on..while the getCode will know when you pressed shift alone, but 's' would be the same as 'S'
in AS3, I think its even easyer to understand..
package {
import flash.events.KeyboardEvent;
public class KeyboardEvt extends Sprite{
public function keyboardEvt{
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);//its (e:Event, f:function)
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
}
private function keyDownHandler(e:KeyboardEvent):void{
trace(e.keyCode);
}
private function keyUpHandler(e:KeyboardEvent):void{
trace(e.keyCode);
}
}
}
in this case it uses more code..true..but its also much simpler than making a listener and adding it to the Key class..
most the code there is just to make a class and a main function for the compiler..the actual code is, just saying the stage will watch for keyboard events KEY_DOWN and KEY_UP from the keyboard, and execute the given functions declared later on..
vBulletin® v3.7.3, Copyright ©2000-2010, Jelsoft Enterprises Ltd.