diff --git a/source/VolumeController.cpp b/source/VolumeController.cpp index f890eed..18e9d0d 100644 --- a/source/VolumeController.cpp +++ b/source/VolumeController.cpp @@ -27,8 +27,8 @@ using json = nlohmann::json; constexpr keycode default_up_keycode = VK_VOLUME_UP; constexpr keycode default_down_keycode = VK_VOLUME_DOWN; #else -constexpr keycode default_up_keycode = 100; -constexpr keycode default_down_keycode = 101; +constexpr keycode default_up_keycode = 57390; +constexpr keycode default_down_keycode = 57392; #endif VolumeController::VolumeController(const Config& config, Client& client) diff --git a/source/key_hooks.cpp b/source/key_hooks.cpp index 17c3f6a..19aae29 100644 --- a/source/key_hooks.cpp +++ b/source/key_hooks.cpp @@ -5,68 +5,34 @@ #include "key_hooks.h" #include - +#include #ifdef __linux__ # include -# include #endif -#ifdef _WIN32 -# include -# include -# include -# include -#endif #include "VolumeController.h" namespace spotify_volume_controller::key_hooks { namespace { -#ifdef _WIN32 -std::unique_ptr g_controller {}; // NOLINT -HHOOK hook; // NOLINT - -LRESULT CALLBACK volume_callback(int n_code, WPARAM w_param, LPARAM l_param) -{ - if (n_code < 0) { - return CallNextHookEx(nullptr, n_code, w_param, l_param); - } - - if (w_param == WM_KEYDOWN) { - auto* keyboard_struct = std::bit_cast(l_param); - if (keyboard_struct->vkCode == g_controller->volume_up_keycode()) { - g_controller->increase_volume(); - return 1; - } - if (keyboard_struct->vkCode == g_controller->volume_down_keycode()) { - g_controller->decrease_volume(); - return 1; - } - } - return CallNextHookEx(nullptr, n_code, w_param, l_param); -} -LRESULT CALLBACK print_v_key(int n_code, WPARAM w_param, LPARAM l_param) -{ - if (n_code < 0) { - return CallNextHookEx(nullptr, n_code, w_param, l_param); - } - if (w_param == WM_KEYDOWN) { - auto* keyboard_struct = std::bit_cast(l_param); - fmt::println("{}", keyboard_struct->vkCode); - } - return CallNextHookEx(nullptr, n_code, w_param, l_param); -} -#else void volume_callback(uiohook_event* const event, void* user_data) { auto* controller = static_cast(user_data); switch (event->type) { case EVENT_KEY_PRESSED: { - if (event->data.keyboard.keycode == controller->volume_up_keycode()) { + // Use rawcodes on Windows for backward compatibility +#ifdef _WIN32 + auto const keycode = event->data.keyboard.rawcode; +#else + auto const keycode = event->data.keyboard.keycode; +#endif + if (keycode == controller->volume_up_keycode()) { controller->increase_volume(); - } else if (event->data.keyboard.keycode == controller->volume_down_keycode()) { + event->mask |= MASK_CONSUMED; + } else if (keycode == controller->volume_down_keycode()) { controller->decrease_volume(); + event->mask |= MASK_CONSUMED; } break; } @@ -90,28 +56,6 @@ void print_callback(uiohook_event* const event, void* user_data) } } -#endif -} // namespace -#ifdef _WIN32 -void start_volume_hook(std::unique_ptr controller) -{ - g_controller = std::move(controller); - hook = SetWindowsHookExA(WH_KEYBOARD_LL, volume_callback, GetModuleHandle(nullptr), 0); - MSG msg; - while (GetMessage(&msg, nullptr, 0, 0)) {}; - UnhookWindowsHookEx(hook); -} - -void start_print_vkey() -{ - hook = SetWindowsHookExA(WH_KEYBOARD_LL, print_v_key, GetModuleHandle(nullptr), 0); - MSG msg; - while (GetMessage(&msg, nullptr, 0, 0)) {}; - - UnhookWindowsHookEx(hook); -} -#else - void print_hook_run_status(int status) { switch (status) { @@ -150,6 +94,8 @@ void print_hook_run_status(int status) } } +} // namespace + void start_volume_hook(std::unique_ptr controller) { hook_set_dispatch_proc(&volume_callback, controller.get()); @@ -160,6 +106,5 @@ void start_print_vkey() hook_set_dispatch_proc(&print_callback, nullptr); print_hook_run_status(hook_run()); } -#endif } // namespace spotify_volume_controller::key_hooks