From cd701deb9c5f9c19f46c3634e9b3a9dc648fdd51 Mon Sep 17 00:00:00 2001 From: "Charles V. Chandler" Date: Wed, 28 Jun 2023 15:16:05 -0500 Subject: [PATCH 1/2] register two hotkeys --- AltBacktick.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/AltBacktick.cpp b/AltBacktick.cpp index 8ad76ac..6172980 100644 --- a/AltBacktick.cpp +++ b/AltBacktick.cpp @@ -16,7 +16,7 @@ int StartBackgroundApp() { MSG msg; UINT keyCode = MapVirtualKey(BACKTICK_SCAN_CODE, MAPVK_VSC_TO_VK); - if (!RegisterHotKey(NULL, NULL, MOD_ALT, keyCode)) { + if (!RegisterHotKey(NULL, 1, MOD_ALT, keyCode)) { DWORD lastError = GetLastError(); if (lastError == ERROR_HOTKEY_ALREADY_REGISTERED) { MessageBox( @@ -25,6 +25,15 @@ int StartBackgroundApp() { return 0; } } + if (!RegisterHotKey(NULL, 2, MOD_ALT | MOD_SHIFT, keyCode)){ + DWORD lastError = GetLastError(); + if (lastError == ERROR_HOTKEY_ALREADY_REGISTERED) { + MessageBox( + NULL, L"Failed to register the ALT+SHIFT+~ hotkey.\nMake sure no other application is already binding to it.", + L"Failed to register hotkey", MB_ICONEXCLAMATION); + return 0; + } + } WindowFinder windowFinder; HWND lastWindow = nullptr; From 3d19c906ef7998b5a7f00fbd9598027b46dd3ac9 Mon Sep 17 00:00:00 2001 From: "Charles V. Chandler" Date: Thu, 29 Jun 2023 12:25:42 -0500 Subject: [PATCH 2/2] shift+alt+backtick navigates to previous window Testing revealed that "windows" is a list of HWNDs in Z order. This list excluded the current active window. The loop that was here previously was simply selecting the last item in the list. This takes advantage of vector methods to skip all the middlemen. Additionally, we're now using the second hotkey to go to the previously selected window. --- AltBacktick.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/AltBacktick.cpp b/AltBacktick.cpp index 6172980..c106cf1 100644 --- a/AltBacktick.cpp +++ b/AltBacktick.cpp @@ -36,15 +36,18 @@ int StartBackgroundApp() { } WindowFinder windowFinder; - HWND lastWindow = nullptr; + while (GetMessage(&msg, nullptr, 0, 0)) { if (msg.message == WM_HOTKEY) { std::vector windows = windowFinder.FindCurrentProcessWindows(); HWND windowToFocus = nullptr; - for (const HWND &window : windows) { - if (window != lastWindow || windows.size() == 1) { - windowToFocus = window; - } + if (windows.size() < 1) { + continue; + } + if (msg.lParam & MOD_SHIFT){ + windowToFocus = windows[0]; + } else { + windowToFocus = *--windows.end(); } if (windowToFocus != nullptr) { @@ -54,7 +57,6 @@ int StartBackgroundApp() { ShowWindow(windowToFocus, SW_RESTORE); } SetForegroundWindow(windowToFocus); - lastWindow = windowToFocus; } } }