How to create an EventListener for a specific keyboard press?
Hello,
I have been trying to figure out how to switch my actionscript3 from a mouse click to a keyboard press. I'm new to Flash, but the problem I keep coming to is that I need to have 3 separate keys programmed in to do three seperate outcomes. I have messed around with eventListeners for keyboard presses, but I cannot figure out how to have flash listen for a specific key press and then do an action based on that specific key press.
Here is my actionscript. Any suggestions on how I can modify the mouse clicks to be keyboard presses, where key 's' = btn1 and triggers gotoAndPlay(2), 'g' = btn2 and triggers gotoAndPlay(3), 'k' = btn3 and triggers gotoAndPlay(4) as outline below. I also need my timer and writing to an exteral file to remain the same.
stop();
var startTime:Number=getTimer();
var elapsedTime:Number;
stream.writeUTFBytes("Item1,");
function BTN1Action(eventObject:MouseEvent) {
elapsedTime = getTimer() - startTime;
stream.writeUTFBytes("Tar1,");
stream.writeUTFBytes(elapsedTime.toString());
stream.writeUTFBytes("\n");
gotoAndPlay(2);
}
function BTN2Action(eventObject:MouseEvent) {
elapsedTime = getTimer() - startTime;
stream.writeUTFBytes("Tar2,");
stream.writeUTFBytes(elapsedTime.toString());
stream.writeUTFBytes("\n");
gotoAndPlay(3);
}
function BTN3Action(eventObject:MouseEvent) {
elapsedTime = getTimer() - startTime;
stream.writeUTFBytes("Tar3,");
stream.writeUTFBytes(elapsedTime.toString());
stream.writeUTFBytes("\n");
gotoAndPlay(4);
}
BTN1.addEventListener(MouseEvent.CLICK, BTN1Action);
BTN2.addEventListener(MouseEvent.CLICK, BTN2Action);
BTN3.addEventListener(MouseEvent.CLICK, BTN3Action);
-
Any assistance with this is greatly appriciated.