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 "ui/views/controls/webview/unhandled_keyboard_event_handler.h"
7 #include "content/public/browser/native_web_keyboard_event.h"
8 #include "ui/views/focus/focus_manager.h"
12 UnhandledKeyboardEventHandler::UnhandledKeyboardEventHandler()
13 : ignore_next_char_event_(false) {
16 UnhandledKeyboardEventHandler::~UnhandledKeyboardEventHandler() {
19 void UnhandledKeyboardEventHandler::HandleKeyboardEvent(
20 const content::NativeWebKeyboardEvent
& event
,
21 FocusManager
* focus_manager
) {
26 // Previous calls to TranslateMessage can generate Char events as well as
27 // RawKeyDown events, even if the latter triggered an accelerator. In these
28 // cases, we discard the Char events.
29 if (event
.type
== blink::WebInputEvent::Char
&& ignore_next_char_event_
) {
30 ignore_next_char_event_
= false;
33 // It's necessary to reset this flag, because a RawKeyDown event may not
34 // always generate a Char event.
35 ignore_next_char_event_
= false;
37 if (event
.type
== blink::WebInputEvent::RawKeyDown
) {
38 ui::Accelerator
accelerator(
39 static_cast<ui::KeyboardCode
>(event
.windowsKeyCode
),
40 content::GetModifiersFromNativeWebKeyboardEvent(event
));
42 // This is tricky: we want to set ignore_next_char_event_ if
43 // ProcessAccelerator returns true. But ProcessAccelerator might delete
44 // |this| if the accelerator is a "close tab" one. So we speculatively
45 // set the flag and fix it if no event was handled.
46 ignore_next_char_event_
= true;
48 if (focus_manager
->ProcessAccelerator(accelerator
)) {
52 // ProcessAccelerator didn't handle the accelerator, so we know both
53 // that |this| is still valid, and that we didn't want to set the flag.
54 ignore_next_char_event_
= false;
57 if (event
.os_event
&& !event
.skip_in_browser
)
58 HandleNativeKeyboardEvent(event
.os_event
, focus_manager
);