OLD | NEW |
| (Empty) |
1 // Copyright 2016 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 #ifndef ASH_COMMON_KEY_EVENT_WATCHER_H_ | |
6 #define ASH_COMMON_KEY_EVENT_WATCHER_H_ | |
7 | |
8 #include <map> | |
9 | |
10 #include "ash/ash_export.h" | |
11 #include "base/callback.h" | |
12 #include "base/macros.h" | |
13 #include "ui/base/accelerators/accelerator.h" | |
14 | |
15 namespace ui { | |
16 class KeyEvent; | |
17 } | |
18 | |
19 namespace ash { | |
20 | |
21 // This class watches the key event given in the form of accelerator | |
22 // and calls the registered callback. | |
23 class ASH_EXPORT KeyEventWatcher { | |
24 public: | |
25 using KeyEventCallback = base::Callback<void(const ui::KeyEvent&)>; | |
26 | |
27 KeyEventWatcher(); | |
28 virtual ~KeyEventWatcher(); | |
29 | |
30 // Add a callback that is called when the accelerator |accel| is | |
31 // matched. The key event will be consumed if a matching accelerator | |
32 // is found. | |
33 void AddKeyEventCallback(const ui::Accelerator& accel, | |
34 const KeyEventCallback& callback); | |
35 | |
36 protected: | |
37 // Finds and calls the registered callback whose accelerator matches | |
38 // the |event|. Returns false if no accelerator is matched. | |
39 bool HandleKeyEvent(const ui::KeyEvent& event); | |
40 | |
41 private: | |
42 std::map<ui::Accelerator, KeyEventCallback> callback_map_; | |
43 | |
44 DISALLOW_COPY_AND_ASSIGN(KeyEventWatcher); | |
45 }; | |
46 | |
47 } // namespace ash | |
48 | |
49 #endif // ASH_COMMON_KEY_EVENT_WATCHER_H_ | |
OLD | NEW |