Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Fastest way to create a KeyboardEvent.ENTER submit function.

Guest
May 10, 2014 May 10, 2014

Hello, a submit button is on the stage next to a text input field. It must be possible to use both mouse and the enter key to press the button.

What's the fastest and easiest way to add an eventListener to the ENTER key, on the button?

TOPICS
ActionScript
461
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , May 11, 2014 May 11, 2014

cast it:

function handler(event:Event){

  
if((event is KeyboarEvent && KeyboardEvent(event).charCode == 13) || event.currentTarget == submitBtn)

     {}

}

Translate
Community Expert ,
May 10, 2014 May 10, 2014

add a keyboard event listener to the stage and mouseevent listener to your submit button.  They can both call the same listener function (use Event for the argument type).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 11, 2014 May 11, 2014

So it would have to be something like this? But now the MouseEvent won't work? Do I really have to create a duplicate of the function for both to work? Seems like a hassle..

submitBtn.addEventListener(MouseEvent.CLICK,handler);

stage.addEventListener(KeyboardEvent.KEY_DOWN,handler);
function handler(Event){

  
if(event.charCode == 13)

     {}

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 11, 2014 May 11, 2014

You should be able to expand the conditional in the one function to handle both cases using an OR (||), as in...

function handler(event:Event){

  
if(event.charCode == 13 || event.currentTarget == submitBtn)

     {}

}


Edit: repaired = to be ==

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 11, 2014 May 11, 2014

I worked pretty well, but it's giving me a dilemma problem.. I can't define the event-type (in the function) to evt:Event, because the keyCode then becomes an "undefined property". However if I change the event-type to evt:KeyboardEvent  -  keyCodeworks, but then the MouseEvent "on the other side" don't.

No compiler errors occur though, just in the output tab it says Error #1034, cannot convert MouseEvent to KeyboardEvent - every time I try to hit the sumbitBtn on the stage.

Note: I want to upload the code, but it's on a work-computer without internet nor the possibility to connect a usb-stick or anything...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 11, 2014 May 11, 2014

cast it:

function handler(event:Event){

  
if((event is KeyboarEvent && KeyboardEvent(event).charCode == 13) || event.currentTarget == submitBtn)

     {}

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 11, 2014 May 11, 2014

That worked very well! Thanks for helping!

On another subject, event is KeyboarEvent is new to me. I couldn't find any good explanations for it, on the web. Care to explain in short terms?

Ex.:  It seems like it's converting the event definition. Therefore why not convert it back on the other side of the logical OR operator?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 11, 2014 May 11, 2014
LATEST

It is not converting it, it is testing it.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 10, 2014 May 10, 2014

The fastest and easiest way to add a listener is to copy and paste the code.   You can make the Enter key perform the same actions as pressing the button but you cannot make the Enter key press the button (The Enter key cannot hover over the button either).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines