/ src / modules / cmdpal / CmdPalKeyboardService / KeyboardListener.h
KeyboardListener.h
 1  #pragma once
 2  
 3  #include "KeyboardListener.g.h"
 4  #include <mutex>
 5  #include <spdlog/stopwatch.h>
 6  #include <set>
 7  
 8  namespace winrt::CmdPalKeyboardService::implementation
 9  {
10      struct KeyboardListener : KeyboardListenerT<KeyboardListener>
11      {
12          struct Hotkey
13          {
14              bool win = false;
15              bool ctrl = false;
16              bool shift = false;
17              bool alt = false;
18              unsigned char key = 0;
19  
20              std::strong_ordering operator<=>(const Hotkey&) const = default;
21          };
22  
23          struct HotkeyDescriptor
24          {
25              Hotkey hotkey;
26              std::wstring id;
27  
28              bool operator<(const HotkeyDescriptor& other) const
29              {
30                  return hotkey < other.hotkey;
31              };
32          };
33  
34          KeyboardListener();
35  
36          void Start();
37          void Stop();
38          void SetHotkeyAction(bool win, bool ctrl, bool shift, bool alt, uint8_t key, hstring const& id);
39          void ClearHotkey(hstring const& id);
40          void ClearHotkeys();
41          void SetProcessCommand(ProcessCommand processCommand);
42  
43          static LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam);
44  
45      private:
46          LRESULT CALLBACK DoLowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam);
47  
48          static inline KeyboardListener* s_instance;
49          HHOOK s_llKeyboardHook = nullptr;
50  
51          // Max DWORD for key code to disable keys.
52          const DWORD VK_DISABLED = 0x100;
53          DWORD vkCodePressed = VK_DISABLED;
54  
55          std::multiset<HotkeyDescriptor> hotkeyDescriptors;
56          std::mutex mutex;
57  
58          std::function<void(hstring const&)> m_processCommandCb;
59      };
60  }
61  
62  namespace winrt::CmdPalKeyboardService::factory_implementation
63  {
64      struct KeyboardListener : KeyboardListenerT<KeyboardListener, implementation::KeyboardListener>
65      {
66      };
67  }