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.
4
5#include "ui/views/focus/accelerator_handler.h"
6
7#include "ui/base/events/event.h"
8#include "ui/base/keycodes/keyboard_code_conversion_win.h"
9#include "ui/base/keycodes/keyboard_codes.h"
10#include "ui/views/focus/focus_manager.h"
11#include "ui/views/widget/widget.h"
12
13namespace views {
14
15AcceleratorHandler::AcceleratorHandler() {
16}
17
18bool AcceleratorHandler::Dispatch(const base::NativeEvent& msg) {
19  if (msg.message >= WM_KEYFIRST && msg.message <= WM_KEYLAST) {
20    Widget* widget = Widget::GetTopLevelWidgetForNativeView(msg.hwnd);
21    FocusManager* focus_manager = widget ? widget->GetFocusManager() : NULL;
22    if (focus_manager) {
23      switch (msg.message) {
24        case WM_KEYDOWN:
25        case WM_SYSKEYDOWN: {
26          ui::KeyEvent event(msg, false);
27          if (!focus_manager->OnKeyEvent(event)) {
28            // Record that this key is pressed so we can remember not to
29            // translate and dispatch the associated WM_KEYUP.
30            pressed_keys_.insert(msg.wParam);
31            return true;
32          }
33          break;
34        }
35        case WM_KEYUP:
36        case WM_SYSKEYUP: {
37          std::set<WPARAM>::iterator iter = pressed_keys_.find(msg.wParam);
38          if (iter != pressed_keys_.end()) {
39            // Don't translate/dispatch the KEYUP since we have eaten the
40            // associated KEYDOWN.
41            pressed_keys_.erase(iter);
42            return true;
43          }
44          break;
45        }
46      }
47    }
48  }
49
50  TranslateMessage(&msg);
51  DispatchMessage(&msg);
52  return true;
53}
54
55}  // namespace views
56