Use SWT to simulate mouse and keyboard events

Source: Internet
Author: User

Recently I was studying SWT/jface. When I was doing something small, I needed to simulate mouse and keyboard events. I searched the internet and simulated these events in Java. There are two main methods:

I. Use the robot class robot in AWT
java.lang.Objectjava.awt.Robot
public class Robot
Extends object
 
This class is used to generate local system input events for automated testing, self-running demos, and other applications that require mouse and keyboard control. The main purpose of robot is to facilitate automatic testing on the Java platform. The main simulated functions in the class are as follows:
void keyPress(int keycode)
Press the given key.
 void keyRelease(int keycode)
Release the given key.
 void mouseMove(int x, int y)
Move the mouse pointer to the specified screen coordinate.
 void mousePress(int buttons)
Click one or more mouse buttons.
 void mouseRelease(int buttons)
Release one or more mouse buttons.
 void mouseWheel(int wheelAmt)
Rotate the scroll wheel with the scroll wheel.
Ii. Use the mouse and keyboard events in SWT

There are two examples in SWT snippets to introduce the use of the two methods, as follows: (1) simulate mouse events
* UI automation (for testing tools) snippet: Post mouse events
Import org. Eclipse. SWT. *; public class snippet142 {public static void main (string [] ARGs ){
Final display = New Display ();
Final shell = new shell (Display );
Final button = new button (shell, SWT. None );
Button. setsize (100,100 );
Button. settext ("click ");
Shell. Pack ();
Shell. open ();
Button. addlistener (SWT. mousedown, new listener (){
Public void handleevent (event e ){
System. out. println ("mouse down (button:" + E. button + "X:" + E. X + "Y:" + E. Y + ")");
}
});
Final point Pt = display. Map (shell, null, 50, 50 );
New thread (){
Event event;
Public void run (){
Try {
Thread. Sleep (300 );
} Catch (interruptedexception e ){}
Event = new event ();
Event. type = SWT. mousemove;
Event. x = pt. X;
Event. Y = pt. Y;
Display. Post (event );
Try {
Thread. Sleep (300 );
} Catch (interruptedexception e ){}
Event. type = SWT. mousedown;
Event. Button = 1;
Display. Post (event );
Try {
Thread. Sleep (300 );
} Catch (interruptedexception e ){}
Event. type = SWT. mouseup;
Display. Post (event );
}
}. Start ();
While (! Shell. isdisposed ()){
If (! Display. readanddispatch () display. Sleep ();
}
Display. Dispose ();
}
}
(2) simulate Keyboard Events * UI automation (for testing tools) snippet: Post key events
Import org. Eclipse. SWT. *; public class snippet146 {public static void main (string [] ARGs ){
Final display = New Display ();
Final shell = new shell (Display );
Final text = new text (shell, SWT. Border );
Text. setsize (text. computesize (150, SWT. Default ));
Shell. Pack ();
Shell. open ();
New thread (){
Public void run (){
String string = "Love the method .";
For (INT I = 0; I <string. Length (); I ++ ){
Char CH = string. charat (I );
Boolean shift = character. isuppercase (CH );
Ch = character. tolowercase (CH );
If (shift ){
Event event = new event ();
Event. type = SWT. keydown;
Event. keycode = SWT. shift;
Display. Post (event );
}
Event event = new event ();
Event. type = SWT. keydown;
Event. Character = CH;
Display. Post (event );
Try {
Thread. Sleep (10 );
} Catch (interruptedexception e ){}
Event. type = SWT. keyup;
Display. Post (event );
Try {
Thread. Sleep (100 );
} Catch (interruptedexception e ){}
If (shift ){
Event = new event ();
Event. type = SWT. keyup;
Event. keycode = SWT. shift;
Display. Post (event );
}
}
}
}. Start ();
While (! Shell. isdisposed ()){
If (! Display. readanddispatch () display. Sleep ();
}
Display. Dispose ();
}
} Both methods declare an event first, and then set the type of the event to the corresponding SWT. keyup/keydown/mouseup/mousedown, and finally simulate the corresponding event through the post (event) method of the display class. NOTE: If multiple controls exist, you need to obtain the display attribute of the corresponding control and then post the event. For example, if there is a browser in a window, you need to use thisclass. browser. getdisplay (). 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.