OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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 "chrome/browser/extensions/global_shortcut_listener_win.h" | |
6 | |
7 #include "base/win/win_util.h" | |
8 #include "content/public/browser/browser_thread.h" | |
9 #include "ui/base/accelerators/accelerator.h" | |
10 #include "ui/base/events/event_constants.h" | |
11 #include "ui/base/keycodes/keyboard_code_conversion_win.h" | |
zhchbin
2013/09/20 07:49:23
This file has been moved from base to events. s/ba
| |
12 | |
13 using content::BrowserThread; | |
14 | |
15 namespace { | |
16 | |
17 static base::LazyInstance<extensions::GlobalShortcutListenerWin> instance = | |
18 LAZY_INSTANCE_INITIALIZER; | |
19 | |
20 } // namespace | |
21 | |
22 namespace extensions { | |
23 | |
24 // static | |
25 GlobalShortcutListener* GlobalShortcutListener::GetInstance() { | |
26 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
27 return instance.Pointer(); | |
28 } | |
29 | |
30 GlobalShortcutListenerWin::GlobalShortcutListenerWin() | |
31 : keyboard_hook_(NULL), | |
32 is_hooking_(false) { | |
33 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
34 } | |
35 | |
36 GlobalShortcutListenerWin::~GlobalShortcutListenerWin() { | |
37 if (is_hooking_) | |
38 StopHooking(); | |
39 } | |
40 | |
41 void GlobalShortcutListenerWin::StartHooking() { | |
42 if (is_hooking_) | |
43 return; | |
44 | |
45 keyboard_hook_ = SetWindowsHookEx(WH_KEYBOARD_LL, &KeyboardHook, NULL, 0L); | |
zhchbin
2013/09/20 07:49:23
Do you mind explaining the reason that you chose u
Finnur
2013/09/20 13:01:12
I haven't fully made up my mind yet (and not fully
Finnur
2013/09/20 13:15:56
Well, I take that back: a window isn't required. B
zhchbin
2013/09/21 02:07:50
We can use base::win::MessageWindow to subscribe t
Finnur
2013/09/21 12:23:50
Yes, I came to the conclusion after responding to
| |
46 if (keyboard_hook_) | |
47 is_hooking_ = true; | |
48 } | |
49 | |
50 void GlobalShortcutListenerWin::StopHooking() { | |
51 if (!is_hooking_) | |
52 return; | |
53 | |
54 if (keyboard_hook_) | |
55 UnhookWindowsHookEx(keyboard_hook_); | |
56 keyboard_hook_ = NULL; | |
57 is_hooking_ = false; | |
58 } | |
59 | |
60 // static | |
61 LRESULT CALLBACK GlobalShortcutListenerWin::KeyboardHook(int code, | |
62 WPARAM w_param, | |
63 LPARAM l_param) { | |
64 if (code >= 0 && w_param == WM_KEYDOWN) { | |
65 int vkCode = reinterpret_cast<KBDLLHOOKSTRUCT*>(l_param)->vkCode; | |
66 int modifiers = 0; | |
67 modifiers |= base::win::IsShiftPressed() ? ui::EF_SHIFT_DOWN : 0; | |
68 modifiers |= base::win::IsCtrlPressed() ? ui::EF_CONTROL_DOWN : 0; | |
69 modifiers |= base::win::IsAltPressed() ? ui::EF_ALT_DOWN : 0; | |
70 ui::Accelerator accelerator( | |
71 ui::KeyboardCodeForWindowsKeyCode(vkCode), modifiers); | |
72 | |
73 if (instance.Get().NotifyKeyPressed(accelerator)) | |
74 return -1; // Prevent further processing. | |
75 } | |
76 | |
77 return CallNextHookEx(NULL, code, w_param, l_param); | |
78 } | |
79 | |
80 } // namespace extensions | |
OLD | NEW |