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
3 // found in the LICENSE file.
5 #include "remoting/client/key_event_mapper.h"
7 #include "remoting/proto/event.pb.h"
11 KeyEventMapper::KeyEventMapper() {
14 KeyEventMapper::KeyEventMapper(InputStub
* stub
) : protocol::InputFilter(stub
) {
17 KeyEventMapper::~KeyEventMapper() {
20 void KeyEventMapper::SetTrapCallback(KeyTrapCallback callback
) {
21 trap_callback
= callback
;
24 void KeyEventMapper::TrapKey(uint32 usb_keycode
, bool trap_key
) {
26 trapped_keys
.insert(usb_keycode
);
28 trapped_keys
.erase(usb_keycode
);
32 void KeyEventMapper::RemapKey(uint32 in_usb_keycode
, uint32 out_usb_keycode
) {
33 if (in_usb_keycode
== out_usb_keycode
) {
34 mapped_keys
.erase(in_usb_keycode
);
36 mapped_keys
[in_usb_keycode
] = out_usb_keycode
;
40 void KeyEventMapper::InjectKeyEvent(const protocol::KeyEvent
& event
) {
41 if (event
.has_usb_keycode()) {
42 // Deliver trapped keys to the callback, not the next stub.
43 if (!trap_callback
.is_null() && event
.has_pressed() &&
44 (trapped_keys
.find(event
.usb_keycode()) != trapped_keys
.end())) {
45 trap_callback
.Run(event
);
49 // Re-map mapped keys to the new value before passing them on.
50 std::map
<uint32
,uint32
>::iterator mapped
=
51 mapped_keys
.find(event
.usb_keycode());
52 if (mapped
!= mapped_keys
.end()) {
53 protocol::KeyEvent
new_event(event
);
54 new_event
.set_usb_keycode(mapped
->second
);
55 InputFilter::InjectKeyEvent(new_event
);
60 InputFilter::InjectKeyEvent(event
);
63 } // namespace remoting