Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(963)

Unified Diff: ui/events/keycodes/dom4/keycode_converter.cc

Issue 641753003: DOM Level 3 .code and .key value enumerations. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: add code for unicode keys Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/events/keycodes/dom4/keycode_converter.cc
diff --git a/ui/events/keycodes/dom4/keycode_converter.cc b/ui/events/keycodes/dom4/keycode_converter.cc
index 63689f0e5467e41c7536f05cc80d279435746bb7..8a19dcb6233ce77722a234ebf197b1b13937cca6 100644
--- a/ui/events/keycodes/dom4/keycode_converter.cc
+++ b/ui/events/keycodes/dom4/keycode_converter.cc
@@ -4,23 +4,45 @@
#include "ui/events/keycodes/dom4/keycode_converter.h"
+#include "ui/events/keycodes/dom3/dom_code.h"
+#include "ui/events/keycodes/dom3/dom_key.h"
+
namespace ui {
namespace {
+// Table of USB codes (equivalent to DomCode values), native scan codes,
+// and DOM Level 3 |code| strings.
#if defined(OS_WIN)
-#define USB_KEYMAP(usb, xkb, win, mac, code) {usb, win, code}
+#define USB_KEYMAP(usb, xkb, win, mac, code, id) {usb, win, code}
#elif defined(OS_LINUX)
-#define USB_KEYMAP(usb, xkb, win, mac, code) {usb, xkb, code}
+#define USB_KEYMAP(usb, xkb, win, mac, code, id) {usb, xkb, code}
#elif defined(OS_MACOSX)
-#define USB_KEYMAP(usb, xkb, win, mac, code) {usb, mac, code}
+#define USB_KEYMAP(usb, xkb, win, mac, code, id) {usb, mac, code}
#else
-#define USB_KEYMAP(usb, xkb, win, mac, code) {usb, 0, code}
+#define USB_KEYMAP(usb, xkb, win, mac, code, id) {usb, 0, code}
#endif
+#define USB_KEYMAP_DECLARATION const KeycodeMapEntry usb_keycode_map[] =
#include "ui/events/keycodes/dom4/keycode_converter_data.h"
+#undef USB_KEYMAP
+#undef USB_KEYMAP_DECLARATION
const size_t kKeycodeMapEntries = arraysize(usb_keycode_map);
+// Table of DomKey enum values and DOM Level 3 |key| strings.
+struct DomKeyMapEntry {
+ DomKey dom_key;
+ const char* string;
+};
+
+#define DOM_KEY_MAP(key, id) {DomKey::id, key}
+#define DOM_KEY_MAP_DECLARATION const DomKeyMapEntry dom_key_map[] =
+#include "ui/events/keycodes/dom3/dom_key_data.h"
+#undef DOM_KEY_MAP
+#undef DOM_KEY_MAP_DECLARATION
+
+const size_t kDomKeyMapEntries = arraysize(dom_key_map);
+
} // namespace
// static
@@ -34,6 +56,13 @@ const KeycodeMapEntry* KeycodeConverter::GetKeycodeMapForTest() {
}
// static
+const char* KeycodeConverter::DomKeyStringForTest(size_t index) {
+ if (index >= kDomKeyMapEntries)
+ return nullptr;
+ return dom_key_map[index].string;
+}
+
+// static
uint16_t KeycodeConverter::InvalidNativeKeycode() {
return usb_keycode_map[0].native_keycode;
}
@@ -56,6 +85,18 @@ const char* KeycodeConverter::NativeKeycodeToCode(uint16_t native_keycode) {
}
// static
+DomCode KeycodeConverter::NativeKeycodeToDomCode(uint16_t native_keycode) {
+ for (size_t i = 0; i < kKeycodeMapEntries; ++i) {
+ if (usb_keycode_map[i].native_keycode == native_keycode) {
+ if (usb_keycode_map[i].code != NULL)
+ return static_cast<DomCode>(usb_keycode_map[i].usb_keycode);
+ break;
+ }
+ }
+ return DomCode::NONE;
+}
+
+// static
uint16_t KeycodeConverter::CodeToNativeKeycode(const char* code) {
if (!code ||
strcmp(code, InvalidKeyboardEventCode()) == 0) {
@@ -71,6 +112,59 @@ uint16_t KeycodeConverter::CodeToNativeKeycode(const char* code) {
return InvalidNativeKeycode();
}
+// static
+uint16_t KeycodeConverter::DomCodeToNativeKeycode(DomCode code) {
+ if (code == DomCode::NONE)
+ return InvalidNativeKeycode();
+ for (size_t i = 0; i < kKeycodeMapEntries; ++i) {
+ if (usb_keycode_map[i].usb_keycode == static_cast<uint32_t>(code))
+ return usb_keycode_map[i].native_keycode;
+ }
+ return InvalidNativeKeycode();
+}
+
+// static
+DomCode KeycodeConverter::CodeStringToDomCode(const char* code) {
+ if (!code ||
+ strcmp(code, InvalidKeyboardEventCode()) == 0) {
+ return DomCode::NONE;
+ }
+ for (size_t i = 0; i < kKeycodeMapEntries; ++i) {
+ if (usb_keycode_map[i].code &&
+ strcmp(usb_keycode_map[i].code, code) == 0) {
+ return static_cast<DomCode>(usb_keycode_map[i].usb_keycode);
+ }
+ }
+ return DomCode::NONE;
+}
+
+// static
+const char* KeycodeConverter::DomCodeToCodeString(DomCode dom_code) {
+ return UsbKeycodeToCode(static_cast<uint32_t>(dom_code));
+}
+
+// static
+DomKey KeycodeConverter::KeyStringToDomKey(const char* key) {
+ if (!key || !*key)
+ return DomKey::NONE;
+ for (size_t i = 0; i < kDomKeyMapEntries; ++i) {
+ if (dom_key_map[i].string &&
+ strcmp(dom_key_map[i].string, key) == 0) {
+ return dom_key_map[i].dom_key;
+ }
+ }
+ return DomKey::NONE;
+}
+
+// static
+const char* KeycodeConverter::DomKeyToKeyString(DomKey dom_key) {
+ for (size_t i = 0; i < kDomKeyMapEntries; ++i) {
+ if (dom_key_map[i].dom_key == dom_key)
+ return dom_key_map[i].string;
+ }
+ return nullptr;
+}
+
// USB keycodes
// Note that USB keycodes are not part of any web standard.
// Please don't use USB keycodes in new code.

Powered by Google App Engine
This is Rietveld 408576698