isDisposed()) {
53 if (!display.readAndDispatch ()) display.sleep();
54 }
55 display.dispose();
56 }
57 }
使用SWT中Display的Post方法控制键盘(2)
时间:2011-04-13 blogjava VWPOLO
上面的例子功能是演示在一个文本框中模拟用户输入一段字符串,字符串的内容是"Love the method.",还可以通过这个来移动鼠标的箭头,像下面这样:
1 public static void main(String[] args) {
2 final Display display = new Display();
3 final Shell shell = new Shell(display);
4 final Button button = new Button (shell,SWT.NONE);
5 button.setSize(100,100);
6 button.setText("Click");
7 shell.pack();
8 shell.open ();
9 button.addListener(SWT.MouseDown, new Listener() {
10 public void handleEvent(Event e){
11 System.out.println("Mouse Down (button: " + e.button + " x: " + e.x + " y: " + e.y + ")");
12 }
13 });
14 final Point pt = display.map(shell, null, 50, 50);
15 new Thread(){
16 Event event;
17 public void run(){
18 try {
19 Thread.sleep(300);
20 } catch (InterruptedException e) {}
21 event = new Event();
22 event.type = SWT.MouseMove;
23 event.x = pt.x;
24 event.y = pt.y;
25 display.post(event);
26 try {
27 Thread.sleep(300);
28 } catch (InterruptedException e) {}
29 event.type = SWT.MouseDown;
30 event.button = 1;
31 display.post(event);
32 try {
33 Thread.sleep (300);
34 } catch (InterruptedException e) {}
35 event.type = SWT.MouseUp;
36 display.post(event);
37 }
38 }.start();
39 while (!shell.isDisposed()) {
40 if (!display.readAndDispatch()) display.sleep();
41 }
42 display.dispose();
43 }
首先创建一个100*100大小的按钮,然后通过display.map(shell, null, 50, 50);这 段代码获得指定shell上的相对坐标,这里是指shell的相对坐标上的x=50,y=50,然后再换算 成显示屏幕系统的绝对坐标,设置事件类型为鼠标移动,移动的目标坐标位置是刚才我们取 得的系统坐标
event = new Event();
event.type = SWT.MouseMove;
event.x = pt.x;
event.y = pt.y;
display.post(event);
在触发这个事件后,让它休眠0.3秒
try {
Thread.sleep(300);
} catch (InterruptedException e) {}
接着再将事件的类型设置为鼠标按下事件:
event.type = SWT.M
|