Suppression for crbug/241044.
[chromium-blink-merge.git] / chrome / test / chromedriver / keycode_text_conversion_win.cc
blob64cc18453f3ba47571a60fab6cd488d4155d7eec
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 #include <windows.h>
9 #include <cctype>
11 #include "base/utf_string_conversions.h"
12 #include "chrome/test/chromedriver/chrome/ui_events.h"
14 std::string ConvertKeyCodeToText(ui::KeyboardCode key_code, int modifiers) {
15 UINT scan_code = ::MapVirtualKeyW(key_code, MAPVK_VK_TO_VSC);
16 BYTE keyboard_state[256];
17 memset(keyboard_state, 0, 256);
18 if (modifiers & kShiftKeyModifierMask)
19 keyboard_state[VK_SHIFT] |= 0x80;
20 if (modifiers & kControlKeyModifierMask)
21 keyboard_state[VK_CONTROL] |= 0x80;
22 if (modifiers & kAltKeyModifierMask)
23 keyboard_state[VK_MENU] |= 0x80;
24 wchar_t chars[5];
25 int code = ::ToUnicode(key_code, scan_code, keyboard_state, chars, 4, 0);
26 // |ToUnicode| converts some non-text key codes like F1 to various ASCII
27 // control chars. Filter those out.
28 if (code <= 0 || (code == 1 && std::iscntrl(chars[0]))) {
29 return "";
30 } else {
31 std::string text;
32 WideToUTF8(chars, code, &text);
33 return text;
37 bool ConvertCharToKeyCode(
38 char16 key, ui::KeyboardCode* key_code, int *necessary_modifiers) {
39 short vkey_and_modifiers = ::VkKeyScanW(key);
40 bool translated = vkey_and_modifiers != -1 &&
41 LOBYTE(vkey_and_modifiers) != -1 &&
42 HIBYTE(vkey_and_modifiers) != -1;
43 if (translated) {
44 *key_code = static_cast<ui::KeyboardCode>(LOBYTE(vkey_and_modifiers));
45 int win_modifiers = HIBYTE(vkey_and_modifiers);
46 int modifiers = 0;
47 if (win_modifiers & 0x01)
48 modifiers |= kShiftKeyModifierMask;
49 if (win_modifiers & 0x02)
50 modifiers |= kControlKeyModifierMask;
51 if (win_modifiers & 0x04)
52 modifiers |= kAltKeyModifierMask;
53 // Ignore bit 0x08: It is for Hankaku key.
54 *necessary_modifiers = modifiers;
56 return translated;