1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.eclipse.swtbot.swt.finder.keyboard; |
12 | |
|
13 | |
import java.awt.AWTException; |
14 | |
import java.awt.Robot; |
15 | |
import java.awt.event.KeyEvent; |
16 | |
import java.util.HashMap; |
17 | |
import java.util.Map; |
18 | |
|
19 | |
import org.eclipse.jface.bindings.keys.KeyStroke; |
20 | |
import org.eclipse.swt.SWT; |
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | 1 | class AWTKeyboardStrategy extends AbstractKeyboardStrategy { |
29 | |
|
30 | |
private final Robot robot; |
31 | |
|
32 | 1 | private static final Map<Integer, Integer> modifierKeyMapping = new HashMap<Integer, Integer>(); |
33 | 1 | private static final Map<Integer, Integer> naturalKeyKeyMapping = new HashMap<Integer, Integer>(); |
34 | |
|
35 | 115 | AWTKeyboardStrategy() { |
36 | |
try { |
37 | 115 | this.robot = new Robot(); |
38 | 0 | } catch (AWTException e) { |
39 | 0 | throw new RuntimeException(e); |
40 | |
} |
41 | 115 | } |
42 | |
|
43 | |
public void pressKey(KeyStroke key) { |
44 | 137 | robot.keyPress(key(key)); |
45 | 137 | } |
46 | |
|
47 | |
public void releaseKey(KeyStroke key) { |
48 | 137 | robot.keyRelease(key(key)); |
49 | |
|
50 | 137 | } |
51 | |
|
52 | |
private int key(KeyStroke key) { |
53 | 274 | if (key.getModifierKeys() != KeyStroke.NO_KEY) |
54 | 106 | return sendModifierKeys(key); |
55 | 168 | if (key.getNaturalKey() != KeyStroke.NO_KEY) |
56 | 168 | return sendNaturalKey(key); |
57 | 0 | throw new IllegalArgumentException("Could not understand keystroke " + key); |
58 | |
} |
59 | |
|
60 | |
private int sendNaturalKey(KeyStroke key) { |
61 | 168 | Integer awtKey = naturalKeyKeyMapping.get(key.getNaturalKey()); |
62 | 168 | return awtKey != null ? awtKey : key.getNaturalKey(); |
63 | |
} |
64 | |
|
65 | |
private int sendModifierKeys(KeyStroke key) { |
66 | 106 | Integer awtKey = modifierKeyMapping.get(key.getModifierKeys()); |
67 | 106 | if (awtKey != null) |
68 | 106 | return awtKey; |
69 | 0 | throw new IllegalArgumentException("Could not understand keystroke " + key); |
70 | |
} |
71 | |
|
72 | |
static { |
73 | |
|
74 | 1 | addModifierKeyMapping(SWT.CTRL, KeyEvent.VK_CONTROL); |
75 | 1 | addModifierKeyMapping(SWT.SHIFT, KeyEvent.VK_SHIFT); |
76 | 1 | addModifierKeyMapping(SWT.ALT, KeyEvent.VK_ALT); |
77 | 1 | addModifierKeyMapping(SWT.COMMAND, KeyEvent.VK_META); |
78 | |
|
79 | |
|
80 | 1 | addNaturalKeyMapping(SWT.ESC, KeyEvent.VK_ESCAPE); |
81 | |
|
82 | 1 | addNaturalKeyMapping(SWT.F1, KeyEvent.VK_F1); |
83 | 1 | addNaturalKeyMapping(SWT.F2, KeyEvent.VK_F2); |
84 | 1 | addNaturalKeyMapping(SWT.F3, KeyEvent.VK_F3); |
85 | 1 | addNaturalKeyMapping(SWT.F4, KeyEvent.VK_F4); |
86 | 1 | addNaturalKeyMapping(SWT.F5, KeyEvent.VK_F5); |
87 | 1 | addNaturalKeyMapping(SWT.F6, KeyEvent.VK_F6); |
88 | 1 | addNaturalKeyMapping(SWT.F7, KeyEvent.VK_F7); |
89 | 1 | addNaturalKeyMapping(SWT.F8, KeyEvent.VK_F8); |
90 | 1 | addNaturalKeyMapping(SWT.F9, KeyEvent.VK_F9); |
91 | 1 | addNaturalKeyMapping(SWT.F10, KeyEvent.VK_F10); |
92 | 1 | addNaturalKeyMapping(SWT.F11, KeyEvent.VK_F11); |
93 | 1 | addNaturalKeyMapping(SWT.F12, KeyEvent.VK_F12); |
94 | |
|
95 | 1 | addNaturalKeyMapping(SWT.BS, KeyEvent.VK_BACK_SPACE); |
96 | 1 | addNaturalKeyMapping(SWT.DEL, KeyEvent.VK_DELETE); |
97 | |
|
98 | |
|
99 | 1 | addNaturalKeyMapping(SWT.HOME, KeyEvent.VK_HOME); |
100 | 1 | addNaturalKeyMapping(SWT.END, KeyEvent.VK_END); |
101 | 1 | addNaturalKeyMapping(SWT.PAGE_UP, KeyEvent.VK_PAGE_UP); |
102 | 1 | addNaturalKeyMapping(SWT.PAGE_DOWN, KeyEvent.VK_PAGE_DOWN); |
103 | 1 | addNaturalKeyMapping(SWT.ARROW_RIGHT, KeyEvent.VK_RIGHT); |
104 | 1 | addNaturalKeyMapping(SWT.ARROW_DOWN, KeyEvent.VK_DOWN); |
105 | 1 | addNaturalKeyMapping(SWT.ARROW_LEFT, KeyEvent.VK_LEFT); |
106 | 1 | addNaturalKeyMapping(SWT.ARROW_UP, KeyEvent.VK_UP); |
107 | |
|
108 | |
|
109 | 1 | addNaturalKeyMapping('`', KeyEvent.VK_BACK_QUOTE); |
110 | 1 | addNaturalKeyMapping('\'', KeyEvent.VK_QUOTE); |
111 | |
} |
112 | |
|
113 | |
private static void addModifierKeyMapping(int swtKey, int awtKey) { |
114 | 4 | modifierKeyMapping.put(swtKey, awtKey); |
115 | 4 | } |
116 | |
|
117 | |
private static void addNaturalKeyMapping(int swtKey, int awtKey) { |
118 | 25 | naturalKeyKeyMapping.put(swtKey, awtKey); |
119 | 25 | } |
120 | |
|
121 | |
} |