1 // Copyright (c) 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 "chrome/test/chromedriver/keycode_text_conversion.h"
7 #import <Carbon/Carbon.h>
11 #include "base/mac/scoped_cftyperef.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/test/chromedriver/chrome/ui_events.h"
14 #include "ui/events/keycodes/keyboard_code_conversion_mac.h"
16 bool ConvertKeyCodeToText(
17 ui::KeyboardCode key_code, int modifiers, std::string* text,
18 std::string* error_msg) {
20 ui::MacKeyCodeForWindowsKeyCode(key_code, 0, nullptr, nullptr);
21 *error_msg = std::string();
22 if (mac_key_code < 0) {
23 *text = std::string();
27 int mac_modifiers = 0;
28 if (modifiers & kShiftKeyModifierMask)
29 mac_modifiers |= shiftKey;
30 if (modifiers & kControlKeyModifierMask)
31 mac_modifiers |= controlKey;
32 if (modifiers & kAltKeyModifierMask)
33 mac_modifiers |= optionKey;
34 if (modifiers & kMetaKeyModifierMask)
35 mac_modifiers |= cmdKey;
36 // Convert EventRecord modifiers to format UCKeyTranslate accepts. See docs
37 // on UCKeyTranslate for more info.
38 UInt32 modifier_key_state = (mac_modifiers >> 8) & 0xFF;
40 base::ScopedCFTypeRef<TISInputSourceRef> input_source_copy(
41 TISCopyCurrentKeyboardLayoutInputSource());
42 CFDataRef layout_data = static_cast<CFDataRef>(TISGetInputSourceProperty(
43 input_source_copy, kTISPropertyUnicodeKeyLayoutData));
45 UInt32 dead_key_state = 0;
46 UniCharCount char_count = 0;
47 UniChar character = 0;
48 OSStatus status = UCKeyTranslate(
49 reinterpret_cast<const UCKeyboardLayout*>(CFDataGetBytePtr(layout_data)),
50 static_cast<UInt16>(mac_key_code),
54 kUCKeyTranslateNoDeadKeysBit,
59 if (status == noErr && char_count == 1 && !std::iscntrl(character)) {
60 base::string16 text16;
61 text16.push_back(character);
62 *text = base::UTF16ToUTF8(text16);
65 *text = std::string();
69 bool ConvertCharToKeyCode(
70 base::char16 key, ui::KeyboardCode* key_code, int *necessary_modifiers,
71 std::string* error_msg) {
72 base::string16 key_string;
73 key_string.push_back(key);
74 std::string key_string_utf8 = base::UTF16ToUTF8(key_string);
75 bool found_code = false;
76 *error_msg = std::string();
77 // There doesn't seem to be a way to get a mac key code for a given unicode
78 // character. So here we check every key code to see if it produces the
79 // right character. We could cache the results and regenerate everytime the
80 // language changes, but this brute force technique has negligble performance
81 // effects (on my laptop it is a submillisecond difference).
82 for (int i = 0; i < 256; ++i) {
83 ui::KeyboardCode code = static_cast<ui::KeyboardCode>(i);
84 // Skip the numpad keys.
85 if (code >= ui::VKEY_NUMPAD0 && code <= ui::VKEY_DIVIDE)
87 std::string key_string;
88 if (!ConvertKeyCodeToText(code, 0, &key_string, error_msg))
90 found_code = key_string_utf8 == key_string;
91 std::string key_string_utf8_tmp;
92 if (!ConvertKeyCodeToText(
93 code, kShiftKeyModifierMask, &key_string_utf8_tmp, error_msg))
95 if (!found_code && key_string_utf8 == key_string_utf8_tmp) {
96 *necessary_modifiers = kShiftKeyModifierMask;