Skip to main content
Participating Frequently
Jan 2, 2022 4:47 AM UTC
Question

Problem executing mouse event and key board event simultaneously

I have a button which does play/pause animation using mouse event. Now I wanted to do the same thing with spacebar/keyboard, so i scripted (AS3) keyboard event as well. Now the problem is buttons work fine, keyboard works fine if only keyboard is used but if i play using button and then try to pause using keyboard it is not executing. Please help. 

This topic is closed to new replies. Start a new post to keep the conversation going.

10 replies

JoãoCésar17023019
Community Expert
Community Expert
Jan 2, 2022 1:34 PM UTC

Hi.

 

Here is a suggestion:

import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;

function playAnim(e:MouseEvent):void
{
	if (yourAnim.isPlaying)
		yourAnim.stop();
	else
		yourAnim.play();
}

function keyDownHandler(e:KeyboardEvent):void
{
	if (e.keyCode == Keyboard.SPACE)
		playAnim(null);
}

yourButton.addEventListener(MouseEvent.CLICK, playAnim);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);

 

Regards,

JC

Translate
Participating Frequently
Jan 2, 2022 1:56 PM UTC

Hi JC thanks for the suggestion. But can you please tell me what is (null) and how does that function. Thank you

Translate
Colin Holgate
Inspiring
Jan 2, 2022 2:03 PM UTC

He is calling a function that normally is triggered with a mouse action. This line is expecting that mouse event:

function playAnim(e:MouseEvent)

 

To call the function from somewhere other than a mouse action, he uses playAnim(null), just to keep the function happy. Without null there would be an error message.

 

Another solution to the same issue is this:

function playAnim(e:MouseEvent=null)

then if you call it with playAnim(), the missing parameter is filled in with a null value.

Translate