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