summaryrefslogtreecommitdiffstats
path: root/tools/memory_watcher/hotkey.h
blob: c0d0938f0b643536517c1b0e9a3566a3a3020ee1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright (c) 2009 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 TOOLS_MEMORY_WATCHER_HOTKEY_H_
#define TOOLS_MEMORY_WATCHER_HOTKEY_H_

#include "base/gfx/rect.h"
#include "app/win/window_impl.h"

// HotKey handler.
// Programs wishing to register a hotkey can use this.
class HotKeyHandler : public app::WindowImpl {
 public:
  HotKeyHandler(UINT modifiers, UINT vk)
    : modifiers_(modifiers),
      vkey_(vk) {
    Start();
  }
  ~HotKeyHandler() { Stop(); }

  BEGIN_MSG_MAP_EX(HotKeyHandler)
    MESSAGE_HANDLER(WM_HOTKEY, OnHotKey)
  END_MSG_MAP()

 private:
  static const int hotkey_id = 0x0000baba;

  bool Start() {
    set_window_style(WS_POPUP);
    Init(NULL, gfx::Rect());
    return RegisterHotKey(hwnd(), hotkey_id, modifiers_, vkey_) == TRUE;
  }

  void Stop() {
    UnregisterHotKey(hwnd(), hotkey_id);
    DestroyWindow(hwnd());
  }

  // Handle the registered Hotkey being pressed.
  virtual LRESULT OnHotKey(UINT /*uMsg*/, WPARAM /*wParam*/,
                           LPARAM /*lParam*/, BOOL& bHandled) = 0;

  UINT modifiers_;
  UINT vkey_;
};

#endif  // TOOLS_MEMORY_WATCHER_HOTKEY_H_