Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions AltBacktick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -25,17 +25,29 @@ 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;

while (GetMessage(&msg, nullptr, 0, 0)) {
if (msg.message == WM_HOTKEY) {
std::vector<HWND> 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) {
Expand All @@ -45,7 +57,6 @@ int StartBackgroundApp() {
ShowWindow(windowToFocus, SW_RESTORE);
}
SetForegroundWindow(windowToFocus);
lastWindow = windowToFocus;
}
}
}
Expand Down