-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect Shortcut: Hold Esc/Enter to Cancel/Accept
Bypass shorcut/single key remapping by holding the navigation keys
- Loading branch information
Tomas Raies
committed
Apr 15, 2020
1 parent
95eb17b
commit e9e3a3b
Showing
10 changed files
with
413 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
#include "pch.h" | ||
#include "KeyDelay.h" | ||
|
||
#define LONG_PRESS_DELAY_MILLIS 900 | ||
#define ON_HOLD_WAIT_TIMEOUT_MILLIS 50 | ||
|
||
KeyDelay::~KeyDelay() | ||
{ | ||
_quit = true; | ||
_cv.notify_all(); | ||
_delayThread.join(); | ||
} | ||
|
||
void KeyDelay::KeyEvent(LowlevelKeyboardEvent* ev) | ||
{ | ||
std::lock_guard guard(_queueMutex); | ||
_queue.push({ ev->lParam->time, ev->wParam }); | ||
_cv.notify_all(); | ||
} | ||
|
||
KeyTimedEvent KeyDelay::NextEvent() | ||
{ | ||
auto ev = _queue.front(); | ||
_queue.pop(); | ||
return ev; | ||
} | ||
|
||
bool KeyDelay::CheckIfMillisHaveElapsed(DWORD first, DWORD last, DWORD duration) | ||
{ | ||
if (first < last && first <= first + duration) | ||
{ | ||
return first + duration < last; | ||
} | ||
else | ||
{ | ||
first += ULONG_MAX / 2; | ||
last += ULONG_MAX / 2; | ||
return first + duration < last; | ||
} | ||
} | ||
|
||
bool KeyDelay::HasNextEvent() | ||
{ | ||
return !_queue.empty(); | ||
} | ||
|
||
bool KeyDelay::HandleRelease() | ||
{ | ||
while (HasNextEvent()) | ||
{ | ||
auto ev = NextEvent(); | ||
switch (ev.message) | ||
{ | ||
case WM_KEYDOWN: | ||
case WM_SYSKEYDOWN: | ||
_state = KeyDelayState::ON_HOLD; | ||
_initialHoldKeyDown = ev.time; | ||
return false; | ||
case WM_KEYUP: | ||
case WM_SYSKEYUP: | ||
break; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
bool KeyDelay::HandleOnHold(std::unique_lock<std::mutex>& cvLock) | ||
{ | ||
while (HasNextEvent()) | ||
{ | ||
auto ev = NextEvent(); | ||
switch (ev.message) | ||
{ | ||
case WM_KEYDOWN: | ||
case WM_SYSKEYDOWN: | ||
break; | ||
case WM_KEYUP: | ||
case WM_SYSKEYUP: | ||
if (CheckIfMillisHaveElapsed(_initialHoldKeyDown, ev.time, LONG_PRESS_DELAY_MILLIS)) | ||
{ | ||
_onLongPress(_key); | ||
} | ||
else | ||
{ | ||
_onShortPress(_key); | ||
} | ||
_state = KeyDelayState::RELEASED; | ||
return false; | ||
} | ||
} | ||
|
||
if (CheckIfMillisHaveElapsed(_initialHoldKeyDown, GetTickCount(), LONG_PRESS_DELAY_MILLIS)) | ||
{ | ||
_onLongPress(_key); | ||
_state = KeyDelayState::ON_HOLD_TIMEOUT; | ||
} | ||
else | ||
{ | ||
_cv.wait_for(cvLock, std::chrono::milliseconds(ON_HOLD_WAIT_TIMEOUT_MILLIS)); | ||
} | ||
return false; | ||
} | ||
|
||
bool KeyDelay::HandleOnHoldTimeout() | ||
{ | ||
while (HasNextEvent()) | ||
{ | ||
auto ev = NextEvent(); | ||
switch (ev.message) | ||
{ | ||
case WM_KEYDOWN: | ||
case WM_SYSKEYDOWN: | ||
case WM_KEYUP: | ||
case WM_SYSKEYUP: | ||
_state = KeyDelayState::RELEASED; | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
void KeyDelay::DelayThread() | ||
{ | ||
std::unique_lock<std::mutex> qLock(_queueMutex); | ||
bool shouldWait = true; | ||
|
||
while (!_quit) | ||
{ | ||
if (shouldWait) | ||
{ | ||
_cv.wait(qLock); | ||
} | ||
|
||
switch (_state) | ||
{ | ||
case KeyDelayState::RELEASED: | ||
shouldWait = HandleRelease(); | ||
break; | ||
case KeyDelayState::ON_HOLD: | ||
shouldWait = HandleOnHold(qLock); | ||
break; | ||
case KeyDelayState::ON_HOLD_TIMEOUT: | ||
shouldWait = HandleOnHoldTimeout(); | ||
break; | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#pragma once | ||
#include <interface/lowlevel_keyboard_event_data.h> | ||
#include <functional> | ||
#include <thread> | ||
#include <queue> | ||
#include <mutex> | ||
#include <chrono> | ||
|
||
enum class KeyDelayState | ||
{ | ||
RELEASED, | ||
ON_HOLD, | ||
ON_HOLD_TIMEOUT, | ||
}; | ||
|
||
struct KeyTimedEvent | ||
{ | ||
DWORD time; | ||
WPARAM message; | ||
}; | ||
|
||
class KeyDelay | ||
{ | ||
public: | ||
KeyDelay( | ||
DWORD key, | ||
std::function<void(DWORD)> onShortPress, | ||
std::function<void(DWORD)> onLongPress | ||
) : | ||
_quit(false), | ||
_state(KeyDelayState::RELEASED), | ||
_initialHoldKeyDown(0), | ||
_key(key), | ||
_onLongPress(onLongPress), | ||
_onShortPress(onShortPress), | ||
_delayThread(&KeyDelay::DelayThread, this) | ||
{}; | ||
|
||
void KeyEvent(LowlevelKeyboardEvent* ev); | ||
~KeyDelay(); | ||
|
||
private: | ||
void DelayThread(); | ||
bool HandleRelease(); | ||
bool HandleOnHold(std::unique_lock<std::mutex>& cvLock); | ||
bool HandleOnHoldTimeout(); | ||
KeyTimedEvent NextEvent(); | ||
bool HasNextEvent(); | ||
bool CheckIfMillisHaveElapsed(DWORD first, DWORD last, DWORD duration); | ||
|
||
std::thread _delayThread; | ||
bool _quit; | ||
KeyDelayState _state; | ||
std::function<void(DWORD)> _onLongPress; | ||
std::function<void(DWORD)> _onShortPress; | ||
std::queue<KeyTimedEvent> _queue; | ||
std::mutex _queueMutex; | ||
std::condition_variable _cv; | ||
DWORD _initialHoldKeyDown; | ||
DWORD _key; | ||
}; | ||
|
||
|
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.