Implement alt to toggle to previous window(s)

This commit is contained in:
2025-08-26 20:42:50 +02:00
parent 48d3af7c94
commit 5e95937ae9
3 changed files with 90 additions and 24 deletions

View File

@@ -13,10 +13,10 @@ namespace DD2Switcher {
private static int ActiveIndex = -1;
private static int PreviousIndex = -1;
private static bool AltPressed = false;
private static Timer AltTimer;
private static readonly IntPtr defaultAffinity = new(0xFF000000);
private static readonly IntPtr fullAffinity = new(0xFFFFFFFF);
private static readonly KeyboardHook keyboardHook = new();
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
@@ -151,6 +151,7 @@ namespace DD2Switcher {
Console.WriteLine($"Added {process.ProcessName} to tracked windows at index {firstNullIndex}");
}
// TODO: Restore this
// private static void TrackWows() {
// CleanWindows();
// var processes = Process.GetProcesses().OrderBy(p => p.Id).ToList();
@@ -257,14 +258,6 @@ namespace DD2Switcher {
}
}
private static void OnAltTimer() {
if (AltPressed) {
AltPressed = false;
AltTimer.Stop();
TabToPrevious();
}
}
private static bool IsCapsLockOn() {
return (GetKeyState(0x14) & 1) == 1;
}
@@ -282,10 +275,28 @@ namespace DD2Switcher {
Process.GetCurrentProcess().Kill();
}
AltTimer = new Timer(OnAltTimer, null, Timeout.Infinite, Timeout.Infinite);
bool onlyAlt = false;
KeyboardHook.Start();
KeyboardHook.KeyDown += (sender, e) => {
Console.WriteLine($"Key down: {e}");
if (e == 164 && !onlyAlt) {
Console.WriteLine("Only alt");
onlyAlt = true;
} else {
Console.WriteLine("Not only alt");
onlyAlt = false;
}
};
KeyboardHook.KeyUp += (sender, e) => {
Console.WriteLine($"Key up: {e}");
if (e == 164 && onlyAlt) { // Left alt
Console.WriteLine("Tab to previous");
onlyAlt = false;
TabToPrevious();
}
};
HotKeyManager.RegisterHotKey(Keys.Capital, KeyModifiers.NoRepeat);
HotKeyManager.RegisterHotKey(Keys.Menu, KeyModifiers.NoRepeat);
// Register main number keys (0-9)
for (int i = 0; i < 10; i++) HotKeyManager.RegisterHotKey(Keys.D0 + i, KeyModifiers.Alt);
@@ -301,17 +312,6 @@ namespace DD2Switcher {
// return;
// }
if (e.Key == Keys.Menu && e.Modifiers == KeyModifiers.NoRepeat) {
AltPressed = true;
AltTimer.Change(150, Timeout.Infinite);
return;
}
if (e.Modifiers == KeyModifiers.Alt) {
AltPressed = false;
AltTimer.Stop();
}
int index;
if (e.Key >= Keys.D0 && e.Key <= Keys.D9) {
index = e.Key - Keys.D0;
@@ -330,8 +330,9 @@ namespace DD2Switcher {
}
}
Console.CancelKeyPress += (sender, e) => { Process.GetCurrentProcess().Kill(); };
while (true) System.Threading.Thread.Sleep(100000);
// Console.CancelKeyPress += (sender, e) => { Process.GetCurrentProcess().Kill(); };
Application.Run();
KeyboardHook.Stop();
}
}
}