• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 Kevin Ollivier <kevino@theolliviers.com>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include "config.h"
27 #include "PlatformKeyboardEvent.h"
28 
29 #include "KeyboardCodes.h"
30 
31 #include <wx/defs.h>
32 #include <wx/event.h>
33 
34 namespace WebCore {
35 
keyIdentifierForWxKeyCode(int keyCode)36 static String keyIdentifierForWxKeyCode(int keyCode)
37 {
38     switch (keyCode) {
39         case WXK_MENU:
40         case WXK_ALT:
41             return "Alt";
42         case WXK_CLEAR:
43             return "Clear";
44         case WXK_DOWN:
45             return "Down";
46         case WXK_END:
47             return "End";
48         case WXK_RETURN:
49             return "Enter";
50         case WXK_EXECUTE:
51             return "Execute";
52         case WXK_F1:
53             return "F1";
54         case WXK_F2:
55             return "F2";
56         case WXK_F3:
57             return "F3";
58         case WXK_F4:
59             return "F4";
60         case WXK_F5:
61             return "F5";
62         case WXK_F6:
63             return "F6";
64         case WXK_F7:
65             return "F7";
66         case WXK_F8:
67             return "F8";
68         case WXK_F9:
69             return "F9";
70         case WXK_F10:
71             return "F10";
72         case WXK_F11:
73             return "F11";
74         case WXK_F12:
75             return "F12";
76         case WXK_F13:
77             return "F13";
78         case WXK_F14:
79             return "F14";
80         case WXK_F15:
81             return "F15";
82         case WXK_F16:
83             return "F16";
84         case WXK_F17:
85             return "F17";
86         case WXK_F18:
87             return "F18";
88         case WXK_F19:
89             return "F19";
90         case WXK_F20:
91             return "F20";
92         case WXK_F21:
93             return "F21";
94         case WXK_F22:
95             return "F22";
96         case WXK_F23:
97             return "F23";
98         case WXK_F24:
99             return "F24";
100         case WXK_HELP:
101             return "Help";
102         case WXK_HOME:
103             return "Home";
104         case WXK_INSERT:
105             return "Insert";
106         case WXK_LEFT:
107             return "Left";
108         case WXK_PAGEDOWN:
109             return "PageDown";
110         case WXK_PAGEUP:
111             return "PageUp";
112         case WXK_PAUSE:
113             return "Pause";
114         case WXK_PRINT:
115             return "PrintScreen";
116         case WXK_RIGHT:
117             return "Right";
118         case WXK_SELECT:
119             return "Select";
120         case WXK_UP:
121             return "Up";
122             // Standard says that DEL becomes U+007F.
123         case WXK_DELETE:
124             return "U+007F";
125         default:
126             return String::format("U+%04X", toupper(keyCode));
127     }
128 }
129 
windowsKeyCodeForKeyEvent(unsigned int keycode)130 static int windowsKeyCodeForKeyEvent(unsigned int keycode)
131 {
132     switch (keycode) {
133         /* FIXME: Need to supply a bool in this func, to determine wheter the event comes from the keypad
134         */
135         case WXK_NUMPAD0:
136             return VK_NUMPAD0;// (60) Numeric keypad 0 key
137         case WXK_NUMPAD1:
138             return VK_NUMPAD1;// (61) Numeric keypad 1 key
139         case WXK_NUMPAD2:
140             return  VK_NUMPAD2; // (62) Numeric keypad 2 key
141         case WXK_NUMPAD3:
142             return VK_NUMPAD3; // (63) Numeric keypad 3 key
143         case WXK_NUMPAD4:
144             return VK_NUMPAD4; // (64) Numeric keypad 4 key
145         case WXK_NUMPAD5:
146             return VK_NUMPAD5; //(65) Numeric keypad 5 key
147         case WXK_NUMPAD6:
148             return VK_NUMPAD6; // (66) Numeric keypad 6 key
149         case WXK_NUMPAD7:
150             return VK_NUMPAD7; // (67) Numeric keypad 7 key
151         case WXK_NUMPAD8:
152             return VK_NUMPAD8; // (68) Numeric keypad 8 key
153         case WXK_NUMPAD9:
154             return VK_NUMPAD9; // (69) Numeric keypad 9 key
155         case WXK_NUMPAD_MULTIPLY:
156             return VK_MULTIPLY; // (6A) Multiply key
157         case WXK_NUMPAD_ADD:
158             return VK_ADD; // (6B) Add key
159         case WXK_NUMPAD_SUBTRACT:
160             return VK_SUBTRACT; // (6D) Subtract key
161         case WXK_NUMPAD_DECIMAL:
162             return VK_DECIMAL; // (6E) Decimal key
163         case WXK_DIVIDE:
164             return VK_DIVIDE; // (6F) Divide key
165         case WXK_NUMPAD_SEPARATOR:
166             return VK_SEPARATOR;
167 
168         case WXK_BACK:
169             return VK_BACK; // (08) BACKSPACE key
170         case WXK_TAB:
171         case WXK_NUMPAD_TAB:
172             return VK_TAB; // (09) TAB key
173         case WXK_CLEAR:
174             return VK_CLEAR; // (0C) CLEAR key
175         case WXK_NUMPAD_ENTER:
176         case WXK_RETURN:
177             return VK_RETURN; //(0D) Return key
178         case WXK_SHIFT:
179             return VK_SHIFT; // (10) SHIFT key
180         case WXK_CONTROL:
181             return VK_CONTROL; // (11) CTRL key
182         case WXK_MENU:
183         case WXK_ALT:
184             return VK_MENU; // (12) ALT key
185 
186         case WXK_PAUSE:
187             return VK_PAUSE; // (13) PAUSE key
188         case WXK_CAPITAL:
189             return VK_CAPITAL; // (14) CAPS LOCK key
190         /*
191         case WXK_Kana_Lock:
192         case WXK_Kana_Shift:
193             return VK_KANA; // (15) Input Method Editor (IME) Kana mode
194         case WXK_Hangul:
195             return VK_HANGUL; // VK_HANGUL (15) IME Hangul mode
196             // VK_JUNJA (17) IME Junja mode
197             // VK_FINAL (18) IME final mode
198         case WXK_Hangul_Hanja:
199             return VK_HANJA; // (19) IME Hanja mode
200         case WXK_Kanji:
201             return VK_KANJI; // (19) IME Kanji mode
202         */
203         case WXK_ESCAPE:
204             return VK_ESCAPE; // (1B) ESC key
205             // VK_CONVERT (1C) IME convert
206             // VK_NONCONVERT (1D) IME nonconvert
207             // VK_ACCEPT (1E) IME accept
208             // VK_MODECHANGE (1F) IME mode change request
209         case WXK_NUMPAD_SPACE:
210         case WXK_SPACE:
211             return VK_SPACE; // (20) SPACEBAR
212         case WXK_NUMPAD_PAGEUP:
213         case WXK_PAGEUP:
214             return VK_PRIOR; // (21) PAGE UP key
215         case WXK_NUMPAD_PAGEDOWN:
216         case WXK_PAGEDOWN:
217             return VK_NEXT; // (22) PAGE DOWN key
218         case WXK_NUMPAD_END:
219         case WXK_END:
220             return VK_END; // (23) END key
221         case WXK_NUMPAD_HOME:
222         case WXK_HOME:
223             return VK_HOME; // (24) HOME key
224         case WXK_NUMPAD_LEFT:
225         case WXK_LEFT:
226             return VK_LEFT; // (25) LEFT ARROW key
227         case WXK_NUMPAD_UP:
228         case WXK_UP:
229             return VK_UP; // (26) UP ARROW key
230         case WXK_NUMPAD_RIGHT:
231         case WXK_RIGHT:
232             return VK_RIGHT; // (27) RIGHT ARROW key
233         case WXK_NUMPAD_DOWN:
234         case WXK_DOWN:
235             return VK_DOWN; // (28) DOWN ARROW key
236         case WXK_SELECT:
237             return VK_SELECT; // (29) SELECT key
238         case WXK_PRINT:
239             return VK_PRINT; // (2A) PRINT key
240         case WXK_EXECUTE:
241             return VK_EXECUTE;// (2B) EXECUTE key
242         case WXK_SNAPSHOT:
243              return VK_SNAPSHOT; // (2C) PRINT SCREEN key
244         case WXK_NUMPAD_INSERT:
245         case WXK_INSERT:
246             return VK_INSERT; // (2D) INS key
247         case WXK_NUMPAD_DELETE:
248         case WXK_DELETE:
249             return VK_DELETE; // (2E) DEL key
250         case WXK_HELP:
251             return VK_HELP; // (2F) HELP key
252         case '0':
253             return VK_0;    //  (30) 0) key
254         case '1':
255             return VK_1; //  (31) 1 ! key
256         case '2':
257             return VK_2; //  (32) 2 & key
258         case '3':
259             return VK_3; //case '3': case '#';
260         case '4':  //  (34) 4 key '$';
261             return VK_4;
262         case '5':
263             return VK_5; //  (35) 5 key  '%'
264         case '6':
265             return VK_6; //  (36) 6 key  '^'
266         case '7':
267             return VK_7; //  (37) 7 key  case '&'
268         case '8':
269             return VK_8; //  (38) 8 key  '*'
270         case '9':
271             return VK_9; //  (39) 9 key '('
272         case 'A':
273             return VK_A; //  (41) A key case 'a': case 'A': return 0x41;
274         case 'B':
275             return VK_B; //  (42) B key case 'b': case 'B': return 0x42;
276         case 'C':
277             return VK_C; //  (43) C key case 'c': case 'C': return 0x43;
278         case 'D':
279             return VK_D; //  (44) D key case 'd': case 'D': return 0x44;
280         case 'E':
281             return VK_E; //  (45) E key case 'e': case 'E': return 0x45;
282         case 'F':
283             return VK_F; //  (46) F key case 'f': case 'F': return 0x46;
284         case 'G':
285             return VK_G; //  (47) G key case 'g': case 'G': return 0x47;
286         case 'H':
287             return VK_H; //  (48) H key case 'h': case 'H': return 0x48;
288         case 'I':
289             return VK_I; //  (49) I key case 'i': case 'I': return 0x49;
290         case 'J':
291             return VK_J; //  (4A) J key case 'j': case 'J': return 0x4A;
292         case 'K':
293             return VK_K; //  (4B) K key case 'k': case 'K': return 0x4B;
294         case 'L':
295             return VK_L; //  (4C) L key case 'l': case 'L': return 0x4C;
296         case 'M':
297             return VK_M; //  (4D) M key case 'm': case 'M': return 0x4D;
298         case 'N':
299             return VK_N; //  (4E) N key case 'n': case 'N': return 0x4E;
300         case 'O':
301             return VK_O; //  (4F) O key case 'o': case 'O': return 0x4F;
302         case 'P':
303             return VK_P; //  (50) P key case 'p': case 'P': return 0x50;
304         case 'Q':
305             return VK_Q; //  (51) Q key case 'q': case 'Q': return 0x51;
306         case 'R':
307             return VK_R; //  (52) R key case 'r': case 'R': return 0x52;
308         case 'S':
309             return VK_S; //  (53) S key case 's': case 'S': return 0x53;
310         case 'T':
311             return VK_T; //  (54) T key case 't': case 'T': return 0x54;
312         case 'U':
313             return VK_U; //  (55) U key case 'u': case 'U': return 0x55;
314         case 'V':
315             return VK_V; //  (56) V key case 'v': case 'V': return 0x56;
316         case 'W':
317             return VK_W; //  (57) W key case 'w': case 'W': return 0x57;
318         case 'X':
319             return VK_X; //  (58) X key case 'x': case 'X': return 0x58;
320         case 'Y':
321             return VK_Y; //  (59) Y key case 'y': case 'Y': return 0x59;
322         case 'Z':
323             return VK_Z; //  (5A) Z key case 'z': case 'Z': return 0x5A;
324         case WXK_WINDOWS_LEFT:
325             return VK_LWIN; // (5B) Left Windows key (Microsoft Natural keyboard)
326 
327         case WXK_NUMLOCK:
328             return VK_NUMLOCK; // (90) NUM LOCK key
329 
330         case WXK_SCROLL:
331             return VK_SCROLL; // (91) SCROLL LOCK key
332 
333         default:
334             return 0;
335     }
336 }
337 
PlatformKeyboardEvent(wxKeyEvent & event)338 PlatformKeyboardEvent::PlatformKeyboardEvent(wxKeyEvent& event)
339 {
340     if (event.GetEventType() == wxEVT_KEY_UP)
341         m_type = KeyUp;
342     else if (event.GetEventType() == wxEVT_KEY_DOWN)
343         m_type = KeyDown;
344     else if (event.GetEventType() == wxEVT_CHAR)
345         m_type = Char;
346     else
347         ASSERT_NOT_REACHED();
348     if (m_type != Char)
349         m_keyIdentifier = keyIdentifierForWxKeyCode(event.GetKeyCode());
350     else {
351         //ENTER is an editing command processed as a char (only Enter and Tab are)
352         //unfortunately the unicode key for numpad_enter (370) is NOT what we want here (13)
353         //The unicode values for normal enter and tab are the same as the ASCII values, thus ok
354         //Note that I think this a wx bug, as the Character Code is actually 13 when
355         //numpad_enter is a CHAR event.
356         if (event.GetKeyCode() == 13 && event.GetUnicodeKey() == wxChar(370))
357             m_text = "\r";
358         else
359             m_text = wxString(event.GetUnicodeKey());
360         m_unmodifiedText = m_text;
361     }
362     m_autoRepeat = false; // FIXME: not correct.
363     m_windowsVirtualKeyCode = windowsKeyCodeForKeyEvent(event.GetKeyCode());
364     m_nativeVirtualKeyCode = event.GetKeyCode();
365     m_isKeypad = (event.GetKeyCode() >= WXK_NUMPAD_SPACE) && (event.GetKeyCode() <= WXK_NUMPAD_DIVIDE);
366     m_shiftKey = event.ShiftDown();
367     m_ctrlKey = event.CmdDown();
368     m_altKey = event.AltDown();
369     m_metaKey = event.MetaDown();
370 }
371 
disambiguateKeyDownEvent(Type type,bool)372 void PlatformKeyboardEvent::disambiguateKeyDownEvent(Type type, bool)
373 {
374     // Can only change type from KeyDown to RawKeyDown or Char, as we lack information for other conversions.
375     ASSERT(m_type == KeyDown);
376     m_type = type;
377     if (type == RawKeyDown) {
378         m_text = String();
379         m_unmodifiedText = String();
380     } else {
381         m_keyIdentifier = String();
382         m_windowsVirtualKeyCode = 0;
383     }
384 }
385 
currentCapsLockState()386 bool PlatformKeyboardEvent::currentCapsLockState()
387 {
388     return wxGetKeyState(WXK_CAPITAL);
389 }
390 
391 }
392 
393