Skip to content

Commit

Permalink
Now shows a warning when pressed keys which don't work
Browse files Browse the repository at this point in the history
  • Loading branch information
Broennimann Florian committed Jul 11, 2023
1 parent 6c292f6 commit f68567f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
1 change: 1 addition & 0 deletions Code/KbdHook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {

// Eat any alphanumeric key
if (config.mediaKeysWithCapsLockSpaceArrow && nKey >= 'A' && nKey <= 'Z' && !anyModifierPressed() && capsLockDownLazy()) {
StatusWindow::ShowBlocked();
return 1;
}

Expand Down
34 changes: 28 additions & 6 deletions Code/StatusWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,21 @@
static bool alreadyRegistered = false;
static HWND g_hWnd, g_hStaticText;
static char text[100];
static int windowWidth, windowHeight;

static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message) {
case WM_CREATE:
g_hStaticText = CreateWindow("static", text, WS_CHILD | WS_VISIBLE | SS_CENTER | SS_CENTERIMAGE, 0, 0, 200, 50, hWnd, NULL, GetModuleHandle(NULL), 0);
g_hStaticText = CreateWindow("static", text, WS_CHILD | WS_VISIBLE | SS_CENTER | SS_CENTERIMAGE, 0, 0, windowWidth, windowHeight, hWnd, NULL, GetModuleHandle(NULL), 0);
break;

//case WM_WINDOWPOSCHANGED: {
// WINDOWPOS *pos = (WINDOWPOS*)lParam;
// printf("SetWindowPos %d %d\n", pos->cx, pos->cy);
// SetWindowPos(hWnd, g_hStaticText, 0, 0, windowWidth, windowHeight, SWP_NOMOVE | SWP_NOZORDER);
// break;
//}

case WM_TIMER:
DestroyWindow(hWnd);
break;
Expand Down Expand Up @@ -52,19 +60,23 @@ static ATOM MyRegisterClass(HINSTANCE hInstance)

// Needs the text variable to be set to the message text to show
// If the text is an empty string, the window is hidden if active / nothing is shown
static void showMessageInternal(int delayMs) {
static void showMessageInternal(int delayMs, int width = 200, int height = 50) {
MONITORINFOEX monitorInfo;
HMONITOR hMonitor = MonitorFromWindow(GetForegroundWindow(), MONITOR_DEFAULTTOPRIMARY);
monitorInfo.cbSize = sizeof(monitorInfo);
GetMonitorInfo(hMonitor, &monitorInfo);
const int width = 200, height = 50;
bool isEmptyMessage = !text[0];

if (g_hWnd && (windowWidth != width || windowHeight != height)) {
KillTimer(g_hWnd, 0);
DestroyWindow(g_hWnd);
}

windowWidth = width, windowHeight = height;
if (g_hWnd) {
KillTimer(g_hWnd, 0);
SetTimer(g_hWnd, 0, delayMs, NULL);
}
else if (!isEmptyMessage) {
} else if (!isEmptyMessage) {
if (!alreadyRegistered)
MyRegisterClass(GetModuleHandle(NULL));
alreadyRegistered = true;
Expand All @@ -79,7 +91,8 @@ static void showMessageInternal(int delayMs) {
SetWindowPos(g_hWnd, HWND_TOPMOST,
(monitorInfo.rcMonitor.right + monitorInfo.rcMonitor.left - width) / 2,
(monitorInfo.rcMonitor.bottom + monitorInfo.rcMonitor.top - height) / 2,
width, height, SWP_NOSIZE | SWP_NOZORDER);
width, height, SWP_NOZORDER);
SetWindowPos(g_hWnd, g_hStaticText, 0, 0, windowWidth, windowHeight, SWP_NOMOVE | SWP_NOZORDER);
}
}

Expand All @@ -97,6 +110,15 @@ void StatusWindow::showVolume(double volumeLevel) {
showMessageInternal(2000);
}

void StatusWindow::ShowBlocked() {
MONITORINFOEX monitorInfo;
HMONITOR hMonitor = MonitorFromWindow(GetForegroundWindow(), MONITOR_DEFAULTTOPRIMARY);
monitorInfo.cbSize = sizeof(monitorInfo);
GetMonitorInfo(hMonitor, &monitorInfo);
strcpy_s(text, "CapsLock set; ignoring keystroke");
showMessageInternal(2000, monitorInfo.rcMonitor.right - monitorInfo.rcMonitor.left, monitorInfo.rcMonitor.bottom - monitorInfo.rcMonitor.top);
}

void StatusWindow::showMessage(const char *message, int timeToLeaveOpen) {
strcpy_s(text, message);
showMessageInternal(timeToLeaveOpen);
Expand Down
1 change: 1 addition & 0 deletions Code/StatusWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace StatusWindow {
extern void showBrightness(int brightnessLevel);
extern void showVolume(double volumeLevel);
extern void ShowBlocked();

extern void showMessage(const char *message, int timeToLeaveOpen);
extern void hideMessage();
Expand Down

0 comments on commit f68567f

Please sign in to comment.