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 #include "ui/events/keycodes/dom4/keycode_converter.h"
12 #define USB_KEYMAP(usb, xkb, win, mac, code) {usb, win, code}
13 #elif defined(OS_LINUX)
14 #define USB_KEYMAP(usb, xkb, win, mac, code) {usb, xkb, code}
15 #elif defined(OS_MACOSX)
16 #define USB_KEYMAP(usb, xkb, win, mac, code) {usb, mac, code}
18 #define USB_KEYMAP(usb, xkb, win, mac, code) {usb, 0, code}
20 #include "ui/events/keycodes/dom4/keycode_converter_data.h"
22 const size_t kKeycodeMapEntries
= arraysize(usb_keycode_map
);
27 size_t KeycodeConverter::NumKeycodeMapEntriesForTest() {
28 return kKeycodeMapEntries
;
32 const KeycodeMapEntry
* KeycodeConverter::GetKeycodeMapForTest() {
33 return &usb_keycode_map
[0];
37 uint16_t KeycodeConverter::InvalidNativeKeycode() {
38 return usb_keycode_map
[0].native_keycode
;
42 const char* KeycodeConverter::InvalidKeyboardEventCode() {
43 return "Unidentified";
47 const char* KeycodeConverter::NativeKeycodeToCode(uint16_t native_keycode
) {
48 for (size_t i
= 0; i
< kKeycodeMapEntries
; ++i
) {
49 if (usb_keycode_map
[i
].native_keycode
== native_keycode
) {
50 if (usb_keycode_map
[i
].code
!= NULL
)
51 return usb_keycode_map
[i
].code
;
55 return InvalidKeyboardEventCode();
59 uint16_t KeycodeConverter::CodeToNativeKeycode(const char* code
) {
61 strcmp(code
, InvalidKeyboardEventCode()) == 0) {
62 return InvalidNativeKeycode();
65 for (size_t i
= 0; i
< kKeycodeMapEntries
; ++i
) {
66 if (usb_keycode_map
[i
].code
&&
67 strcmp(usb_keycode_map
[i
].code
, code
) == 0) {
68 return usb_keycode_map
[i
].native_keycode
;
71 return InvalidNativeKeycode();
75 // Note that USB keycodes are not part of any web standard.
76 // Please don't use USB keycodes in new code.
79 uint16_t KeycodeConverter::InvalidUsbKeycode() {
80 return usb_keycode_map
[0].usb_keycode
;
84 uint16_t KeycodeConverter::UsbKeycodeToNativeKeycode(uint32_t usb_keycode
) {
85 // Deal with some special-cases that don't fit the 1:1 mapping.
86 if (usb_keycode
== 0x070032) // non-US hash.
87 usb_keycode
= 0x070031; // US backslash.
88 #if defined(OS_MACOSX)
89 if (usb_keycode
== 0x070046) // PrintScreen.
90 usb_keycode
= 0x070068; // F13.
93 for (size_t i
= 0; i
< kKeycodeMapEntries
; ++i
) {
94 if (usb_keycode_map
[i
].usb_keycode
== usb_keycode
)
95 return usb_keycode_map
[i
].native_keycode
;
97 return InvalidNativeKeycode();
101 uint32_t KeycodeConverter::NativeKeycodeToUsbKeycode(uint16_t native_keycode
) {
102 for (size_t i
= 0; i
< kKeycodeMapEntries
; ++i
) {
103 if (usb_keycode_map
[i
].native_keycode
== native_keycode
)
104 return usb_keycode_map
[i
].usb_keycode
;
106 return InvalidUsbKeycode();
110 const char* KeycodeConverter::UsbKeycodeToCode(uint32_t usb_keycode
) {
111 for (size_t i
= 0; i
< kKeycodeMapEntries
; ++i
) {
112 if (usb_keycode_map
[i
].usb_keycode
== usb_keycode
)
113 return usb_keycode_map
[i
].code
;
115 return InvalidKeyboardEventCode();
119 uint32_t KeycodeConverter::CodeToUsbKeycode(const char* code
) {
121 strcmp(code
, InvalidKeyboardEventCode()) == 0) {
122 return InvalidUsbKeycode();
125 for (size_t i
= 0; i
< kKeycodeMapEntries
; ++i
) {
126 if (usb_keycode_map
[i
].code
&&
127 strcmp(usb_keycode_map
[i
].code
, code
) == 0) {
128 return usb_keycode_map
[i
].usb_keycode
;
131 return InvalidUsbKeycode();