How to capture all keyboard input to a custom element? - actionscript-3

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
}

Related

How to detect menu button press in TVOS app using TVJS

I need to know when a document(screen) is popped off the stack in an Apple tvOS app. I thought detecting the Menu button press would be the simplest way, but I'm using TVJS and have not been able to figure out how to write the event handler.
Please help me write an event handler that will fire on document removal, menu button press or offer an alternative solution.
Subscribe to the event unload - it's triggered whenever a page disappears after being popped from the stack:
doc.addEventListener("unload", Presenter.onUnload.bind(Presenter));
[...]
onUnload: function(event) {
console.log("onUnload");
},
There is such thing of a handler for onDocumentRemoval or similar. What you can do, instead, is create a global select handler:
doc.addEventListener("select", self.doThing.bind(self));
And then check if the fired event comes from one of the buttons used to remove an element of the stack (let's suppose those buttons have a class named delete:
doThing: function(event){
var element = event.target;
if (element.getAttribute("class").contains("delete")){
//enter code here
}
EDIT 1:
I found the possible events the TVMLKit handles (I know it is in Swift/Objective-C, but the events are the same):
TVElementTypePlay
A play event has been dispatched.
TVElementTypeSelect
A select event has been dispatched.
TVElementTypeHoldSelect
A hold event has been dispatched.
TVElementTypeHighlight
A highlight event has been dispatched.
TVElementTypeChange
A change event has been dispatched.
Those events are only attachable to a template as far as I could test. I guessed the change event would be perfect if I could attach it to the navigationDocument to listen for changes, but those two options won't work and both fire errors:
Attached to the global:
navigationDocument.addEventListener("change", function(event){console.log(event)});
Attached to the documents array:
navigationDocument.documents.addEventListener("change", function(event){console.log(event)});
There is no built-in method for those above to listen for any change. The event, though, will work on a template listening to internal changes. But it won't fire when the template is pushed to or popped from the stack.
I am guessing you will need to re-design your app in order to achieve what you are looking for.

Flash / AS3 - How to Disable Child SWF Mouse Clicks Without Disabling Mouse Hover Events

I have a flash browser application that loads in a child swf using a Loader object.
The child swf has a mouseclick listener that calls nativateToUrl, and opens a new web page. It also contains mouse hover events that make things in the swf move around.
This application that does the loading is like a container for the child swf and should open a different web page when it is clicked. So essentially I want to suppress the child swf mouseEvent and listen for the mouse click on the container swf, opening the new URL.
I have heard of other people putting an invisible sprite over the original content so that the child swf doesn't actually catch a mouseclick event. However, this won't work in my case because then it doesn't get the mouse hover events either. I want the mousehover events to go through and make the things in the child swf move but also suppress the click event.
So, for all the flash masterminds out there... is this possible? And how can I do it?
:) thanks.
If you want to suppress all MouseEvent.CLICK events from reaching a loaded SWF, you can do the following:
Let's assume you have the following setup:
var loader:Loader = new Loader();
loader.load(new URLRequest("child.swf"));
addChild(loader);
What you can do, is listen on the capture phase for a click event on the Loader:
loader.addEventListener(MouseEvent.CLICK, handler, true);//the third parameter (true) tells flash to listen on the capture phase of an event's lifecycle
Then in the click handler from that, cancel the event so it doesn't propagate down to the loaded SWF:
function handler(e:Event):void {
e.stopImmediatePropagation();
}
If you don't understand the phases of events, this diagram from Adobe may help:
Source
As an aside, if you wanted to suppress all mouse stuffs from the SWF, a much better way (instead of adding an invisible sprite) is to just do this:
loader.mouseChildren = false;

Flash ActionScript 3.0 button click over global MouseEvent.CLICK

I want do to the next one in flash with ActionScript 3.0:
Global event (if user click in any part of the screen by mouse):
addEventListener (MouseEvent.CLICK, nextc);
function nextc (event:MouseEvent): void
{nextFrame();}
Button event (if user click exact this button):
returnb54.addEventListener(MouseEvent.CLICK, returnb54a);
function returnb54a(event:MouseEvent):void
{prevFrame();}
But on the frame with this a global event and a button nothing happens when clicking the button.
Is there any way to prioritize button event over global?
Thank you.
I created a very simple application to test your question (I'm using the same names as you defined, to be easier to understand).
I have changed 3 points:
1-
this.stage.addEventListener (MouseEvent.CLICK, nextc);
2-
function returnb54a(event:MouseEvent):void
{
event.stopImmediatePropagation();
prevFrame();
}
3-
function nextc(event:MouseEvent): void
{
event.stopImmediatePropagation();
nextFrame();
}
The method: stopImmediatePropagation() prevents processing of any event listeners in the current node and any subsequent nodes in the event flow. This method takes effect immediately, and it affects event listeners in the current node. In contrast, the stopPropagation() method doesn't take effect until all the event listeners in the current node finish processing.
Try to implement these changes and see if will have the desired result.

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.

As3 - Child heredit event listener. How to stop?

I'm an as3 newbie. I'm experiencing this strange problem. I've created a button (type: MyButton) with two child, a text (TextField) and a image icon (type: MyIcon).
Then I've append an eventlistener mouse_click on my button.
As soon as I click on the text, the e.target on the handeler function is recognized of MyButton type. Otherwise, if I click on the image icon (child of button) the e.target is MyIcon type, instead of MyButton.
How can I prevent this? I need all click to be recorded on the button, where I've stored some attributes I need on handeler function.
Thanks.
Use the e.currentTarget instead. It returns the object that the MouseEvent has currently bubbled to. e.target returns the object that the MouseEvent actually started on.
Read about Event bubbling here. It's a very important concept to grasp.