-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyboard.cpp
53 lines (44 loc) · 1.25 KB
/
keyboard.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
THIS FILE IS A PART OF RDR 2 SCRIPT HOOK SDK
http://dev-c.com
(C) Alexander Blade 2019
*/
#include "keyboard.h"
const int KEYS_SIZE = 255;
struct {
DWORD time;
BOOL isWithAlt;
BOOL wasDownBefore;
BOOL isUpNow;
} keyStates[KEYS_SIZE];
void OnKeyboardMessage(DWORD key, WORD repeats, BYTE scanCode, BOOL isExtended, BOOL isWithAlt, BOOL wasDownBefore, BOOL isUpNow)
{
if (key < KEYS_SIZE)
{
keyStates[key].time = GetTickCount();
keyStates[key].isWithAlt = isWithAlt;
keyStates[key].wasDownBefore = wasDownBefore;
keyStates[key].isUpNow = isUpNow;
}
}
const int NOW_PERIOD = 100, MAX_DOWN = 5000, MAX_DOWN_LONG = 30000; // ms
bool IsKeyDown(DWORD key)
{
return (key < KEYS_SIZE) ? ((GetTickCount() < keyStates[key].time + MAX_DOWN) && !keyStates[key].isUpNow) : false;
}
bool IsKeyDownLong(DWORD key)
{
return (key < KEYS_SIZE) ? ((GetTickCount() < keyStates[key].time + MAX_DOWN_LONG) && !keyStates[key].isUpNow) : false;
}
bool IsKeyJustUp(DWORD key, bool exclusive)
{
bool b = (key < KEYS_SIZE) ? (GetTickCount() < keyStates[key].time + NOW_PERIOD && keyStates[key].isUpNow) : false;
if (b && exclusive)
ResetKeyState(key);
return b;
}
void ResetKeyState(DWORD key)
{
if (key < KEYS_SIZE)
memset(&keyStates[key], 0, sizeof(keyStates[0]));
}