KeyboardListener.h
  1  #pragma once
  2  
  3  #include "KeyboardListener.g.h"
  4  #include <mutex>
  5  #include <spdlog/stopwatch.h>
  6  
  7  namespace winrt::PowerToys::PowerAccentKeyboardService::implementation
  8  {
  9      enum PowerAccentActivationKey
 10      {
 11          LeftRightArrow,
 12          Space,
 13          Both,
 14      };
 15  
 16      struct PowerAccentSettings
 17      {
 18          PowerAccentActivationKey activationKey{ PowerAccentActivationKey::Both };
 19          bool doNotActivateOnGameMode{ true };
 20          std::chrono::milliseconds inputTime{ 300 }; // Should match with UI.Library.PowerAccentSettings.DefaultInputTimeMs
 21          std::vector<std::wstring> excludedApps;
 22      };
 23  
 24      struct KeyboardListener : KeyboardListenerT<KeyboardListener>
 25      {
 26          using LetterKey = winrt::PowerToys::PowerAccentKeyboardService::LetterKey;
 27          using TriggerKey = winrt::PowerToys::PowerAccentKeyboardService::TriggerKey;
 28          using InputType = winrt::PowerToys::PowerAccentKeyboardService::InputType;
 29  
 30          KeyboardListener();
 31  
 32          void KeyboardListener::InitHook();
 33          void KeyboardListener::UnInitHook();
 34          void SetShowToolbarEvent(ShowToolbar showToolbarEvent);
 35          void SetHideToolbarEvent(HideToolbar hideToolbarEvent);
 36          void SetNextCharEvent(NextChar NextCharEvent);
 37          void SetIsLanguageLetterDelegate(IsLanguageLetter IsLanguageLetterDelegate);
 38  
 39          void UpdateActivationKey(int32_t activationKey);
 40          void UpdateDoNotActivateOnGameMode(bool doNotActivateOnGameMode);
 41          void UpdateInputTime(int32_t inputTime);
 42          void UpdateExcludedApps(std::wstring_view excludedApps);
 43  
 44          static LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam);
 45  
 46      private:
 47          bool OnKeyDown(KBDLLHOOKSTRUCT info) noexcept;
 48          bool OnKeyUp(KBDLLHOOKSTRUCT info) noexcept;
 49          bool IsSuppressedByGameMode();
 50          bool IsForegroundAppExcluded();
 51  
 52          static inline KeyboardListener* s_instance;
 53          HHOOK s_llKeyboardHook = nullptr;
 54          bool m_toolbarVisible;
 55          PowerAccentSettings m_settings;
 56          std::function<void(LetterKey)> m_showToolbarCb;
 57          std::function<void(InputType)> m_hideToolbarCb;
 58          std::function<void(TriggerKey, bool)> m_nextCharCb;
 59          std::function<bool(LetterKey)> m_isLanguageLetterCb;
 60          bool m_triggeredWithSpace;
 61          bool m_triggeredWithLeftArrow;
 62          bool m_triggeredWithRightArrow;
 63          spdlog::stopwatch m_stopwatch;
 64          bool m_leftShiftPressed;
 65          bool m_rightShiftPressed;
 66  
 67          std::mutex m_mutex_excluded_apps;
 68          std::pair<HWND, bool> m_prevForegroundAppExcl{ NULL, false };
 69  
 70          static inline const std::vector<LetterKey> letters = { LetterKey::VK_0,
 71                                                                 LetterKey::VK_1,
 72                                                                 LetterKey::VK_2,
 73                                                                 LetterKey::VK_3,
 74                                                                 LetterKey::VK_4,
 75                                                                 LetterKey::VK_5,
 76                                                                 LetterKey::VK_6,
 77                                                                 LetterKey::VK_7,
 78                                                                 LetterKey::VK_8,
 79                                                                 LetterKey::VK_9,
 80                                                                 LetterKey::VK_A,
 81                                                                 LetterKey::VK_B,
 82                                                                 LetterKey::VK_C,
 83                                                                 LetterKey::VK_D,
 84                                                                 LetterKey::VK_E,
 85                                                                 LetterKey::VK_F,
 86                                                                 LetterKey::VK_G,
 87                                                                 LetterKey::VK_H,
 88                                                                 LetterKey::VK_I,
 89                                                                 LetterKey::VK_J,
 90                                                                 LetterKey::VK_K,
 91                                                                 LetterKey::VK_L,
 92                                                                 LetterKey::VK_M,
 93                                                                 LetterKey::VK_N,
 94                                                                 LetterKey::VK_O,
 95                                                                 LetterKey::VK_P,
 96                                                                 LetterKey::VK_Q,
 97                                                                 LetterKey::VK_R,
 98                                                                 LetterKey::VK_S,
 99                                                                 LetterKey::VK_T,
100                                                                 LetterKey::VK_U,
101                                                                 LetterKey::VK_V,
102                                                                 LetterKey::VK_W,
103                                                                 LetterKey::VK_X,
104                                                                 LetterKey::VK_Y,
105                                                                 LetterKey::VK_Z,
106                                                                 LetterKey::VK_PLUS,
107                                                                 LetterKey::VK_COMMA,
108                                                                 LetterKey::VK_PERIOD,
109                                                                 LetterKey::VK_MINUS,
110                                                                 LetterKey::VK_SLASH_,
111                                                                 LetterKey::VK_DIVIDE_,
112                                                                 LetterKey::VK_MULTIPLY_,
113                                                                 LetterKey::VK_BACKSLASH, };
114          LetterKey letterPressed{};
115  
116          static inline const std::vector<TriggerKey> triggers = { TriggerKey::Right, TriggerKey::Left, TriggerKey::Space };
117      };
118  }
119  
120  namespace winrt::PowerToys::PowerAccentKeyboardService::factory_implementation
121  {
122      struct KeyboardListener : KeyboardListenerT<KeyboardListener, implementation::KeyboardListener>
123      {
124      };
125  }