Skip to main content
Participating Frequently
November 22, 2012
Question

How to create an EventListener for a specific keyboard press?

  • November 22, 2012
  • 1 reply
  • 833 views

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. 

This topic has been closed for replies.

1 reply

Participant
November 22, 2012

Assuming you want to monitor key press on the button BTN1, you can do following:

// Add a key up event listener on the button (or on the source where the key press needs to be captured)
BTN1.addEventListener(KeyPress.KEYUP, BTN1KeyUpAction);

// BTN1KeyUpAction sample, you can modify this
function BTN1KeyUpAction(e:KeyboardEvent):void {
    if(e.keyCode == Keyboard.S) {
    gotoAndPlay(2);
  }
}
Participating Frequently
November 22, 2012

Where would I add the timer and stream.write bits?

Participant
November 22, 2012

If your requirement is to trigger BTN1 click event when 's' key is pressed, and so on for other keys, you can try the following

// BTN1KeyUpAction sample, you can modify this
function BTN1KeyUpAction(e:KeyboardEvent):void {
    if(e.keyCode == Keyboard.S) {
    var event:MouseEvent = new MouseEvent(MouseEvent.CLICK);
    BTN1.dispatchEvent(event);
  }
}