Skip to main content
Participating Frequently
January 2, 2022
Question

Problem executing mouse event and key board event simultaneously

  • January 2, 2022
  • 1 reply
  • 551 views

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 has been closed for replies.

1 reply

JoãoCésar17023019
Community Expert
Community Expert
January 2, 2022

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

Participating Frequently
January 2, 2022

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

Colin Holgate
Inspiring
January 2, 2022

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.