How to detect if the delete key was pressed in Actionscript 3? - actionscript-3

How do I determine if the delete key was pressed using actionscript?
addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
...
function onKeyUp(event:KeyboardEvent):void
{
trace(event.keyCode);
}
The above code yields no value when delete, backspace, enter, and other command keys are pressed. However, arrow keys do yield values.

this.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
....
function onKeyPressed(event:KeyboardEvent):void
{
if (event.keyCode==Keyboard.DELETE) {
.....
}
}
it's workin nice...
But if you test movie from Flash, it will not work, so export to swf and test....

Just guessing you are using the TEXT_INPUT event, this doesn't work for delete and backspace. To catch those ones you can add an eventListener on the stage and listen to a KeyboardEvent.

Code will work fine if the display object that you attached the listener is in focus. For global listening, as Theo said, you have to attach the listener to the stage. Accessing stage from an object that's not yet added to the display list will result in null error. Do it in the ADDED_TO_STAGE event handler to be safe.

Old thread, but if anyone gets this far: in the Flash Player inside the IDE, these keys are associated with shortcuts. When you test your movie, choose Control>disable keyboard shortcuts in the player and you'll get the events back.

Related

Triggering button event (or atleast appear to) via keyboard press AS3

Attempting to make a piano in AS3 and so far I have 2 methods of triggering a note, 1 via keyboard and 1 via mouse-click. When I click the key, it changes colour to indicate that the key has been pressed. Unfortunately , I can't find a way for the keyboard event to trigger the button press, is it possible and how would I do it?
It doesn't need to be an actual button press, it can instead just appear to be.
EDIT
The button is a button object, using the GotoAndStop causes the following error code:
"project.as, Line 13, Column 7 1061: Call to a possibly undefined method gotoAndStop through a reference with static type flash.display:SimpleButton."
Thanks in advance.
The easiest way is to extract the piano key press logic into its own function, and then call that function from two events.
function pressedMyPianoKey(){
// Do something like pianoKey.gotoAndStop(2) to show a new graphic
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
function keyDown(event:KeyboardEvent):void
{
pressedMyPianoKey();
}
pianoKey.addEventListener(MouseEvent.CLICK, click);
function click(e:MouseEvent){
pressedMyPianoKey();
}

Navigation from frame 1 to frame 2 renders event listeners non-functional in flash/actionscript3

I am new to actionscript and flash, and I have been unable to find a good explanation for why I cannot get this pretty basic code to work. I am using flash cc with actionscript 3. I have two frames on my timeline, one labeled "startFrame" and another labeled "newFrame". In "startFrame", I have a button with the instance name "btnStart". There is an event listener that listens for a mouse click on btnStart and then calls a function that goes to the next frame. Here is the code for "startFrame":
stop();
//When "btnStart" button is clicked, call the beginSession function.
btnStart.addEventListener(MouseEvent.CLICK, beginSession);
function beginSession(event:MouseEvent):void
{
//Remove event listener??
//btnStart.removeEventListener(MouseEvent.CLICK, beginSession);
//Go to newFrame.
gotoAndStop("newFrame");
}
I thought initially I should remove the event listener in the function after it is called, but it doesn't seem to work whether I remove the event listener or not. Here I have it commented out. In the "newFrame" frame, I have an event listener that listens for a key press and then calls a function that returns the key and character codes for that given key. That code is here:
stop();
//show that newFrame has been reached.
trace("newFrame has been reached");
//Add event listener for a key press to the stage.
stage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown);
//function that returns the key pressed and the character code.
function reportKeyDown(event:KeyboardEvent):void
{
trace("Key Pressed: " + String.fromCharCode(event.charCode) + " (character code: " + event.keyCode + ")");
}
When I test the movie, the button press works and "newFrame" is reached (the "newFrame has been reached" trace is found in the output). But it does not seem that the event listener for the key press is working. However, if I create a project that only has "newFrame" by itself (no other frames in the timeline), it works just fine when I test the movie. Only after I add in the startFrame and the button navigation do the event listeners seem to no longer hear the key press event in the second frame. Also, I am not getting any compile errors. What am I missing here?
Disable keyboard shortcuts under view while testing the application.

