-
Notifications
You must be signed in to change notification settings - Fork 5
/
KeyBindHandler.cpp
154 lines (132 loc) · 3.95 KB
/
KeyBindHandler.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "KeyBindHandler.h"
#include "arcdps_structs.h"
#include <format>
#include <ranges>
uint64_t KeyBindHandler::Subscribe(Subscriber pSubscriber) {
uint64_t newId = getNewId();
mSubscribers.try_emplace(newId, pSubscriber);
return newId;
}
void KeyBindHandler::Unsubscribe(uint64_t pId) {
mSubscribers.erase(pId);
}
void KeyBindHandler::UpdateKey(uint64_t pId, const KeyBinds::Key& pKey) {
mSubscribers.at(pId).Key = pKey;
}
void KeyBindHandler::UpdateKeys(const KeyBinds::Key& pOldKey, const KeyBinds::Key& pNewKey) {
for (auto& subscriber : mSubscribers | std::views::values) {
if(subscriber.Key == pOldKey) {
subscriber.Key = pNewKey;
}
}
}
bool KeyBindHandler::Wnd(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_KEYDOWN:
case WM_SYSKEYDOWN: {
UINT scanCode = (lParam >> 16) & 0x00ff;
const bool extended = (lParam >> 24) & 0x1;
const bool previous = (lParam >> 30) & 0x1;
if (previous == true) break;
if (extended == true) {
scanCode |= 0xE000;
}
const auto& keyCodeOpt = KeyBinds::MsvcScanCodeToKeyCode(scanCode);
if (!keyCodeOpt) {
ARC_LOG(std::format("unknown key: {}", scanCode).c_str());
break;
}
const KeyBinds::KeyCode& keyCode = keyCodeOpt.value();
KeyBinds::Modifier modifier = KeyBinds::GetModifier(keyCode);
if (modifier != 0) {
mTrackedModifier |= modifier;
} else {
KeyBinds::Modifier arcdpsModifier = GetArcdpsModifier();
// Check if any registered KeyBind is correct
for (const auto& subscriber : mSubscribers | std::views::values) {
const auto& key = subscriber.Key;
if ((subscriber.Flags & SubscriberFlags_ArcdpsModifier ? arcdpsModifier == mTrackedModifier : key.Modifier == mTrackedModifier) &&
key.DeviceType == KeyBinds::DeviceType::Keyboard && static_cast<KeyBinds::KeyCode>(key.Code) == keyCode) {
bool res = subscriber.Fun(key);
if (res) {
return res;
}
}
}
}
break;
}
case WM_KEYUP:
case WM_SYSKEYUP: {
UINT scanCode = (lParam >> 16) & 0x00ff;
const bool extended = (lParam >> 24) & 0x1;
if (extended == true) {
scanCode |= 0xE000;
}
const auto& keyCode = KeyBinds::MsvcScanCodeToKeyCode(scanCode);
if (!keyCode) break;
KeyBinds::Modifier modifier = KeyBinds::GetModifier(keyCode.value());
if (modifier != 0) {
mTrackedModifier &= !modifier;
}
break;
}
case WM_XBUTTONDOWN: {
int32_t code = 0;
WORD word = GET_XBUTTON_WPARAM(wParam);
if (word == XBUTTON1) {
code = static_cast<int32_t>(KeyBinds::MouseCode::Mouse_4);
} else if (word == XBUTTON2) {
code = static_cast<int32_t>(KeyBinds::MouseCode::Mouse_5);
}
if (code != 0) {
for (const auto& subscriber : mSubscribers) {
const auto& key = subscriber.second.Key;
if (key.Modifier == mTrackedModifier && key.DeviceType == KeyBinds::DeviceType::Mouse && key.Code ==
code) {
return subscriber.second.Fun(key);
}
}
}
break;
}
case WM_MBUTTONDOWN: {
for (const auto& subscriber : mSubscribers) {
const auto& key = subscriber.second.Key;
if (key.Modifier == mTrackedModifier && key.DeviceType == KeyBinds::DeviceType::Mouse && key.Code ==
static_cast<int32_t>(KeyBinds::MouseCode::Mouse_3)) {
return subscriber.second.Fun(key);
}
}
break;
}
case WM_ACTIVATEAPP: {
// reset modifier when loosing focus (Alt+Tab)
mTrackedModifier = 0;
break;
}
}
return false;
}
KeyBinds::Modifier KeyBindHandler::GetArcdpsModifier() {
uint64_t e7_result = ARC_EXPORT_E7();
uint16_t* ra = (uint16_t*)&e7_result;
if (ra) {
uint16_t mod1 = ra[0];
uint16_t mod2 = ra[1];
return getArcdpsModifierSingle(mod1) | getArcdpsModifierSingle(mod2);
}
return 0;
}
KeyBinds::Modifier KeyBindHandler::getArcdpsModifierSingle(uint16_t pMod) {
if (pMod == 16) {
return KeyBinds::Modifier_Shift;
}
if (pMod == 18) {
return KeyBinds::Modifier_Alt;
}
if (pMod == 17) {
return KeyBinds::Modifier_Ctrl;
}
return 0;
}