Not catching on key up event - actionscript-3

I have a flash component TextInput within a movieclip, added to stage, through as3.
The moment this mc is added to stage, it assigns keyboard event (key down and key up events), to the main stage (main app stage), and set main stage focus to main stage.
So far so good.
The problem is, when I type ENTER, I catch KEY_DOWN, but when I release key ENTER, my KEY_UP event is uncatched by my function assigned to that event.
Any body knows why?
It only works if I click the flash player.
(Code provided in comment)
private function initAdded(e:Event){
_main.stage.addEventListener(KeyboardEvent.KEY_DOWN,checkKeysDown);
_main.stage.addEventListener(KeyboardEvent.KEY_UP,checkKeysUp);
MovieClip(_main).setStageFocus()
}
private function checkKeysDown(e:KeyboardEvent):void {
if (e.keyCode == 13) {
trace('enter down')
}
}
private function checkKeysUp(e:KeyboardEvent):void {
if(e.keyCode == 13){
trace('enter up')
}
}

I'm going to guess that the KEY_UP event is being handled by the text area you are typing in, and it doesn't want to share, so it kills the event. Try:
_main.stage.addEventListener(KeyboardEvent.KEY_UP,checkKeysUp, true);
This causes your event listener to catch the event in the capture phase, before the other event listener cancels propagation of the event.

Related

keycode for MouseEvent?

So to my understanding, there are keycodes to represent the keystrokes, for example:
public function left(e:KeyboardEvent):void
{
if (e.keycode == 65)
{
leftKey = true;
}
}
I want to do something similar with this logic and apply it for Mouse Events. I've searched on Google but haven't found much results for Flashdevelop AS3. Would there be a keycode to represent Mouse Events?
For example:
stage.addEventListener(MouseEvent.MOUSE_DOWN, down);
stage.addEventListener(MouseEvent.MOUSE_MOVE, move);
stage.addEventListener(MouseEvent.CLICK, click);
public function down(e:MouseEvent):void
{
if (e.keycode == ?)
}
public function move(e:MouseEvent):void
{
if (e.keycode == ?)
}
public function click(e:MouseEvent):void
{
if (e.keycode == ?)
}
Thanks in advance!
When you fire a mouse event that has a listener attached to it, then the function associated with that listener will get called. Period. You don't need to then, again, check to see if the mouse event happened. That's the whole beauty of event listeners. Just to make sure you get it, I'll post an example.
stage.addEventListener(MouseEvent.CLICK,mClick);
Great. We added a listener to the stage. Now, any time you click anywhere on the stage, this listener will cause that mClick function to get called. Now we write the mClick function.
private function mClick(me:MouseEvent):void{
trace("me.target.x",me.target.x);
trace("me.target.y",me.target.y);
}
me is just a variable we chose to represent the event that triggered this function. Event is a class. A MouseEvent is a subclass of Event. So we are saying that me is an Event of sublcass MouseEvent and that this is the expected input for this function. If you were to try to call this function elsewhere in your code it would throw an error saying that this function expected some sort of input.
me.target is the thing that caused the event to get triggered. In this case it is the mouse, so me.target.x will be the x position of the mouse at the time that the mouse was clicked.
That is all there is to it. You have just confused yourself a little bit by trying to apply a specific solution to a different problem, namely, how to register key presses on a keyboard. They are handled slightly fundamentally differently. With a keyboard, we check listen for if the keyboard had a key pressed and then in the event handler, we determine which key was pressed. With mouse events we would have a different listener for mouse move, mouse click, left mouse click and middle mouse click. Since each of those specific events has their own listener, we don't have to evaluate which button was pressed in the handler:
public function left(e:KeyboardEvent):void
{
if (e.keycode == 65)
{
leftKey = true;
}
}
The e is the thing that caused the event. In this case, the Keyboard key. Each key has a keycode. But instead of writing a different listener for every single key on the keyboard, we can just write one listener and one function and then evaluate which key was pressed inside the function. As stated before, with the mouse, the left mouse button gets its own event listener, so no need (or ability) to check that in the event listener. There are properties like ROLL_OVER and MOUSE_UP and MOUSE_DOWN. Look at the documentation for a full list.

Rich Editable Text not dispatching Enter key event

