Flex automatically gets focus listening to global keyboard events _flex

Source: Internet
Author: User
Tags xmlns
This is "Flex first step" QQ group inside a friend asked me a question, hereby take out share. Perhaps the problem is relatively simple, or to record it better.
requirements are as follows:
When air starts, listens for the global keyboard event, namely This.addeventlistener (Keyboardevent.key_down, Keydownhandler);
post-run effects:
When the air is running, although the Keyboardevent.key_down event is properly monitored, it is necessary to click Air on the mouse to get keydownhandler.
Reason:
1, Keydownhandler acquisition needs to listen to the object to gain focus, when mouse click Air, it is equivalent to get focus.
2, only when listening to the object to gain focus, can receive the Keyboardevent.key_down event.
So the problem root knot:
How to get the focus automatically from the Listener object.
Solution: (STEP)
1,
This.dispatchevent (New MouseEvent (Mouseevent.click));
By using the above method, the program "automatically clicks" The Dispatchevent object.
2,
Get focus Mode:
This.stage.focus = this;

<s:windowedapplication
xmlns:fx= "http://ns.adobe.com/mxml/2009"
Xmlns:s= "Library://ns.adobe.com/flex/spark"
xmlns:mx= "Library://ns.adobe.com/flex/halo"
Creationcomplete= "Createcompletehandler (event)" >
Private Function Createcompletehandler (event:flexevent): void {
Monitor Ouseevent.click Event
This.addeventlistener (Mouseevent.click, ClickHandler);
Monitor Keyboardevent.key_down
This.addeventlistener (Keyboardevent.key_down, Keydownhandler);
}
Private Function ClickHandler (event:mouseevent): void {
This.stage.focus = this;
}
Private Function Keydownhandler (event:keyboardevent): void {
Todo
}
Automatically triggers the code for mouse click: (The following code is in This.addeventlistener (Keyboardevent.key_down, Keydownhandler);
var timer:timer = new Timer (100, 1);
Timer.addeventlistener (Timerevent.timer_complete, Function (event:timerevent): void {
Event.target.stop ();
Dispatchevent (New MouseEvent (Mouseevent.click));
});
Timer.start ();
The reason for using a timer is because when the create complete, the direct use of dispatchevent will find that stage has not yet been assigned, so it needs to be delayed for a period of time processing.
Note: The above code is applicable to air, in flex inside according to the following writing is still unable to obtain Keyboardevent.key_down event.
How does Flex get the focus automatically and listen to global keyboard events?
Reason:
Flex because of the browser package, so when the browser is open, because the SWF has not yet got the focus, so even if the focus is automatically acquired (Dispatchevent (Mouseevent.click)) is meaningless.
Workaround:
Add the following functionality to the HTML embedded in the SWF:
<body onload= "document.getElementById (' swf id '). focus () >
The meaning of this sentence: When the OnLoad finished, automatically set the SWF as the focus state.
Finally, the air can be written to achieve the automatic focus, listening to global keyboard events.
Because the code is relatively small, so do not put on the demo, the above fragment code is sufficient to explain the problem:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.