1 // Copyright (c) 2011 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 "ui/events/keycodes/keyboard_code_conversion.h"
7 #include "ui/events/event_constants.h"
11 uint16
GetCharacterFromKeyCode(KeyboardCode key_code
, int flags
) {
12 const bool ctrl
= (flags
& EF_CONTROL_DOWN
) != 0;
13 const bool shift
= (flags
& EF_SHIFT_DOWN
) != 0;
14 const bool upper
= shift
^ ((flags
& EF_CAPS_LOCK_DOWN
) != 0);
16 // Following Windows behavior to map ctrl-a ~ ctrl-z to \x01 ~ \x1A.
17 if (key_code
>= VKEY_A
&& key_code
<= VKEY_Z
)
18 return key_code
- VKEY_A
+ (ctrl
? 1 : (upper
? 'A' : 'a'));
20 // Other ctrl characters
23 // following graphics chars require shift key to input.
25 // ctrl-@ maps to \x00 (Null byte)
28 // ctrl-^ maps to \x1E (Record separator, Information separator two)
31 // ctrl-_ maps to \x1F (Unit separator, Information separator one)
34 // Returns 0 for all other keys to avoid inputting unexpected chars.
40 // ctrl-[ maps to \x1B (Escape)
43 // ctrl-\ maps to \x1C (File separator, Information separator four)
46 // ctrl-] maps to \x1D (Group separator, Information separator three)
49 // ctrl-Enter maps to \x0A (Line feed)
52 // Returns 0 for all other keys to avoid inputting unexpected chars.
60 if (key_code
== ui::VKEY_PROCESSKEY
)
64 if (key_code
>= VKEY_0
&& key_code
<= VKEY_9
) {
65 return shift
? ")!@#$%^&*("[key_code
- VKEY_0
] :
66 static_cast<uint16
>(key_code
);
67 } else if (key_code
>= VKEY_NUMPAD0
&& key_code
<= VKEY_NUMPAD9
) {
68 return key_code
- VKEY_NUMPAD0
+ '0';
89 return shift
? ':' : ';';
91 return shift
? '+' : '=';
93 return shift
? '<' : ',';
95 return shift
? '_' : '-';
97 return shift
? '>' : '.';
99 return shift
? '?' : '/';
101 return shift
? '~' : '`';
103 return shift
? '{' : '[';
105 return shift
? '|' : '\\';
107 return shift
? '}' : ']';
109 return shift
? '"' : '\'';