Skip to main content
Participant
January 17, 2011
Answered

Probably a simple keyboard event question...

  • January 17, 2011
  • 2 replies
  • 586 views

I'm new to AS3 and can't figure this out...

I want to add the function nextClip(); away from a button to the spacebar. So when I click the spacebar, it runs the nextClip();

btnNext.addEventListener(MouseEvent.CLICK, playNext);

function playNext(e:MouseEvent):void {

nextClip();

index = (index + 1)%(clips.length);

}

I can figure out how to do it with any key press, but not with just the spacebar.

function checkKeysDown(event:KeyboardEvent):void{

nextClip();

index = (index + 1)%(clips.length);

}

Thanks in advance for any help.

This topic has been closed for replies.
Correct answer Manno_Bult

Take a look at the keyCode propoerty of a KeyboardEvent. Compare it to the Keyboard.SPACE constant. If they'er equal, the spacebar was pressed:

import flash.events.KeyboardEvent;

import flash.ui.Keyboard;

stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKey );

function checkKey( event:KeyboardEvent ):void

{

if( event.keyCode == Keyboard.SPACE )

{

trace( "Space!" );

}

}

2 replies

kglad
Community Expert
Community Expert
January 17, 2011

:


function checkKeysDown(event:KeyboardEvent):void{

if(event.keyCode==32){

nextClip();

index = (index + 1)%(clips.length);

}

}


baggio16Author
Participant
January 17, 2011

Awesome. I get it. Thanks guys.

Manno_BultCorrect answer
Inspiring
January 17, 2011

Take a look at the keyCode propoerty of a KeyboardEvent. Compare it to the Keyboard.SPACE constant. If they'er equal, the spacebar was pressed:

import flash.events.KeyboardEvent;

import flash.ui.Keyboard;

stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKey );

function checkKey( event:KeyboardEvent ):void

{

if( event.keyCode == Keyboard.SPACE )

{

trace( "Space!" );

}

}