OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef UI_EVENTS_KEYCODES_KEYBOARD_CODE_CONVERSION_H_ | 5 #ifndef UI_EVENTS_KEYCODES_KEYBOARD_CODE_CONVERSION_H_ |
6 #define UI_EVENTS_KEYCODES_KEYBOARD_CODE_CONVERSION_H_ | 6 #define UI_EVENTS_KEYCODES_KEYBOARD_CODE_CONVERSION_H_ |
7 | 7 |
8 #include "base/strings/string16.h" | 8 #include "base/strings/string16.h" |
9 #include "ui/events/events_base_export.h" | 9 #include "ui/events/events_base_export.h" |
10 #include "ui/events/keycodes/keyboard_codes.h" | 10 #include "ui/events/keycodes/keyboard_codes.h" |
11 | 11 |
12 namespace ui { | 12 namespace ui { |
13 | 13 |
14 enum class DomCode; | 14 enum class DomCode; |
15 enum class DomKey; | 15 enum class DomKey; |
16 | 16 |
17 // Helper functions to get the meaning of a Windows key code in a | 17 // Helper functions to get the meaning of a DOM |code| in a |
18 // platform independent way. It supports control characters as well. | 18 // platform independent way. It supports control characters as well. |
19 // It assumes a US keyboard layout is used, so it may only be used when there | 19 // It assumes a US keyboard layout is used, so it may only be used when there |
20 // is no native event or no better way to get the character. | 20 // is no native event or no better way to get the character. |
| 21 // |
21 // For example, if a virtual keyboard implementation can only generate key | 22 // For example, if a virtual keyboard implementation can only generate key |
22 // events with key_code and flags information, then there is no way for us to | 23 // events with key_code and flags information, then there is no way for us to |
23 // determine the actual character that should be generate by the key. Because | 24 // determine the actual character that should be generate by the key. Because |
24 // a key_code only represents a physical key on the keyboard, it has nothing | 25 // a key_code only represents a physical key on the keyboard, it has nothing |
25 // to do with the actual character printed on that key. In such case, the only | 26 // to do with the actual character printed on that key. In such case, the only |
26 // thing we can do is to assume that we are using a US keyboard and get the | 27 // thing we can do is to assume that we are using a US keyboard and get the |
27 // character according to US keyboard layout definition. | 28 // character according to US keyboard layout definition. Preferably, such |
28 // If a virtual keyboard implementation wants to support other keyboard | 29 // events should be created using a full KeyEvent constructor, explicitly |
29 // layouts, that may generate different text for a certain key than on a US | 30 // specifying the character and DOM 3 values as well as the legacy VKEY. |
30 // keyboard, a special native event object should be introduced to carry extra | 31 EVENTS_BASE_EXPORT base::char16 DomCodeToCharacter(DomCode dom_code, |
31 // information to help determine the correct character. | 32 int flags); |
32 // Take XKeyEvent as an example, it contains not only keycode and modifier | 33 |
33 // flags but also group and other extra XKB information to help determine the | 34 EVENTS_BASE_EXPORT bool DomCodeToMeaning(DomCode dom_code, |
34 // correct character. That's why we can use XLookupString() function to get | 35 int flags, |
35 // the correct text generated by a X key event (See how is GetCharacter() | 36 DomKey* dom_key, |
36 // implemented in event_x.cc). | 37 base::char16* character, |
37 EVENTS_BASE_EXPORT base::char16 GetCharacterFromKeyCode(KeyboardCode key_code, | 38 KeyboardCode* key_code); |
38 int flags); | 39 |
39 EVENTS_BASE_EXPORT bool GetMeaningFromKeyCode(KeyboardCode key_code, | 40 // Obtains the control character corresponding to a physical key; |
40 int flags, | 41 // that is, the meaning of the physical key state (dom_code, and flags |
41 DomKey* dom_key, | 42 // containing EF_CONTROL_DOWN) under a specific US English layout. |
42 base::char16* character); | 43 // Returns true and sets the output parameters if the (dom_code, flags) pair |
| 44 // is interpreted as a control character; otherwise the output parameters |
| 45 // ara untouched. |
| 46 EVENTS_BASE_EXPORT bool DomCodeToControlCharacter(DomCode dom_code, |
| 47 int flags, |
| 48 DomKey* dom_key, |
| 49 base::char16* character, |
| 50 KeyboardCode* key_code); |
| 51 |
| 52 // Returns the DomKey value associated with an ASCII/Unicode character |
| 53 // All printable characters are most other character codes use |
| 54 // DomKey::CHARACTER, but a few ASCII C0 codes have their own DomKey. |
| 55 EVENTS_BASE_EXPORT DomKey CharacterToDomKey(base::char16 character); |
| 56 |
| 57 // Returns a Windows-based VKEY for a non-printable DOM Level 3 |key|. |
| 58 // The returned VKEY is non-located (e.g. VKEY_SHIFT). |
| 59 EVENTS_BASE_EXPORT KeyboardCode |
| 60 NonPrintableDomKeyToKeyboardCode(DomKey dom_key); |
| 61 |
| 62 // Returns the Windows-based VKEY value corresponding to a DOM Level 3 |code|. |
| 63 // The returned VKEY is located (e.g. VKEY_LSHIFT) and considered as a |
| 64 // layout-independent physical key (e.g. DomCode::KEY_Q -> VKEY_Q) which is |
| 65 // based on the US English Layout. This should only be used as a last resort |
| 66 // when other means of determining a suitable VKEY fails. |
| 67 EVENTS_BASE_EXPORT KeyboardCode DomCodeToKeyboardCode(DomCode dom_code); |
43 | 68 |
44 // Determine the non-located VKEY corresponding to a located VKEY. | 69 // Determine the non-located VKEY corresponding to a located VKEY. |
45 // Most modifier keys have two kinds of KeyboardCode: located (e.g. | 70 // Most modifier keys have two kinds of KeyboardCode: located (e.g. |
46 // VKEY_LSHIFT and VKEY_RSHIFT), that indentify one of two specific | 71 // VKEY_LSHIFT and VKEY_RSHIFT), that indentify one of two specific |
47 // physical keys, and non-located (e.g. VKEY_SHIFT) that identify | 72 // physical keys, and non-located (e.g. VKEY_SHIFT) that identify |
48 // only the operation. | 73 // only the operation. |
49 EVENTS_BASE_EXPORT KeyboardCode | 74 EVENTS_BASE_EXPORT KeyboardCode |
50 LocatedToNonLocatedKeyboardCode(KeyboardCode key_code); | 75 LocatedToNonLocatedKeyboardCode(KeyboardCode key_code); |
51 | 76 |
52 // Determine the located VKEY corresponding to a non-located VKEY. | 77 // Determine the located VKEY corresponding to a non-located VKEY. |
53 EVENTS_BASE_EXPORT KeyboardCode | 78 EVENTS_BASE_EXPORT KeyboardCode |
54 NonLocatedToLocatedKeyboardCode(KeyboardCode key_code, DomCode dom_code); | 79 NonLocatedToLocatedKeyboardCode(KeyboardCode key_code, DomCode dom_code); |
55 | 80 |
| 81 // Returns a DOM Level 3 |code| from a Windows based VKEY value. |
| 82 // This assumes a US Layout and should only be used when |code| cannot be |
| 83 // determined from a physical scan code. |
| 84 // For example, when a key event was generated synthetically by JS with |
| 85 // onlay a VKEY value supplied. |
| 86 EVENTS_BASE_EXPORT DomCode KeyboardCodeToDomCode(KeyboardCode key_code); |
| 87 |
| 88 // Returns the ui::EventFlags value associated with a modifier key, |
| 89 // or 0 (EF_NONE) if the key is not a modifier. |
| 90 EVENTS_BASE_EXPORT int ModifierDomKeyToEventFlag(DomKey key); |
| 91 |
56 } // namespace ui | 92 } // namespace ui |
57 | 93 |
58 #endif // UI_EVENTS_KEYCODES_KEYBOARD_CODE_CONVERSION_H_ | 94 #endif // UI_EVENTS_KEYCODES_KEYBOARD_CODE_CONVERSION_H_ |
OLD | NEW |