1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.eclipse.swtbot.swt.finder.keyboard; |
12 | |
|
13 | |
import java.util.Arrays; |
14 | |
import java.util.LinkedHashSet; |
15 | |
|
16 | |
import org.eclipse.jface.bindings.keys.IKeyLookup; |
17 | |
import org.eclipse.jface.bindings.keys.KeyLookupFactory; |
18 | |
import org.eclipse.jface.bindings.keys.KeyStroke; |
19 | |
import org.eclipse.swt.SWT; |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | 1 | public class Keystrokes { |
31 | |
|
32 | |
|
33 | 1 | public static final KeyStroke SHIFT = KeyStroke.getInstance(SWT.SHIFT, 0); |
34 | |
|
35 | 1 | public static final KeyStroke CTRL = KeyStroke.getInstance(SWT.CTRL, 0); |
36 | |
|
37 | 1 | public static final KeyStroke ALT = KeyStroke.getInstance(SWT.ALT, 0); |
38 | |
|
39 | 1 | public static final KeyStroke COMMAND = KeyStroke.getInstance(SWT.COMMAND, 0); |
40 | |
|
41 | |
|
42 | 1 | public static final KeyStroke MOD1 = KeyStroke.getInstance(SWT.MOD1, 0); |
43 | |
|
44 | 1 | public static final KeyStroke MOD2 = KeyStroke.getInstance(SWT.MOD2, 0); |
45 | |
|
46 | 1 | public static final KeyStroke MOD3 = KeyStroke.getInstance(SWT.MOD3, 0); |
47 | |
|
48 | 1 | public static final KeyStroke MOD4 = KeyStroke.getInstance(SWT.MOD4, 0); |
49 | |
|
50 | |
|
51 | 1 | public static final KeyStroke ESC = KeyStroke.getInstance(0, SWT.ESC); |
52 | |
|
53 | 1 | public static final KeyStroke BS = KeyStroke.getInstance(0, SWT.BS); |
54 | |
|
55 | 1 | public static final KeyStroke DELETE = KeyStroke.getInstance(0, SWT.DEL); |
56 | |
|
57 | |
|
58 | 1 | public static final KeyStroke CR = KeyStroke.getInstance(0, SWT.CR); |
59 | |
|
60 | 1 | public static final KeyStroke LF = KeyStroke.getInstance(0, SWT.LF); |
61 | |
|
62 | 1 | public static final KeyStroke TAB = KeyStroke.getInstance(0, SWT.TAB); |
63 | |
|
64 | 1 | public static final KeyStroke SPACE = KeyStroke.getInstance(0, ' '); |
65 | |
|
66 | |
|
67 | 1 | public static final KeyStroke F1 = KeyStroke.getInstance(0, SWT.F1); |
68 | |
|
69 | 1 | public static final KeyStroke F2 = KeyStroke.getInstance(0, SWT.F2); |
70 | |
|
71 | 1 | public static final KeyStroke F3 = KeyStroke.getInstance(0, SWT.F3); |
72 | |
|
73 | 1 | public static final KeyStroke F4 = KeyStroke.getInstance(0, SWT.F4); |
74 | |
|
75 | 1 | public static final KeyStroke F5 = KeyStroke.getInstance(0, SWT.F5); |
76 | |
|
77 | 1 | public static final KeyStroke F6 = KeyStroke.getInstance(0, SWT.F6); |
78 | |
|
79 | 1 | public static final KeyStroke F7 = KeyStroke.getInstance(0, SWT.F7); |
80 | |
|
81 | 1 | public static final KeyStroke F8 = KeyStroke.getInstance(0, SWT.F8); |
82 | |
|
83 | 1 | public static final KeyStroke F9 = KeyStroke.getInstance(0, SWT.F9); |
84 | |
|
85 | 1 | public static final KeyStroke F10 = KeyStroke.getInstance(0, SWT.F10); |
86 | |
|
87 | 1 | public static final KeyStroke F11 = KeyStroke.getInstance(0, SWT.F11); |
88 | |
|
89 | 1 | public static final KeyStroke F12 = KeyStroke.getInstance(0, SWT.F12); |
90 | |
|
91 | |
|
92 | 1 | public static final KeyStroke UP = KeyStroke.getInstance(0, SWT.ARROW_UP); |
93 | |
|
94 | 1 | public static final KeyStroke DOWN = KeyStroke.getInstance(0, SWT.ARROW_DOWN); |
95 | |
|
96 | 1 | public static final KeyStroke LEFT = KeyStroke.getInstance(0, SWT.ARROW_LEFT); |
97 | |
|
98 | 1 | public static final KeyStroke RIGHT = KeyStroke.getInstance(0, SWT.ARROW_RIGHT); |
99 | |
|
100 | |
|
101 | 1 | public static final KeyStroke END = KeyStroke.getInstance(0, SWT.END); |
102 | |
|
103 | 1 | public static final KeyStroke HOME = KeyStroke.getInstance(0, SWT.HOME); |
104 | |
|
105 | 1 | public static final KeyStroke PAGE_UP = KeyStroke.getInstance(0, SWT.PAGE_UP); |
106 | |
|
107 | 1 | public static final KeyStroke PAGE_DOWN = KeyStroke.getInstance(0, SWT.PAGE_DOWN); |
108 | |
|
109 | |
|
110 | 1 | private static KeyboardLayout defaultKeyboardLayout = KeyboardLayout.getDefaultKeyboardLayout(); |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
static KeyStroke[] create(char ch) { |
117 | 82 | KeyStroke keyStroke = defaultKeyboardLayout.keyStrokeFor(ch); |
118 | 82 | if (keyStroke.getModifierKeys() == KeyStroke.NO_KEY) |
119 | 61 | return new KeyStroke[] { keyStroke }; |
120 | 21 | KeyStroke modifier = KeyStroke.getInstance(keyStroke.getModifierKeys(), 0); |
121 | 21 | KeyStroke key = KeyStroke.getInstance(0, keyStroke.getNaturalKey()); |
122 | 21 | return new KeyStroke[] { modifier, key }; |
123 | |
} |
124 | |
|
125 | |
static char toCharacter(KeyStroke... keys) { |
126 | 0 | int modifierKeys = 0; |
127 | 0 | int ch = 0; |
128 | 0 | for (KeyStroke keyStroke : keys) { |
129 | 0 | modifierKeys |= keyStroke.getModifierKeys(); |
130 | 0 | ch |= keyStroke.getNaturalKey(); |
131 | |
} |
132 | |
|
133 | 0 | return defaultKeyboardLayout.toCharacter(KeyStroke.getInstance(modifierKeys, ch)); |
134 | |
} |
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
public static KeyStroke[] toKeys(int modificationKeys, char c) { |
142 | 5 | LinkedHashSet<KeyStroke> keys = new LinkedHashSet<KeyStroke>(); |
143 | 5 | if (modificationKeys != 0) { |
144 | 5 | int[] sortModifierKeys = sortModifierKeys(modificationKeys); |
145 | 25 | for (int modifierKey : sortModifierKeys) { |
146 | 20 | if (modifierKey != KeyStroke.NO_KEY) |
147 | 10 | keys.add(KeyStroke.getInstance(modifierKey, 0)); |
148 | |
} |
149 | |
} |
150 | 5 | if (c != 0) |
151 | 4 | keys.addAll(Arrays.asList(Keystrokes.create(c))); |
152 | 5 | return keys.toArray(new KeyStroke[] {}); |
153 | |
} |
154 | |
|
155 | |
|
156 | |
private static int[] sortModifierKeys(final int modifierKeys) { |
157 | 5 | final IKeyLookup lookup = KeyLookupFactory.getDefault(); |
158 | 5 | final int[] sortedKeys = new int[4]; |
159 | 5 | int index = 0; |
160 | |
|
161 | 5 | if ((modifierKeys & lookup.getAlt()) != 0) { |
162 | 1 | sortedKeys[index++] = lookup.getAlt(); |
163 | |
} |
164 | 5 | if ((modifierKeys & lookup.getCommand()) != 0) { |
165 | 1 | sortedKeys[index++] = lookup.getCommand(); |
166 | |
} |
167 | 5 | if ((modifierKeys & lookup.getCtrl()) != 0) { |
168 | 5 | sortedKeys[index++] = lookup.getCtrl(); |
169 | |
} |
170 | 5 | if ((modifierKeys & lookup.getShift()) != 0) { |
171 | 3 | sortedKeys[index++] = lookup.getShift(); |
172 | |
} |
173 | |
|
174 | 5 | return sortedKeys; |
175 | |
} |
176 | |
|
177 | |
} |