SWT: Initiating an incident post event

Source: Internet
Author: User

Many of the comrades who study SWT have encountered a kind of need, for some controls such as MouseListener, KeyListener and so on, and then, want to use the code to simulate the mouse, keyboard to perform the click, keystrokes and other operations.

Let's start by stating that this is achievable, but 99% of the demand should not be implemented.

Before you proceed, consider whether you can pull the logic code out of the listener and make it into a separate function block that you want to call "simulate".

If you can't think of a better way, please look down.

The solution is simple:

                New Event ();                 = Targetwidget;                 = SWT. KeyDown;                 = 1;                Display.getcurrent (). Post (ex);

Build an event yourself, specify a control target for the event, and then initiate it.

So, why do you need to search for such a simple code?

Here's a solution to the idea of not knowing the event and not looking at the API.

1, add a listener for any control, break point, trigger it

With this step, we can see that event events are dispatched by the Readanddispatch method of display.

2, read the Rundeferredevents method, you can observe the EventQueue event queue, you should be able to be sensitive to detect you need to add an event to this area, only to initiate events

3, Ctrl+shift+g search EventQueue in the display of the use, you can observe sentevent, postevent and so on, do not look at the source code, Ctrl+o, you can see, display external provides a post ( Event) method

4. So, is it okay to call post? Let's take a look at step 2, read the following source code:

/**        = event.widget        ; if NULL &&! widget.isdisposed ()) {            = event.item;             if Null | | ! item.isdisposed ()) {                true;                Widget.sendevent (event);            }        }

As can be seen, the necessary and sufficient condition is that the event must have an disposed widget.

Earlier solutions can be obtained.

SWT: Initiating an incident post event

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.