Note that there are some explanatory texts on larger screens.

plurals
  1. POAS3 KeyboardEvents not firing until after I've clicked on an onscreen button
    primarykey
    data
    text
    <p>So here's my setup:</p> <p>My document class is Main.as (it extends MovieClip). The following code is in my Main.as.</p> <p>I've declared all these variables in my class definition:</p> <pre><code>private var holder:MovieClip; private var leftButton:SimpleButton; private var rightButton:SimpleButton; </code></pre> <p>in constructor...</p> <pre><code>holder = new MovieClip(); addChild(holder); holder.addEventListener(KeyboardEvent.KEY_UP, handleKeyboardEvent); leftButton = new Arrow(); rightButton = new Arrow(); holder.addChild(leftButton); holder.addChild(rightButton); leftButton.x = 50; leftButton.rotation = 180; rightButton.x = 150; leftButton.y = rightButton.y = 50; leftButton.addEventListener(MouseEvent.CLICK, handlePaging); rightButton.addEventListener(MouseEvent.CLICK, handlePaging); </code></pre> <p>...outside of constructor...</p> <pre><code>public function handleKeyboardEvent ( e:KeyboardEvent ) { trace("got a keyboard event"); trace("e.charCode : " + e.charCode); trace("e.keyCode : " + e.keyCode); switch (e.keyCode) { case 37: //left arrow showPreviousMoment(); break; case 39: //right arrow showNextMoment(); break; } } </code></pre> <p>The two buttons are instances of an Arrow movieclip that I have in the library. The Arrow movie clip just has graphics in it (in the property dialog for it the base class is flash.display.SimpleButton).</p> <p>I want the user to be able to press the onscreen arrows OR the keyboard arrow keys to page forwards and backwards.</p> <p>So...when I test the movie or publish it as an app, pressing the left and right keyboard keys doesn't do anything. HOWEVER...AFTER I click on the screen arrows THEN the keyboard events are getting picked up!!! How come my keyboard events are not getting received initially? Why does clicking on the onscreen buttons make the 'holder' start receiving the events? </p> <p><strong>UPDATE</strong></p> <p>I've also just commented out all of my code EXCEPT for the instantiating the 'holder' and adding the listener. The KeyboardEvents are not being heard by the holder.</p> <p><strong>UPDATE...ANSWER</strong></p> <p>So I found out that if I want to globally hear the KeyboardEvent, I should register stage as the listener. So this did the trick:</p> <pre><code>stage.addEventListener(KeyboardEvent.KEY_UP, handleKeyboardEvent); </code></pre> <p>Info from Colin Moock's book:</p> <blockquote> <p>Keyboard-input events that trigger application-wide commands are typically handled globally, by listeners registered with Flash Player's Stage instance. By contrast, keyboard-input events that trigger a specific interface-element response are typically handled by listeners registered with the object that currently has keyboard focus.</p> </blockquote> <p>So I guess that in my initial version, when I clicked on the onscreen arrow button, I somehow gave keyboard focus to the 'holder;.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload