OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/client/plugin/mac_key_event_processor.h" | 5 #include "remoting/client/plugin/mac_key_event_processor.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
| 9 #include "base/logging.h" |
| 10 |
9 namespace remoting { | 11 namespace remoting { |
10 | 12 |
11 namespace { | 13 namespace { |
12 | 14 |
13 // A list of known keycodes. | 15 const unsigned int kUsbLeftControl = 0x0700e0; |
14 const int kShift = 16; | 16 const unsigned int kUsbLeftShift = 0x0700e1; |
15 const int kControl = 17; | 17 const unsigned int kUsbLeftOption = 0x0700e2; |
16 const int kOption = 18; | 18 const unsigned int kUsbLeftCmd = 0x0700e3; |
17 const int kCapsLock = 20; | 19 const unsigned int kUsbRightControl = 0x0700e4; |
18 const int kLeftCmd = 91; | 20 const unsigned int kUsbRightShift = 0x0700e5; |
19 const int kRightCmd = 93; | 21 const unsigned int kUsbRightOption = 0x0700e6; |
| 22 const unsigned int kUsbRightCmd = 0x0700e7; |
| 23 const unsigned int kUsbTab = 0x07002b; |
20 | 24 |
21 } // namespace | 25 } // namespace |
22 | 26 |
23 MacKeyEventProcessor::MacKeyEventProcessor(protocol::InputStub* input_stub) | 27 MacKeyEventProcessor::MacKeyEventProcessor(protocol::InputStub* input_stub) |
24 : protocol::InputFilter(input_stub) { | 28 : protocol::InputFilter(input_stub) { |
25 } | 29 } |
26 | 30 |
27 MacKeyEventProcessor::~MacKeyEventProcessor() { | 31 MacKeyEventProcessor::~MacKeyEventProcessor() { |
28 } | 32 } |
29 | 33 |
30 void MacKeyEventProcessor::InjectKeyEvent(const protocol::KeyEvent& event) { | 34 void MacKeyEventProcessor::InjectKeyEvent(const protocol::KeyEvent& event) { |
31 if (event.pressed()) { | 35 DCHECK(event.has_usb_keycode()); |
32 key_pressed_map_[event.keycode()] = event; | 36 |
33 } else { | 37 bool is_special_key = event.usb_keycode() == kUsbLeftControl || |
34 key_pressed_map_.erase(event.keycode()); | 38 event.usb_keycode() == kUsbLeftShift || |
35 if (event.keycode() == kLeftCmd || event.keycode() == kRightCmd) | 39 event.usb_keycode() == kUsbLeftOption || |
36 GenerateKeyupEvents(); | 40 event.usb_keycode() == kUsbRightControl || |
| 41 event.usb_keycode() == kUsbRightShift || |
| 42 event.usb_keycode() == kUsbRightOption || |
| 43 event.usb_keycode() == kUsbTab; |
| 44 |
| 45 bool is_cmd_key = event.usb_keycode() == kUsbLeftCmd || |
| 46 event.usb_keycode() == kUsbRightCmd; |
| 47 |
| 48 if (!is_cmd_key && !is_special_key) { |
| 49 if (event.pressed()) { |
| 50 key_pressed_map_[event.usb_keycode()] = event; |
| 51 } else { |
| 52 key_pressed_map_.erase(event.usb_keycode()); |
| 53 } |
| 54 } |
| 55 |
| 56 if (is_cmd_key && !event.pressed()) { |
| 57 GenerateKeyupEvents(); |
37 } | 58 } |
38 | 59 |
39 InputFilter::InjectKeyEvent(event); | 60 InputFilter::InjectKeyEvent(event); |
40 } | 61 } |
41 | 62 |
42 int MacKeyEventProcessor::NumberOfPressedKeys() const { | |
43 return key_pressed_map_.size(); | |
44 } | |
45 | |
46 void MacKeyEventProcessor::GenerateKeyupEvents() { | 63 void MacKeyEventProcessor::GenerateKeyupEvents() { |
47 // A list of key codes to be erased from |key_pressed_map_|. | 64 // A list of key codes to be erased from |key_pressed_map_|. |
48 typedef std::vector<int> KeycodeList; | 65 typedef std::vector<int> KeycodeList; |
49 KeycodeList keycodes; | 66 KeycodeList keycodes; |
50 | 67 |
51 for (KeyPressedMap::iterator i = key_pressed_map_.begin(); | 68 for (KeyPressedMap::iterator i = key_pressed_map_.begin(); |
52 i != key_pressed_map_.end(); ++i) { | 69 i != key_pressed_map_.end(); ++i) { |
53 const int keycode = i->first; | 70 const int keycode = i->first; |
54 | 71 |
55 if (keycode == kCapsLock || keycode == kOption || | |
56 keycode == kControl || keycode == kShift || | |
57 keycode == kLeftCmd || keycode == kRightCmd) { | |
58 continue; | |
59 } | |
60 | |
61 keycodes.push_back(keycode); | 72 keycodes.push_back(keycode); |
62 protocol::KeyEvent event = i->second; | 73 protocol::KeyEvent event = i->second; |
63 event.set_pressed(false); | 74 event.set_pressed(false); |
64 InputFilter::InjectKeyEvent(event); | 75 InputFilter::InjectKeyEvent(event); |
65 } | 76 } |
66 | 77 |
67 for (KeycodeList::iterator i = keycodes.begin(); i != keycodes.end(); ++i) { | 78 for (KeycodeList::iterator i = keycodes.begin(); i != keycodes.end(); ++i) { |
68 key_pressed_map_.erase(*i); | 79 key_pressed_map_.erase(*i); |
69 } | 80 } |
70 } | 81 } |
71 | 82 |
72 } // namespace remoting | 83 } // namespace remoting |
OLD | NEW |