Chromium Code Reviews| Index: chrome/test/chromedriver/keycode_text_conversion_win.cc |
| diff --git a/chrome/test/chromedriver/keycode_text_conversion_win.cc b/chrome/test/chromedriver/keycode_text_conversion_win.cc |
| index 802eb3c7483246e9b67758539e32f982abc3ce0d..284679a1e54a63418b018f93bafc8920ed6a2a1d 100644 |
| --- a/chrome/test/chromedriver/keycode_text_conversion_win.cc |
| +++ b/chrome/test/chromedriver/keycode_text_conversion_win.cc |
| @@ -1,59 +1,103 @@ |
| -// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| -// Use of this source code is governed by a BSD-style license that can be |
| -// found in the LICENSE file. |
| - |
| -#include "chrome/test/chromedriver/keycode_text_conversion.h" |
| - |
| -#include <stdlib.h> |
| -#include <windows.h> |
| - |
| -#include "base/strings/utf_string_conversions.h" |
| -#include "chrome/test/chromedriver/chrome/ui_events.h" |
| - |
| -bool ConvertKeyCodeToText( |
| - ui::KeyboardCode key_code, int modifiers, std::string* text, |
| - std::string* error_msg) { |
| - UINT scan_code = ::MapVirtualKeyW(key_code, MAPVK_VK_TO_VSC); |
| - BYTE keyboard_state[256]; |
| - memset(keyboard_state, 0, 256); |
| - *error_msg = std::string(); |
| - if (modifiers & kShiftKeyModifierMask) |
| - keyboard_state[VK_SHIFT] |= 0x80; |
| - if (modifiers & kControlKeyModifierMask) |
| - keyboard_state[VK_CONTROL] |= 0x80; |
| - if (modifiers & kAltKeyModifierMask) |
| - keyboard_state[VK_MENU] |= 0x80; |
| - wchar_t chars[5]; |
| - int code = ::ToUnicode(key_code, scan_code, keyboard_state, chars, 4, 0); |
| - // |ToUnicode| converts some non-text key codes like F1 to various |
| - // control chars. Filter those out. |
| - if (code <= 0 || (code == 1 && iswcntrl(chars[0]))) |
| - *text = std::string(); |
| - else |
| - base::WideToUTF8(chars, code, text); |
| - return true; |
| -} |
| - |
| -bool ConvertCharToKeyCode( |
| - base::char16 key, ui::KeyboardCode* key_code, int *necessary_modifiers, |
| - std::string* error_msg) { |
| - short vkey_and_modifiers = ::VkKeyScanW(key); |
| - bool translated = vkey_and_modifiers != -1 && |
| - LOBYTE(vkey_and_modifiers) != 0xFF && |
| - HIBYTE(vkey_and_modifiers) != 0xFF; |
| - *error_msg = std::string(); |
| - if (translated) { |
| - *key_code = static_cast<ui::KeyboardCode>(LOBYTE(vkey_and_modifiers)); |
| - int win_modifiers = HIBYTE(vkey_and_modifiers); |
| - int modifiers = 0; |
| - if (win_modifiers & 0x01) |
| - modifiers |= kShiftKeyModifierMask; |
| - if (win_modifiers & 0x02) |
| - modifiers |= kControlKeyModifierMask; |
| - if (win_modifiers & 0x04) |
| - modifiers |= kAltKeyModifierMask; |
| - // Ignore bit 0x08: It is for Hankaku key. |
| - *necessary_modifiers = modifiers; |
| - } |
| - return translated; |
| -} |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/test/chromedriver/keycode_text_conversion.h" |
| + |
| +#include <VersionHelpers.h> |
| +#include <stdlib.h> |
| +#include <windows.h> |
| + |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "chrome/test/chromedriver/chrome/ui_events.h" |
| + |
| +bool ConvertKeyCodeToText( |
| + ui::KeyboardCode key_code, int modifiers, std::string* text, |
| + std::string* error_msg) { |
| + UINT scan_code = ::MapVirtualKeyW(key_code, MAPVK_VK_TO_VSC); |
| + BYTE keyboard_state[256]; |
| + memset(keyboard_state, 0, 256); |
| + *error_msg = std::string(); |
| + if (modifiers & kShiftKeyModifierMask) |
| + keyboard_state[VK_SHIFT] |= 0x80; |
| + if (modifiers & kControlKeyModifierMask) |
| + keyboard_state[VK_CONTROL] |= 0x80; |
| + if (modifiers & kAltKeyModifierMask) |
| + keyboard_state[VK_MENU] |= 0x80; |
| + wchar_t chars[5]; |
| + int code = ::ToUnicode(key_code, scan_code, keyboard_state, chars, 4, 0); |
| + // |ToUnicode| converts some non-text key codes like F1 to various |
| + // control chars. Filter those out. |
| + if (code <= 0 || (code == 1 && iswcntrl(chars[0]))) |
| + *text = std::string(); |
| + else |
| + base::WideToUTF8(chars, code, text); |
| + return true; |
| +} |
| + |
| +bool ConvertCharToKeyCode( |
| + base::char16 key, ui::KeyboardCode* key_code, int *necessary_modifiers, |
| + std::string* error_msg) { |
| + short vkey_and_modifiers = ::VkKeyScanW(key); |
| + bool translated = vkey_and_modifiers != -1 && |
| + LOBYTE(vkey_and_modifiers) != 0xFF && |
| + HIBYTE(vkey_and_modifiers) != 0xFF; |
| + *error_msg = std::string(); |
| + if (translated) { |
| + *key_code = static_cast<ui::KeyboardCode>(LOBYTE(vkey_and_modifiers)); |
| + int win_modifiers = HIBYTE(vkey_and_modifiers); |
| + int modifiers = 0; |
| + if (win_modifiers & 0x01) |
| + modifiers |= kShiftKeyModifierMask; |
| + if (win_modifiers & 0x02) |
| + modifiers |= kControlKeyModifierMask; |
| + if (win_modifiers & 0x04) |
| + modifiers |= kAltKeyModifierMask; |
| + // Ignore bit 0x08: It is for Hankaku key. |
| + *necessary_modifiers = modifiers; |
| + } |
| + return translated; |
| +} |
| + |
| +bool SwitchToUSKeyboardLayout() { |
| + bool switch_status = false; |
| + |
| + // For LoadKeyboardLayout - Prior to Windows 8: If the specified input |
| + // locale identifier is not already loaded, the function loads and |
| + // activates the input locale identifier for the current thread. |
| + // Beginning in Windows 8: If the specified input locale identifier is not |
| + // already loaded, the function loads and activates the input |
| + // locale identifier for the system. |
| + // For Windows 8 - Use ActivateKeyboardLayout instead of LoadKeyboardLayout |
| + if (IsWindows8OrGreater()) { |
| + int size; |
| + HKL * keyBoard_handles_list; |
|
samuong
2015/01/23 19:36:58
Can we use a scoped_ptr here instead? If you do, y
andrewcheng1
2015/01/24 01:40:00
1. my initial approach do not have switch_status,
andrewcheng1
2015/02/19 00:56:59
Done.
|
| + TCHAR active_keyboard[KL_NAMELENGTH]; |
| + |
| + if ((size = ::GetKeyboardLayoutList(0, NULL)) > 0) |
| + keyBoard_handles_list = new HKL[size]; |
| + else |
| + return switch_status; |
| + |
| + ::GetKeyboardLayoutList(size, keyBoard_handles_list); |
| + |
| + for (int keyboard_index = 0; keyboard_index < size; keyboard_index++) { |
| + ::ActivateKeyboardLayout(keyBoard_handles_list[keyboard_index], |
| + KLF_SETFORPROCESS); |
| + ::GetKeyboardLayoutName(active_keyboard); |
| + // if active keyboard match US keyboard layout - 00000409 |
| + if (wcscmp(active_keyboard, L"00000409") == 0) { |
| + switch_status = true; |
| + break; |
|
samuong
2015/01/23 19:36:58
What happens if none of the HKLs match the US keyb
andrewcheng1
2015/01/24 01:40:00
I verified this already two weeks ago - in lan
andrewcheng1
2015/01/24 01:40:00
Done.
|
| + } |
| + } |
| + delete [] keyBoard_handles_list; |
| + } else { |
| + HKL layout = ::LoadKeyboardLayout((LPCTSTR)"00000409", KLF_ACTIVATE); |
|
samuong
2015/01/23 19:36:58
Can you define a for "00000409" (or use an existin
andrewcheng1
2015/01/24 01:40:00
Done.
I can not find any pre-defined constant for
andrewcheng1
2015/02/19 00:56:59
Done.
andrewcheng1
2015/02/19 00:56:59
Done.
|
| + if (!layout) |
| + switch_status = false; |
| + else |
| + switch_status = true; |
| + } |
| + return switch_status; |
| +} |