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. 4 5 #ifndef UI_EVENTS_KEYCODES_DOM4_KEYCODE_CONVERTER_H_ 6 #define UI_EVENTS_KEYCODES_DOM4_KEYCODE_CONVERTER_H_ 7 8 #include <stdint.h> 9 #include "base/basictypes.h" 10 11 // For reference, the W3C UI Event spec is located at: 12 // http://www.w3.org/TR/uievents/ 13 14 namespace ui { 15 16 // This structure is used to define the keycode mapping table. 17 // It is defined here because the unittests need access to it. 18 typedef struct { 19 // USB keycode: 20 // Upper 16-bits: USB Usage Page. 21 // Lower 16-bits: USB Usage Id: Assigned ID within this usage page. 22 uint32_t usb_keycode; 23 24 // Contains one of the following: 25 // On Linux: XKB scancode 26 // On Windows: Windows OEM scancode 27 // On Mac: Mac keycode 28 uint16_t native_keycode; 29 30 // The UIEvents (aka: DOM4Events) |code| value as defined in: 31 // https://dvcs.w3.org/hg/d4e/raw-file/tip/source_respec.htm 32 const char* code; 33 } KeycodeMapEntry; 34 35 // A class to convert between the current platform's native keycode (scancode) 36 // and platform-neutral |code| values (as defined in the W3C UI Events 37 // spec (http://www.w3.org/TR/uievents/). 38 class KeycodeConverter { 39 public: 40 // Return the value that identifies an invalid native keycode. 41 static uint16_t InvalidNativeKeycode(); 42 43 // Return the string that indentifies an invalid UI Event |code|. 44 // The returned pointer references a static global string. 45 static const char* InvalidKeyboardEventCode(); 46 47 // Convert a native (Mac/Win/Linux) keycode into the |code| string. 48 // The returned pointer references a static global string. 49 static const char* NativeKeycodeToCode(uint16_t native_keycode); 50 51 // Convert a UI Events |code| string value into a native keycode. 52 static uint16_t CodeToNativeKeycode(const char* code); 53 54 // The following methods relate to USB keycodes. 55 // Note that USB keycodes are not part of any web standard. 56 // Please don't use USB keycodes in new code. 57 58 // Return the value that identifies an invalid USB keycode. 59 static uint16_t InvalidUsbKeycode(); 60 61 // Convert a USB keycode into an equivalent platform native keycode. 62 static uint16_t UsbKeycodeToNativeKeycode(uint32_t usb_keycode); 63 64 // Convert a platform native keycode into an equivalent USB keycode. 65 static uint32_t NativeKeycodeToUsbKeycode(uint16_t native_keycode); 66 67 // Convert a USB keycode into the string with the DOM3 |code| value. 68 // The returned pointer references a static global string. 69 static const char* UsbKeycodeToCode(uint32_t usb_keycode); 70 71 // Convert a DOM3 Event |code| string into a USB keycode value. 72 static uint32_t CodeToUsbKeycode(const char* code); 73 74 // Static methods to support testing. 75 static size_t NumKeycodeMapEntriesForTest(); 76 static const KeycodeMapEntry* GetKeycodeMapForTest(); 77 78 private: 79 DISALLOW_COPY_AND_ASSIGN(KeycodeConverter); 80 }; 81 82 } // namespace ui 83 84 #endif // UI_EVENTS_KEYCODES_DOM4_KEYCODE_CONVERTER_H_ 85