Notes for flex programming: automatic focus acquisition and monitoring of global Keyboard Events

Source: Internet
Author: User

Notes for flex programming: automatic focus acquisition and monitoring of global Keyboard Events

 

See http://www.k-zone.cn/zblog/post/flex-air-auto-set-focus.html for details
This is a question from a friend in the QQ group of flex Step 1. I would like to share it with you. The problem may be relatively simple, but it is better to record it.

The requirements are as follows:
When air is started, it listens to global keyboard events, that is, this. addeventlistener (keyboardevent. key_down, keydownhandler );

Effect after running:
After the air is running, although the keyboardevent. key_down event is correctly monitored, you must click the air button to obtain the keydownhandler.

Cause:
1. The keydownhandler obtains the focus of the object to be listened to. When the mouse clicks the air, the focus is obtained.
2. The keyboardevent. key_down event can be received only when the Monitored object obtains the focus.

The root cause is:
How to automatically obtain the focus of a listener object.

Solution: (STEP)
1,
This. dispatchevent (New mouseevent (mouseevent. Click ));
The above method enables the program to "automatically click" The dispatchevent object.

2,
Focus retrieval method:
This. Stage. Focus = this;

The code snippet is as follows:
<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 {
// Listen to ouseevent. Click Event
This. addeventlistener (mouseevent. Click, clickhandler );
// Listen to keyboardevent. key_down
This. addeventlistener (keyboardevent. key_down, keydownhandler );
}

Private function clickhandler (Event: mouseevent): void {
This. Stage. Focus = this;
}

Private function clickhandler (Event: keyboardevent): void {
// To do
}

Code for automatically triggering mouse click: (the following code is found in this. addeventlistener (keyboardevent. key_down, keydownhandler)
VaR Timer: timer = new timer (100, 1 );
Timer. addeventlistener (timerevent. timer_complete, function (Event: timerevent): void {
Event.tar get. Stop ();
Dispatchevent (New mouseevent (mouseevent. Click ));
});
Timer. Start ();
The reason why timer is used for processing is that when create complete is used directly, dispatchevent will find that the stage has not been assigned a value, so it needs to be delayed for a period of time.
Note: The above Code applies to air. In Flex, The keyboardevent. key_down event cannot be obtained as follows.

In Flex, how does one automatically obtain the focus and listen for global Keyboard Events?

Cause:
Flex has a browser package, so after opening the browser, since SWF has not obtained the focus, it automatically obtains the focus (dispatchevent (New mouseevent (mouseevent. click);) is meaningless.

Solution:
Add the following functions to the HTML embedded in SWF:
<Body onload = "document. getelementbyid ('swf id'). Focus ()">
The meaning of this sentence: When onload is complete, the SWF is automatically set as the focus state.

Finally, you can automatically obtain the focus and listen to global Keyboard Events by writing according to air.

Because the code is relatively small, so do not put the demo, the above part of the code is enough 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.