This source file includes following definitions.
- ConvertKeyCodeToText
- ConvertCharToKeyCode
#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);
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;
*necessary_modifiers = modifiers;
}
return translated;
}