How to capture all keyboard input to a custom element?

Here is my problem: I've got a swf loaed inside my loader an in that swf I have a keylistener:
stage.addEventListener(KeyboardEvent.KEY_DOWN, this.__onKeyDown, false, int.MAX_VALUE);
Now I'm adding a TextInput to this stage and I would like this input to catch all keyboard events while I'm focusing it. Is it possible to do so that native __onKeyDown will not fire until my TextInput has lost focus?
Thank you for your answers and sorry for my bad english.
You could give your listener higher priority (which you are), and stopAllPropogation in your handler. I've never tried this with an embedded swf so if it doesn't work right away, you could also try listening to the event in the capture phase (third parameter in addEventListener).
function __onKeyDown(e:KeyboardEvent):void {
e.stopImmediatePropagation();
//rest of you handler code here
}

Simulate mouse click AS3

I'm having trouble simulating a click call to a button(displayObject) thats generated via an API call( youtube as3 API). I have not seen any mention of security reasons as to why I can not simulate a click as long as something is registered with a click handler.
Basically I checked to make sure the button made is listening to a mouse click event with:
trace(generatedButton.hasEventListener(MouseEvent.CLICK)) which returns true
I proceed to than call this:
generatedButton.dispatchEvent( new MouseEvent(MouseEvent.CLICK, true) );
And nothing happens yet if I physically click the button it works. Is there some security measure that prevents something from being fake clicked unless its origin is strictly from the system mouse?
I even set a timeout call on the click function and moved my cursor over the button and let it fire in case it was an issue of the mouse having to over the object but still nothing. I am kind of stumped at this point.
Any help would be appreciated!
Ok well I made a quick FLA and wrote this code and the dispatch Mouse event works perfectly..
import flash.events.MouseEvent;
myButton.addEventListener(MouseEvent.CLICK, myClickHandler);
function myClickHandler(e:MouseEvent):void
{
trace("clicked");
}
setTimeout(function()
{
myButton.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
},2000);
YouTube api must block against something like this
The following code works fine in my local sandbox.
btn.addEventListener( MouseEvent.CLICK, test )
function test( e:Event ):void{
trace('asdf');
}
btn.dispatchEvent( new MouseEvent( MouseEvent.CLICK ) );
So I can only come to 2 conclusions.
First is a security issue. Flash has some security issues with click events triggering the FileReference class where it can only be used if the stack has a user click in it. This issue may carry over to any artificial dispatching of that event. It works in my sandbox because the restriction doesn't apply here.
The second one is you are dispatching the event to soon and the buttons listener hasn't been added to the button from the api.
In this case try calling the dispatch after the stage render event has been called.
stage.addEventListener( Event.RENDER, test )
function test( e:Event ):void{
btn.dispatchEvent( new MouseEvent( MouseEvent.CLICK ) );
}
Just my guess anyway.
instead of doing a dispatchEvent, which will eventually call a method anyway, just call the method and since its expecting a MouseEvent, do yourClickMethod(null)
You can't simulate click events on a server, it is because security issues. Instead of dispatchingEvent you can call method directly. I ll look for the description but its illegal. You can check firefox firebug logs for error description.

Double right click mouse event in AS3?

It it possible to catch a double right click event in Actionscript 3?
It is with AIR. You can't capture RIGHT_CLICK in regular AS3, unfortunately.
There is, however, always the possibility of capturing right click events in JavaScript, and using ExternalInterface to call an event handler in the Flash program. See this blog, for example.
Actually you can catch RIGHT_CLICK at the moment :
You need player >11.2
Add additional compiler options -swf-version=19
Use this code :
stage.doubleClickEnabled = true;
stage.addEventListener(MouseEvent.RIGHT_CLICK, handler);
In handler you can save time of previous click, compare with second click and if it`s shorter than 0.5 seconds dispatch your own RIGHT_DOUBLE_CLICK event.