I'm trying to catch when a user presses a "enter" key in a rich editable text field. It wasn't dispatching KeyboardEvent.KEY_DOWN so I found that it dispatches an "enter" key event. That seemed more appropriate but that is not getting dispatched either.
Here is my code:
editableRichTextField.addEventListener(FlexEvent.ENTER, commitTextEditorValues, false, 0, true);
Am I missing something?
NB: This is with Apache Flex 4.14.
I knocked up a sample application for this and it seemed that the RichEditableText does dispatch KeyboardEvent.KEY_DOWN. How are you catching it? The following code:
private function CCH():void
{
richEditableText.addEventListener(KeyboardEvent.KEY_DOWN, KeyDownHandler);
}
private function KeyDownHandler(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.ENTER)
{
Alert.show("Enter Key Pressed");
}
}
should trigger an alert (where the CCH method is the creation complete handler for the component/application.
You have to set multiline to false for the event to be dispatched. This is not helpful if you need to support multiple lines so you have to use #DaJobat's answer and listen on key down.
editableRichTextField.multiline = false;

My mouse clicks created and triggered my eventListener at the same time

I tried to achieve the following but failed with the code below:
Click mouse once to appear the box.
Click it again to disappear the box.
What happened is that when I fire the mouse.click event (by clicking), that triggered the "stage.addEventListener(MouseEvent.CLICK, boxGone)" event listener as well. At the screen there is nothing happened because I technically addChild and removeChild the box at the same frame.
I am guessing my initial click created and triggered the event listener at the same time.Is there anyway to avoid this from happening without changing my triggering event(mouse click)? below is the code:
public function ClassConstructor(){
addEventListener(MouseEvent.CLICK, onMouseClickHandler);
}
private function onMouseClickHandler(e:MouseEvent):void{
box.x = stage.mouseX;
box.y = stage.mouseY;
box.gotoAndPlay(1);
stage.addChild(box);
stage.addEventListener(MouseEvent.CLICK, boxGone);
}
private function boxGone(e:MouseEvent):void{
stage.removeChild(box);
stage.removeEventListener(MouseEvent.CLICK, boxGone);
}
Thanks in advance,
Sunny
Modify your first listener with:
stage.addEventListener(MouseEvent.CLICK, onMouseClickHandler);
The event goes from your main class to the stage, and you add the second listener in between, so it is called just after the function's closure. Another solution, to be sure, would be to call
e.stopImmediatePropagation();
This prevents any listener to catch the same event.

Flash AS3 making it so whenever a button is clicked on screen is like pressing a specific keyboard key

Hello I am working on a game in flash, in the mobile version of this app I am trying to make it so in a mousedown event on a button it will be equal to the Left, Right keycodes etc. being active?
Is there a way in AS3 for a keyboard key to be activated by other means such as mouse clicks?
leftButton.addEventListener(MouseEvent.MOUSE_DOWN, leftKeyPress)
function leftKeyPress(e:MouseEvent){
// Left Key is pressed
Keyboard.LEFT;
trace("trace statement");
}
This code doesent seem to work
EDIT: also this is important this class and the class that uses the keyboard(The player) are in two seperate classes, so Im trying to make it so that when this is click it is recognized as a key press on a keyboard
Sorry but It is all wrong. You can't trigger keyboard events like that. That is the way of capturing events. And your function is also a MouseEvent. Should be keyboard event. I Opened Flash after one year. Anyway. AFAI understand you are trying to disable keyboard until mouse click somewhere.
leftButton.addEventListener(MouseEvent.MOUSE_DOWN, leftKeyPress);
var keyEnabled:Boolean = false;
function leftKeyPress(e:MouseEvent){
keyEnabled = true;
}
key.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
function keyPressed(e:KeyboardEvent):void{
if(keyPressed){
//do whatever here
}
}
You can manually dispatch a keyboard event; however, is not the best design pattern:
dispatchEvent(new KeyboardEvent(KeyboardEvent.KEY_DOWN,
true,
false,
Keyboard.LEFT,
Keyboard.LEFT));
It would be better to establish two handlers:
addEventListener(KeyboardEvent.KEY_DOWN, moveKeyHandler);
addEventListener(MouseEvent.CLICK, moveMouseHandler);
From each handler, call a function that abstracts movement function from the handlers:
protected function moveKeyHandler(event:KeyboardEvent):void
{
/* switch statement for key */
moveLeft();
}
protected function moveMouseHandler(event:MouseEvent):void
{
/* switch statement for button target */
moveLeft();
}
protected function moveLeft():void
{
}

Skipping to next game level in AS3

I am in the process of building a game with around 20 levels. Now, as I was thinking of trying to add a "skip" option to the game with the space bar key. I have a little trouble, since holding down the spacebar invokes the KeyboardEvent.KEY_DOWN event multiple times.
The above scenario (Keydown and keyup) works just fine when I'm trying to move my player character on screen.
The one main concern that is lingering in my mind is "Is this going wrong because the KeyboardEvent listeners do get removed and added when changing levels?"
P.S: Adding the skip option as a button works just fine, but I'd really like to use Spacebar for the ease of use.
Listen for KeyboardEvent.KEY_UP as well, and keep a boolean that acts as a switch. If the keyboard is pressed AND the boolean is false, set the boolean to true and proceed. Then, when the key_up event callback is invoked, reset the boolean to false.
Does somethig like this work for you?
private function levelInit () : {
// ...
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownListener);
// ...
}
private function keyDownListener (e : KeyboardEvent) : void {
if (e.keyCode == Keyboard.SPACE) {
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownListener);
// add stuff to initiate the level skip
}
}
You could also call the stage.addEventListener from a setTimeout to delay adding the listener to protect things more.
Remove the key_down listener in the function and add a key_up listener. Then in the key_up function remove that listener and add back the key_down listener.