1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef UI_EVENTS_KEYCODES_DOM4_KEYCODE_CONVERTER_H_
6 #define UI_EVENTS_KEYCODES_DOM4_KEYCODE_CONVERTER_H_
11 #include "base/basictypes.h"
12 #include "ui/events/keycodes/dom/dom_key.h"
14 // For reference, the W3C UI Event spec is located at:
15 // http://www.w3.org/TR/uievents/
21 enum class DomKeyLocation
{ STANDARD
, LEFT
, RIGHT
, NUMPAD
};
23 // This structure is used to define the keycode mapping table.
24 // It is defined here because the unittests need access to it.
27 // Upper 16-bits: USB Usage Page.
28 // Lower 16-bits: USB Usage Id: Assigned ID within this usage page.
31 // Contains one of the following:
32 // On Linux: XKB scancode
33 // On Windows: Windows OEM scancode
34 // On Mac: Mac keycode
37 // The UIEvents (aka: DOM4Events) |code| value as defined in:
38 // http://www.w3.org/TR/DOM-Level-3-Events-code/
42 // A class to convert between the current platform's native keycode (scancode)
43 // and platform-neutral |code| values (as defined in the W3C UI Events
44 // spec (http://www.w3.org/TR/uievents/).
45 class KeycodeConverter
{
47 // Return the value that identifies an invalid native keycode.
48 static int InvalidNativeKeycode();
50 // Convert a native (Mac/Win/Linux) keycode into a DomCode.
51 static DomCode
NativeKeycodeToDomCode(int native_keycode
);
53 // Convert a DomCode into a native keycode.
54 static int DomCodeToNativeKeycode(DomCode code
);
56 // Convert a UI Events |code| string value into a DomCode.
57 static DomCode
CodeStringToDomCode(const char* code
);
59 // Convert a DomCode into a UI Events |code| string value.
60 static const char* DomCodeToCodeString(DomCode dom_code
);
62 // Return the DomKeyLocation of a DomCode. The DomKeyLocation distinguishes
63 // keys with the same meaning, and therefore the same DomKey or non-located
64 // KeyboardCode (VKEY), and corresponds to the DOM UI Events
65 // |KeyboardEvent.location|.
66 static DomKeyLocation
DomCodeToLocation(DomCode dom_code
);
68 // Convert a UI Events |key| string value into a DomKey.
69 // Accepts a character string containing either
70 // - a key name from http://www.w3.org/TR/DOM-Level-3-Events-key/, or
71 // - a single Unicode character (represented in UTF-8).
72 // Returns DomKey::NONE for other inputs, including |nullptr|.
73 static DomKey
KeyStringToDomKey(const char* key
);
75 // Convert a DomKey into a UI Events |key| string value.
76 // For an invalid DomKey, returns an empty string.
77 static std::string
DomKeyToKeyString(DomKey dom_key
);
79 // Returns true if the DomKey is a modifier.
80 static bool IsDomKeyForModifier(DomKey dom_key
);
82 // The following methods relate to USB keycodes.
83 // Note that USB keycodes are not part of any web standard.
84 // Please don't use USB keycodes in new code.
86 // Return the value that identifies an invalid USB keycode.
87 static uint32_t InvalidUsbKeycode();
89 // Convert a USB keycode into an equivalent platform native keycode.
90 static int UsbKeycodeToNativeKeycode(uint32_t usb_keycode
);
92 // Convert a platform native keycode into an equivalent USB keycode.
93 static uint32_t NativeKeycodeToUsbKeycode(int native_keycode
);
95 // Convert a USB keycode into a DomCode.
96 static DomCode
UsbKeycodeToDomCode(uint32_t usb_keycode
);
98 // Convert a DomCode into a USB keycode.
99 static uint32_t DomCodeToUsbKeycode(DomCode dom_code
);
101 // Convert a DOM3 Event |code| string into a USB keycode value.
102 static uint32_t CodeToUsbKeycode(const char* code
);
104 // Static methods to support testing.
105 static size_t NumKeycodeMapEntriesForTest();
106 static const KeycodeMapEntry
* GetKeycodeMapForTest();
107 static const char* DomKeyStringForTest(size_t index
);
110 DISALLOW_COPY_AND_ASSIGN(KeycodeConverter
);
115 #endif // UI_EVENTS_KEYCODES_DOM4_KEYCODE_CONVERTER_H_