Skip to content

Commit da4ae8d

Browse files
committed
Custom keybinds added, God that was difficult
1 parent cd466a3 commit da4ae8d

File tree

4 files changed

+230
-84
lines changed

4 files changed

+230
-84
lines changed

src/win/WinApp.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include "core/Platform.h"
55
#include "WinSettings.h"
66

7-
87
static const char * WINDOW_CLASS = "SplitLoafTray";
98
static HINSTANCE g_hInstance = NULL; // A handle to the current instance of the application.
109

@@ -38,29 +37,31 @@ LRESULT CALLBACK WindowProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
3837
return DefWindowProc(hwnd, msg, wParam, lParam);
3938
}
4039

41-
int RunWindowsApp ( ) { // Create the application.
40+
int RunWindowsApp() {
4241
g_hInstance = GetModuleHandle(NULL);
4342

43+
// Initialize keybinds & settings
44+
WinSettings_Init();
45+
4446
WNDCLASS wc = {};
4547
wc.lpfnWndProc = WindowProc;
4648
wc.hInstance = g_hInstance;
4749
wc.lpszClassName = WINDOW_CLASS;
48-
RegisterClass(& wc);
49-
50-
HWND hwnd = CreateWindowEx(0, WINDOW_CLASS, "Split Loaf",0, 0, 0, 0, 0,NULL, NULL, g_hInstance, NULL);
50+
RegisterClass(&wc);
5151

52-
InitTrayIcon(hwnd); // Create the System Tray icon.
52+
HWND hwnd = CreateWindowEx(0, WINDOW_CLASS, "Split Loaf", 0, 0, 0, 0, 0, NULL, NULL, g_hInstance, NULL);
5353

54-
Platform::init(); // Start the main program.
54+
InitTrayIcon(hwnd); // System Tray icon
55+
Platform::init(); // Start main program
5556

5657
MSG msg{};
5758
while (true) {
58-
DWORD result = MsgWaitForMultipleObjects(0,NULL,FALSE,1,QS_ALLINPUT);
59+
DWORD result = MsgWaitForMultipleObjects(0, NULL, FALSE, 1, QS_ALLINPUT);
5960

6061
if (result == WAIT_OBJECT_0) {
61-
while (PeekMessage(& msg, NULL, 0, 0, PM_REMOVE)) {
62-
TranslateMessage(& msg);
63-
DispatchMessage(& msg);
62+
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
63+
TranslateMessage(&msg);
64+
DispatchMessage(&msg);
6465

6566
if (msg.message == WM_QUIT) {
6667
Platform::shutdown();

src/win/WinHooks.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
#include <string>
77
#include <algorithm>
88

9+
#include "WinSettings.h"
10+
11+
const Keybind targetBind = WinSettings_GetTargetBind();
12+
const Keybind lockBind = WinSettings_GetLockBind();
13+
const Keybind unlockBind = WinSettings_GetUnlockBind();
14+
915
std::string ToLower (const std::string & s) { // Set the string to lowercase.
1016
std::string out = s;
1117
std::transform(out.begin(), out.end(), out.begin(), ::tolower);
@@ -36,6 +42,15 @@ void Tray_SetLocked (const std::string & windowName) {
3642
UpdateTrayTooltip("Split Loaf - " + ToUpper(windowName)); // When the keyboard is locked, the name of the window is shown in capitals.
3743
}
3844

45+
bool IsKeybindPressed(const Keybind & bind, KBDLLHOOKSTRUCT * kbd) {
46+
bool ctrlDown = (GetAsyncKeyState(VK_CONTROL) & 0x8000) != 0;
47+
bool shiftDown = (GetAsyncKeyState(VK_SHIFT) & 0x8000) != 0;
48+
bool altDown = (GetAsyncKeyState(VK_MENU) & 0x8000) != 0; // ALT
49+
50+
return (kbd->vkCode == bind.key) && (ctrlDown == bind.ctrl) && (shiftDown == bind.shift) && (altDown == bind.alt);
51+
}
52+
53+
3954
LRESULT CALLBACK LowLevelKeyboardProc (int nCode, WPARAM wParam, LPARAM lParam) {
4055
if (nCode != HC_ACTION) {
4156
// If no action go to next.
@@ -54,7 +69,7 @@ LRESULT CALLBACK LowLevelKeyboardProc (int nCode, WPARAM wParam, LPARAM lParam)
5469
}
5570

5671
if (wParam == WM_KEYDOWN) { // For key press downs.
57-
if (kbd -> vkCode == VK_F8) {
72+
if (IsKeybindPressed(WinSettings_GetTargetBind(), kbd)) {
5873
// If F8 is pressed set the target window to the current one.
5974
POINT p;
6075
GetCursorPos(& p);
@@ -70,7 +85,7 @@ LRESULT CALLBACK LowLevelKeyboardProc (int nCode, WPARAM wParam, LPARAM lParam)
7085
return 1;
7186
}
7287

73-
if (kbd -> vkCode == VK_F6) {
88+
if (IsKeybindPressed(WinSettings_GetLockBind(), kbd)) {
7489
// If F6 is pressed, lock the keyboard to the target window.
7590
locked = (targetWindow != NULL);
7691
if (locked) {
@@ -86,7 +101,7 @@ LRESULT CALLBACK LowLevelKeyboardProc (int nCode, WPARAM wParam, LPARAM lParam)
86101
return 1;
87102
}
88103

89-
if (kbd -> vkCode == VK_F7) {
104+
if (IsKeybindPressed(WinSettings_GetUnlockBind(), kbd)) {
90105
// If F7 is pressed unlock the keyboard.
91106
locked = false;
92107
targetHasFocus = false;

0 commit comments

Comments
 (0)