From: Yeechan Lu Date: Sun, 27 Jul 2014 01:28:14 +0000 (+0800) Subject: win: Implement global keyboard shortcut API. X-Git-Tag: v0.15.2~7^2~5 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=c2c5111d75a9f6a4d37c28e25355b0805b116185;p=electron.git win: Implement global keyboard shortcut API. --- diff --git a/atom.gyp b/atom.gyp index 230fdb64..3fb2cbd5 100644 --- a/atom.gyp +++ b/atom.gyp @@ -238,6 +238,8 @@ 'chromium_src/chrome/browser/ui/shortcut/global_shortcut_listener.h', 'chromium_src/chrome/browser/ui/shortcut/global_shortcut_listener_mac.mm', 'chromium_src/chrome/browser/ui/shortcut/global_shortcut_listener_mac.h', + 'chromium_src/chrome/browser/ui/shortcut/global_shortcut_listener_win.cc', + 'chromium_src/chrome/browser/ui/shortcut/global_shortcut_listener_win.h', '<@(native_mate_files)', ], 'framework_sources': [ diff --git a/chromium_src/chrome/browser/ui/shortcut/global_shortcut_listener_win.cc b/chromium_src/chrome/browser/ui/shortcut/global_shortcut_listener_win.cc new file mode 100644 index 00000000..b31fb362 --- /dev/null +++ b/chromium_src/chrome/browser/ui/shortcut/global_shortcut_listener_win.cc @@ -0,0 +1,117 @@ +// Copyright (c) 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/ui/shortcut/global_shortcut_listener_win.h" + +#include "base/win/win_util.h" +#include "content/public/browser/browser_thread.h" +#include "ui/base/accelerators/accelerator.h" +#include "ui/events/event_constants.h" +#include "ui/events/keycodes/keyboard_code_conversion_win.h" + +using content::BrowserThread; + +namespace atom { + +namespace api { + +// static +GlobalShortcutListener* GlobalShortcutListener::GetInstance() { + CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); + static GlobalShortcutListenerWin* instance = + new GlobalShortcutListenerWin(); + return instance; +} + +GlobalShortcutListenerWin::GlobalShortcutListenerWin() + : is_listening_(false) { + CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); +} + +GlobalShortcutListenerWin::~GlobalShortcutListenerWin() { + if (is_listening_) + StopListening(); +} + +bool GlobalShortcutListenerWin::IsAcceleratorRegistered( + const ui::Accelerator& accelerator) { + return hotkey_ids_.find(accelerator) != hotkey_ids_.end(); +} + +void GlobalShortcutListenerWin::StartListening() { + DCHECK(!is_listening_); // Don't start twice. + DCHECK(!hotkey_ids_.empty()); // Also don't start if no hotkey is registered. + gfx::SingletonHwnd::GetInstance()->AddObserver(this); + is_listening_ = true; +} + +void GlobalShortcutListenerWin::StopListening() { + DCHECK(is_listening_); // No point if we are not already listening. + DCHECK(hotkey_ids_.empty()); // Make sure the map is clean before ending. + gfx::SingletonHwnd::GetInstance()->RemoveObserver(this); + is_listening_ = false; +} + +void GlobalShortcutListenerWin::OnWndProc(HWND hwnd, + UINT message, + WPARAM wparam, + LPARAM lparam) { + if (message != WM_HOTKEY) + return; + + int key_code = HIWORD(lparam); + int modifiers = 0; + modifiers |= (LOWORD(lparam) & MOD_SHIFT) ? ui::EF_SHIFT_DOWN : 0; + modifiers |= (LOWORD(lparam) & MOD_ALT) ? ui::EF_ALT_DOWN : 0; + modifiers |= (LOWORD(lparam) & MOD_CONTROL) ? ui::EF_CONTROL_DOWN : 0; + ui::Accelerator accelerator( + ui::KeyboardCodeForWindowsKeyCode(key_code), modifiers); + + NotifyKeyPressed(accelerator); +} + +bool GlobalShortcutListenerWin::RegisterAcceleratorImpl( + const ui::Accelerator& accelerator) { + DCHECK(hotkey_ids_.find(accelerator) == hotkey_ids_.end()); + + int modifiers = 0; + modifiers |= accelerator.IsShiftDown() ? MOD_SHIFT : 0; + modifiers |= accelerator.IsCtrlDown() ? MOD_CONTROL : 0; + modifiers |= accelerator.IsAltDown() ? MOD_ALT : 0; + static int hotkey_id = 0; + //bool success = false; + bool success = !!RegisterHotKey( + gfx::SingletonHwnd::GetInstance()->hwnd(), + hotkey_id, + modifiers, + accelerator.key_code()); + + if (!success) { + // Most likely error: 1409 (Hotkey already registered). + return false; + } + + hotkey_ids_[accelerator] = hotkey_id++; + return true; +} + +void GlobalShortcutListenerWin::UnregisterAcceleratorImpl( + const ui::Accelerator& accelerator) { + HotkeyIdMap::iterator it = hotkey_ids_.find(accelerator); + DCHECK(it != hotkey_ids_.end()); + + //bool success = false; + gfx::SingletonHwnd::GetInstance(); + bool success = !!UnregisterHotKey( + gfx::SingletonHwnd::GetInstance()->hwnd(), it->second); + // This call should always succeed, as long as we pass in the right HWND and + // an id we've used to register before. + DCHECK(success); + + hotkey_ids_.erase(it); +} + +} // namespace api + +} // namespace atom diff --git a/chromium_src/chrome/browser/ui/shortcut/global_shortcut_listener_win.h b/chromium_src/chrome/browser/ui/shortcut/global_shortcut_listener_win.h new file mode 100644 index 00000000..c85ee108 --- /dev/null +++ b/chromium_src/chrome/browser/ui/shortcut/global_shortcut_listener_win.h @@ -0,0 +1,58 @@ +// Copyright (c) 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_UI_SHORTCUT_GLOBAL_SHORTCUT_LISTENER_WIN_H_ +#define CHROME_BROWSER_UI_SHORTCUT_GLOBAL_SHORTCUT_LISTENER_WIN_H_ + +#include + +#include "chrome/browser/ui/shortcut/global_shortcut_listener.h" +#include "ui/gfx/win/singleton_hwnd.h" + +namespace atom { + +namespace api { + +// Windows-specific implementation of the GlobalShortcutListener class that +// listens for global shortcuts. Handles setting up a keyboard hook and +// forwarding its output to the base class for processing. +class GlobalShortcutListenerWin : public GlobalShortcutListener, + public gfx::SingletonHwnd::Observer { + public: + GlobalShortcutListenerWin(); + virtual ~GlobalShortcutListenerWin(); + + virtual bool IsAcceleratorRegistered( + const ui::Accelerator& accelerator) OVERRIDE; + + private: + // The implementation of our Window Proc, called by SingletonHwnd. + virtual void OnWndProc(HWND hwnd, + UINT message, + WPARAM wparam, + LPARAM lparam) OVERRIDE; + + // GlobalShortcutListener implementation. + virtual void StartListening() OVERRIDE; + virtual void StopListening() OVERRIDE; + virtual bool RegisterAcceleratorImpl( + const ui::Accelerator& accelerator) OVERRIDE; + virtual void UnregisterAcceleratorImpl( + const ui::Accelerator& accelerator) OVERRIDE; + + // Whether this object is listening for global shortcuts. + bool is_listening_; + + // A map of registered accelerators and their registration ids. + typedef std::map HotkeyIdMap; + HotkeyIdMap hotkey_ids_; + + DISALLOW_COPY_AND_ASSIGN(GlobalShortcutListenerWin); +}; + +} // namespace api + +} // namespace atom + +#endif // CHROME_BROWSER_UI_SHORTCUT_GLOBAL_SHORTCUT_LISTENER_WIN_H_