|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 package com.nokia.mj.impl.uitestutils; |
|
18 |
|
19 /** |
|
20 * This class is used when sending emulated key events from automated test |
|
21 * cases. hides the platform keycode specifities from the test cases. There |
|
22 * might be an implementation of this class for different platforms enabling |
|
23 * running of the same key input test cases on each platform. This |
|
24 * implementation is for Xlib. |
|
25 */ |
|
26 public class Key { |
|
27 |
|
28 // |
|
29 // LOGICAL KEY CODES |
|
30 // |
|
31 |
|
32 // Non unicode MIDP key codes, these are the correct values from MIDP spec. |
|
33 public static final int UpArrow = -1; |
|
34 public static final int DownArrow = -2; |
|
35 public static final int LeftArrow = -3; |
|
36 public static final int RightArrow = -4; |
|
37 public static final int Select = -5; // Middle softkey |
|
38 public static final int CBA1 = -6; // Left softkey |
|
39 public static final int CBA2 = -7; // Right softkey |
|
40 public static final int Clear = -8; |
|
41 public static final int Send = -10; |
|
42 public static final int End = -11; |
|
43 public static final int Applications = -12; |
|
44 public static final int Voice = -13; |
|
45 public static final int ClockwiseRotate = -16; |
|
46 public static final int CounterClockwiseRotate = -17; |
|
47 public static final int ClockwiseFastRotate = -18; |
|
48 public static final int CounterClockwiseFastRotate = -19; |
|
49 public static final int PlayPause = -20; |
|
50 public static final int PreviousRew = -21; |
|
51 public static final int NextFF = -22; |
|
52 public static final int Stop = -23; |
|
53 public static final int Edit = -50; |
|
54 public static final int AAction = -51; |
|
55 public static final int BAction = -52; |
|
56 |
|
57 // Non unicode non MIDP key codes. These values are just made up and are not from any spec. |
|
58 public static final int Power = -100; |
|
59 public static final int Backspace = -101; |
|
60 public static final int GripOpen = -102; |
|
61 public static final int GripClose = -103; |
|
62 public static final int Camera = -104; |
|
63 public static final int VolumeUp = -105; |
|
64 public static final int VolumeDown = -106; |
|
65 public static final int PageUp = -107; |
|
66 public static final int PageDown = -108; |
|
67 public static final int HomeKey = -109; |
|
68 public static final int EndKey = -110; |
|
69 // Activate key represents a key that will cause a "default selection" of an item. On Linux |
|
70 // this is Enter, but on S60 it may be the same key as Select. |
|
71 public static final int Activate = -111; |
|
72 public static final int InsertKey = -112; |
|
73 public static final int ReturnKey = -113; |
|
74 public static final int DeleteKey = -114; |
|
75 public static final int Escape = -115; |
|
76 public static final int ControlRight = -116; |
|
77 public static final int ControlLeft = -117; |
|
78 public static final int ShiftRight = -118; |
|
79 public static final int ShiftLeft = -119; |
|
80 public static final int Multiply = -120; |
|
81 public static final int Divide = -121; |
|
82 public static final int Add = -122; |
|
83 public static final int Subtract = -123; |
|
84 |
|
85 public static final int Undefined = -121; |
|
86 |
|
87 // Map from logical key codes to Xlib key syms |
|
88 static int keycodes[] = { |
|
89 UpArrow, |
|
90 DownArrow, |
|
91 LeftArrow, |
|
92 RightArrow, |
|
93 Select, |
|
94 CBA1, |
|
95 CBA2, |
|
96 Clear, |
|
97 Send, |
|
98 End, |
|
99 Applications, |
|
100 Voice, |
|
101 ClockwiseRotate, |
|
102 CounterClockwiseRotate, |
|
103 ClockwiseFastRotate, |
|
104 CounterClockwiseFastRotate, |
|
105 PlayPause, |
|
106 PreviousRew, |
|
107 NextFF, |
|
108 Stop, |
|
109 Edit, |
|
110 AAction, |
|
111 BAction, |
|
112 Power, |
|
113 Backspace, |
|
114 GripOpen, |
|
115 GripClose, |
|
116 Camera, |
|
117 VolumeUp, |
|
118 VolumeDown, |
|
119 PageUp, |
|
120 PageDown, |
|
121 HomeKey, |
|
122 EndKey, |
|
123 Activate, |
|
124 InsertKey, |
|
125 ReturnKey, |
|
126 DeleteKey, |
|
127 Escape, |
|
128 ControlRight, |
|
129 ControlLeft, |
|
130 ShiftRight, |
|
131 ShiftLeft, |
|
132 Multiply, |
|
133 Divide, |
|
134 Add, |
|
135 Subtract, |
|
136 Undefined |
|
137 }; |
|
138 static int keysyms[] = { |
|
139 XlibKeys.XK_Up, // UpArrow |
|
140 XlibKeys.XK_Down, // DownArrow |
|
141 XlibKeys.XK_Left, // LeftArrow |
|
142 XlibKeys.XK_Right, // RightArrow |
|
143 XlibKeys.XK_space, // Select |
|
144 0, // CBA1 |
|
145 0, // CBA2 |
|
146 XlibKeys.XK_Clear, // Clear |
|
147 0, // Send |
|
148 0, // End |
|
149 0, // Applications |
|
150 0, // Voice |
|
151 0, // ClockwiseRotate |
|
152 0, // CounterClockwiseRotate |
|
153 0, // ClockwiseFastRotate |
|
154 0, // CounterClockwiseFastRotate |
|
155 0, // PlayPause |
|
156 0, // PreviousRew |
|
157 0, // NextFF |
|
158 0, // Stop |
|
159 0, // Edit |
|
160 0, // AAction |
|
161 0, // BAction |
|
162 0, // Power |
|
163 XlibKeys.XK_BackSpace, // Backspace |
|
164 0, // GripOpen |
|
165 0, // GripClose |
|
166 0, // Camera |
|
167 0, // VolumeUp |
|
168 0, // VolumeDown |
|
169 XlibKeys.XK_Page_Up, // PageUp |
|
170 XlibKeys.XK_Page_Down, // PageDown |
|
171 XlibKeys.XK_Home, // HomeKey |
|
172 XlibKeys.XK_End, // EndKey |
|
173 XlibKeys.XK_Return, // Activate |
|
174 XlibKeys.XK_Insert, // InsertKey |
|
175 XlibKeys.XK_Return, // ReturnKey |
|
176 XlibKeys.XK_Delete, // DeleteKey |
|
177 XlibKeys.XK_Escape, // Esc |
|
178 XlibKeys.XK_Control_R, // ControlRight |
|
179 XlibKeys.XK_Control_L, // ControlLeft |
|
180 XlibKeys.XK_Shift_R, // ControlRight |
|
181 XlibKeys.XK_Shift_L, // ControlLeft |
|
182 XlibKeys.XK_KP_Multiply, // Multiply |
|
183 XlibKeys.XK_KP_Divide, // Divide |
|
184 XlibKeys.XK_KP_Add, // Add |
|
185 XlibKeys.XK_KP_Subtract, // Subtract |
|
186 XlibKeys.XK_Return // Undefined |
|
187 }; |
|
188 |
|
189 protected int keysym(int code) { |
|
190 for(int i = 0; i < keycodes.length; ++i) { |
|
191 if(keycodes[i] == code) return keysyms[i]; |
|
192 } |
|
193 return 0; |
|
194 } |
|
195 |
|
196 /** Default constructor. Only here for compatibility with old test cases. */ |
|
197 public Key() { |
|
198 this.keyCode = 0; |
|
199 } |
|
200 |
|
201 /** |
|
202 * Same as Key(int), only here for compatibility with old test cases. |
|
203 * Scancode not used. |
|
204 */ |
|
205 public Key(int keyCode, int scanCode) { |
|
206 this(keyCode); |
|
207 } |
|
208 |
|
209 /** |
|
210 * Constructor accepting a character code or a logical key code defined |
|
211 * above. E.g. to send a tab key pass '\t' here. To send an up arrow pass |
|
212 * UpArrow. |
|
213 * |
|
214 * @param key |
|
215 * A character code or a logical key code. |
|
216 */ |
|
217 public Key(int key) { |
|
218 |
|
219 // Convert MIDP keyCodes to Xlib keycodes |
|
220 if (key < 0) { |
|
221 int keysym = keysym(key); |
|
222 if(keysym != 0) { |
|
223 keyCode = XlibUtils.XKeysymToKeycode(keysym); |
|
224 } |
|
225 } |
|
226 // Try using character code as the keysym (except the null character) |
|
227 else { |
|
228 // Try if it maps to latin1 |
|
229 keyCode = XlibUtils.XKeysymToKeycode(key); |
|
230 |
|
231 // If not latin1, try tty function keys |
|
232 if((keyCode == 0) && ((0xff00 & keyCode) == 0)) { |
|
233 keyCode = XlibUtils.XKeysymToKeycode(0xff00|key); |
|
234 } |
|
235 |
|
236 // More attempts |
|
237 } |
|
238 |
|
239 if(keyCode == 0 && modifiers == 0) { |
|
240 throw new IllegalArgumentException("Failed to map to any keycode: code=" + key + " char =" + (char)key); |
|
241 } |
|
242 } |
|
243 |
|
244 /** |
|
245 * Use constructor rather than setting directly, only public for |
|
246 * compatibility with old test cases. |
|
247 */ |
|
248 public int keyCode; |
|
249 |
|
250 /** |
|
251 * Not used for anything, only here for compatibility with old test cases. |
|
252 */ |
|
253 public int scanCode; |
|
254 |
|
255 /** |
|
256 * Inclusive OR of modifiers. Will be sent with the key event. |
|
257 * |
|
258 * @see com.nokia.mj.impl.uitestutils.Modifier |
|
259 */ |
|
260 public int modifiers; |
|
261 } |