Last active
October 3, 2017 05:58
-
-
Save yulon/24787c00ed4138acd6567944065694c2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct _wh_keyboard_flags { | |
| unsigned repeat: 16; | |
| unsigned scan_code: 8; | |
| unsigned extended: 1; | |
| unsigned _reserved: 4; | |
| unsigned alt_down: 1; | |
| unsigned previous: 1; | |
| unsigned up: 1; | |
| }; | |
| LRESULT CALLBACK _wh_keyboard_proc(int nCode, WPARAM wParam, LPARAM lParam) { | |
| if (nCode >= 0) { | |
| for (auto &listener : _key_listeners) { | |
| if (!listener(wParam, (reinterpret_cast<_wh_keyboard_flags *>(&lParam)->up == 0), static_cast<size_t>(reinterpret_cast<_wh_keyboard_flags *>(&lParam)->repeat))) { | |
| return false; | |
| } | |
| } | |
| } | |
| return CallNextHookEx(nullptr, nCode, wParam, lParam); | |
| } | |
| LRESULT CALLBACK _wh_keyboard_ll_proc(int nCode, WPARAM wParam, LPARAM lParam) { | |
| if (nCode >= 0 && GetForegroundWindow() == window) { | |
| switch (wParam) { | |
| case WM_KEYDOWN: | |
| for (auto &listener : _key_listeners) { | |
| if (!listener(reinterpret_cast<LPKBDLLHOOKSTRUCT>(&lParam)->vkCode, true, 0)) { | |
| return true; | |
| } | |
| } | |
| break; | |
| case WM_KEYUP: | |
| for (auto &listener : _key_listeners) { | |
| if (!listener(reinterpret_cast<LPKBDLLHOOKSTRUCT>(&lParam)->vkCode, false, 0)) { | |
| return true; | |
| } | |
| } | |
| } | |
| } | |
| return CallNextHookEx(nullptr, nCode, wParam, lParam); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment