• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "ui/events/keycodes/keyboard_code_conversion.h"
6 
7 #include "ui/events/event_constants.h"
8 
9 namespace ui {
10 
GetCharacterFromKeyCode(KeyboardCode key_code,int flags)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);
15 
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'));
19 
20   // Other ctrl characters
21   if (ctrl) {
22     if (shift) {
23       // following graphics chars require shift key to input.
24       switch (key_code) {
25         // ctrl-@ maps to \x00 (Null byte)
26         case VKEY_2:
27           return 0;
28         // ctrl-^ maps to \x1E (Record separator, Information separator two)
29         case VKEY_6:
30           return 0x1E;
31         // ctrl-_ maps to \x1F (Unit separator, Information separator one)
32         case VKEY_OEM_MINUS:
33           return 0x1F;
34         // Returns 0 for all other keys to avoid inputting unexpected chars.
35         default:
36           return 0;
37       }
38     } else {
39       switch (key_code) {
40         // ctrl-[ maps to \x1B (Escape)
41         case VKEY_OEM_4:
42           return 0x1B;
43         // ctrl-\ maps to \x1C (File separator, Information separator four)
44         case VKEY_OEM_5:
45           return 0x1C;
46         // ctrl-] maps to \x1D (Group separator, Information separator three)
47         case VKEY_OEM_6:
48           return 0x1D;
49         // ctrl-Enter maps to \x0A (Line feed)
50         case VKEY_RETURN:
51           return 0x0A;
52         // Returns 0 for all other keys to avoid inputting unexpected chars.
53         default:
54           return 0;
55       }
56     }
57   }
58 
59   // For IME support.
60   if (key_code == ui::VKEY_PROCESSKEY)
61     return 0xE5;
62 
63   // Normal characters
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';
69   }
70 
71   switch (key_code) {
72     case VKEY_TAB:
73       return '\t';
74     case VKEY_RETURN:
75       return '\r';
76     case VKEY_MULTIPLY:
77       return '*';
78     case VKEY_ADD:
79       return '+';
80     case VKEY_SUBTRACT:
81       return '-';
82     case VKEY_DECIMAL:
83       return '.';
84     case VKEY_DIVIDE:
85       return '/';
86     case VKEY_SPACE:
87       return ' ';
88     case VKEY_OEM_1:
89       return shift ? ':' : ';';
90     case VKEY_OEM_PLUS:
91       return shift ? '+' : '=';
92     case VKEY_OEM_COMMA:
93       return shift ? '<' : ',';
94     case VKEY_OEM_MINUS:
95       return shift ? '_' : '-';
96     case VKEY_OEM_PERIOD:
97       return shift ? '>' : '.';
98     case VKEY_OEM_2:
99       return shift ? '?' : '/';
100     case VKEY_OEM_3:
101       return shift ? '~' : '`';
102     case VKEY_OEM_4:
103       return shift ? '{' : '[';
104     case VKEY_OEM_5:
105       return shift ? '|' : '\\';
106     case VKEY_OEM_6:
107       return shift ? '}' : ']';
108     case VKEY_OEM_7:
109       return shift ? '"' : '\'';
110     default:
111       return 0;
112   }
113 }
114 
115 }  // namespace ui
116