diff options
author | yusukes@chromium.org <yusukes@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-11-16 06:35:54 +0000 |
---|---|---|
committer | yusukes@chromium.org <yusukes@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-11-16 06:35:54 +0000 |
commit | 8b700ec91c40bcc8031e48bd7d82c4b5024733f7 (patch) | |
tree | 8d817410ce8bab2ba72ea5a72f07d997c9e913cf /ui/base/keycodes | |
parent | 4eabd06ab6f594ee7244be72efb03b51e778d478 (diff) | |
download | chromium_src-8b700ec91c40bcc8031e48bd7d82c4b5024733f7.zip chromium_src-8b700ec91c40bcc8031e48bd7d82c4b5024733f7.tar.gz chromium_src-8b700ec91c40bcc8031e48bd7d82c4b5024733f7.tar.bz2 |
Move GetCharacterFromKeyCode from views::KeyEvent to ui/base/keycodes/keyboard_code_conversion.h.
BUG=chromium:97261
TEST=try
Review URL: http://codereview.chromium.org/8511061
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@110263 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/base/keycodes')
-rw-r--r-- | ui/base/keycodes/keyboard_code_conversion.cc | 109 | ||||
-rw-r--r-- | ui/base/keycodes/keyboard_code_conversion.h | 38 |
2 files changed, 147 insertions, 0 deletions
diff --git a/ui/base/keycodes/keyboard_code_conversion.cc b/ui/base/keycodes/keyboard_code_conversion.cc new file mode 100644 index 0000000..eaaf1fa --- /dev/null +++ b/ui/base/keycodes/keyboard_code_conversion.cc @@ -0,0 +1,109 @@ +// Copyright (c) 2011 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 "ui/base/keycodes/keyboard_code_conversion.h" + +#include "ui/base/events.h" + +namespace ui { + +uint16 GetCharacterFromKeyCode(KeyboardCode key_code, int flags) { + const bool ctrl = (flags & EF_CONTROL_DOWN) != 0; + const bool shift = (flags & EF_SHIFT_DOWN) != 0; + const bool upper = shift ^ ((flags & EF_CAPS_LOCK_DOWN) != 0); + + // Following Windows behavior to map ctrl-a ~ ctrl-z to \x01 ~ \x1A. + if (key_code >= VKEY_A && key_code <= VKEY_Z) + return key_code - VKEY_A + (ctrl ? 1 : (upper ? 'A' : 'a')); + + // Other ctrl characters + if (ctrl) { + if (shift) { + // following graphics chars require shift key to input. + switch (key_code) { + // ctrl-@ maps to \x00 (Null byte) + case VKEY_2: + return 0; + // ctrl-^ maps to \x1E (Record separator, Information separator two) + case VKEY_6: + return 0x1E; + // ctrl-_ maps to \x1F (Unit separator, Information separator one) + case VKEY_OEM_MINUS: + return 0x1F; + // Returns 0 for all other keys to avoid inputting unexpected chars. + default: + return 0; + } + } else { + switch (key_code) { + // ctrl-[ maps to \x1B (Escape) + case VKEY_OEM_4: + return 0x1B; + // ctrl-\ maps to \x1C (File separator, Information separator four) + case VKEY_OEM_5: + return 0x1C; + // ctrl-] maps to \x1D (Group separator, Information separator three) + case VKEY_OEM_6: + return 0x1D; + // ctrl-Enter maps to \x0A (Line feed) + case VKEY_RETURN: + return 0x0A; + // Returns 0 for all other keys to avoid inputting unexpected chars. + default: + return 0; + } + } + } + + // Normal characters + if (key_code >= VKEY_0 && key_code <= VKEY_9) + return shift ? ")!@#$%^&*("[key_code - VKEY_0] : key_code; + else if (key_code >= VKEY_NUMPAD0 && key_code <= VKEY_NUMPAD9) + return key_code - VKEY_NUMPAD0 + '0'; + + switch (key_code) { + case VKEY_TAB: + return '\t'; + case VKEY_RETURN: + return '\r'; + case VKEY_MULTIPLY: + return '*'; + case VKEY_ADD: + return '+'; + case VKEY_SUBTRACT: + return '-'; + case VKEY_DECIMAL: + return '.'; + case VKEY_DIVIDE: + return '/'; + case VKEY_SPACE: + return ' '; + case VKEY_OEM_1: + return shift ? ':' : ';'; + case VKEY_OEM_PLUS: + return shift ? '+' : '='; + case VKEY_OEM_COMMA: + return shift ? '<' : ','; + case VKEY_OEM_MINUS: + return shift ? '_' : '-'; + case VKEY_OEM_PERIOD: + return shift ? '>' : '.'; + case VKEY_OEM_2: + return shift ? '?' : '/'; + case VKEY_OEM_3: + return shift ? '~' : '`'; + case VKEY_OEM_4: + return shift ? '{' : '['; + case VKEY_OEM_5: + return shift ? '|' : '\\'; + case VKEY_OEM_6: + return shift ? '}' : ']'; + case VKEY_OEM_7: + return shift ? '"' : '\''; + default: + return 0; + } +} + +} // namespace ui diff --git a/ui/base/keycodes/keyboard_code_conversion.h b/ui/base/keycodes/keyboard_code_conversion.h new file mode 100644 index 0000000..afc7aad --- /dev/null +++ b/ui/base/keycodes/keyboard_code_conversion.h @@ -0,0 +1,38 @@ +// Copyright (c) 2011 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. + +#ifndef UI_BASE_KEYCODES_KEYBOARD_CODE_CONVERSION_H_ +#define UI_BASE_KEYCODES_KEYBOARD_CODE_CONVERSION_H_ + +#include "base/basictypes.h" +#include "ui/base/keycodes/keyboard_codes.h" +#include "ui/base/ui_export.h" + +namespace ui { + +// A helper function to get the character generated by a key event in a +// platform independent way. It supports control characters as well. +// It assumes a US keyboard layout is used, so it may only be used when there +// is no native event or no better way to get the character. +// For example, if a virtual keyboard implementation can only generate key +// events with key_code and flags information, then there is no way for us to +// determine the actual character that should be generate by the key. Because +// a key_code only represents a physical key on the keyboard, it has nothing +// to do with the actual character printed on that key. In such case, the only +// thing we can do is to assume that we are using a US keyboard and get the +// character according to US keyboard layout definition. +// If a virtual keyboard implementation wants to support other keyboard +// layouts, that may generate different text for a certain key than on a US +// keyboard, a special native event object should be introduced to carry extra +// information to help determine the correct character. +// Take XKeyEvent as an example, it contains not only keycode and modifier +// flags but also group and other extra XKB information to help determine the +// correct character. That's why we can use XLookupString() function to get +// the correct text generated by a X key event (See how is GetCharacter() +// implemented in event_x.cc). +UI_EXPORT uint16 GetCharacterFromKeyCode(KeyboardCode key_code, int flags); + +} // namespace ui + +#endif // UI_BASE_KEYCODES_KEYBOARD_CODE_CONVERSION_H_ |