Implement switching between previous programs via just alt

This commit is contained in:
2025-08-26 18:10:23 +02:00
parent 603bc488e8
commit 48d3af7c94

View File

@@ -4,12 +4,16 @@ using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Windows.Forms; using System.Windows.Forms;
using System.Threading;
namespace DD2Switcher { namespace DD2Switcher {
internal static class Program { internal static class Program {
private static int NumProc = 19; private static int NumProc = 19;
private static Process[] windows = new Process[NumProc]; private static Process[] windows = new Process[NumProc];
private static int ActiveIndex = -1; 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 defaultAffinity = new(0xFF000000);
private static readonly IntPtr fullAffinity = new(0xFFFFFFFF); private static readonly IntPtr fullAffinity = new(0xFFFFFFFF);
@@ -221,6 +225,7 @@ namespace DD2Switcher {
Console.WriteLine($"Window at index {index} does not exist, removing from tracked windows"); Console.WriteLine($"Window at index {index} does not exist, removing from tracked windows");
windows[index] = null; windows[index] = null;
} else { } else {
PreviousIndex = ActiveIndex;
SetForegroundWindow(window.MainWindowHandle); SetForegroundWindow(window.MainWindowHandle);
ActiveIndex = index; ActiveIndex = index;
AdjustAffinities(); AdjustAffinities();
@@ -228,6 +233,38 @@ namespace DD2Switcher {
} }
} }
private static void TabToPrevious() {
if (PreviousIndex == -1) {
Console.WriteLine("No previous window to switch to");
return;
}
CleanWindows();
Console.WriteLine($"Tab to previous window at index {PreviousIndex}");
var window = windows[PreviousIndex];
if (window == null || window.MainWindowHandle == IntPtr.Zero) {
Console.WriteLine(
$"Previous window at index {PreviousIndex} does not exist, removing from tracked windows");
windows[PreviousIndex] = null;
PreviousIndex = -1;
} else {
var tempIndex = ActiveIndex;
SetForegroundWindow(window.MainWindowHandle);
ActiveIndex = PreviousIndex;
PreviousIndex = tempIndex;
AdjustAffinities();
AdjustPriorities();
}
}
private static void OnAltTimer() {
if (AltPressed) {
AltPressed = false;
AltTimer.Stop();
TabToPrevious();
}
}
private static bool IsCapsLockOn() { private static bool IsCapsLockOn() {
return (GetKeyState(0x14) & 1) == 1; return (GetKeyState(0x14) & 1) == 1;
} }
@@ -245,7 +282,10 @@ namespace DD2Switcher {
Process.GetCurrentProcess().Kill(); Process.GetCurrentProcess().Kill();
} }
AltTimer = new Timer(OnAltTimer, null, Timeout.Infinite, Timeout.Infinite);
HotKeyManager.RegisterHotKey(Keys.Capital, KeyModifiers.NoRepeat); HotKeyManager.RegisterHotKey(Keys.Capital, KeyModifiers.NoRepeat);
HotKeyManager.RegisterHotKey(Keys.Menu, KeyModifiers.NoRepeat);
// Register main number keys (0-9) // Register main number keys (0-9)
for (int i = 0; i < 10; i++) HotKeyManager.RegisterHotKey(Keys.D0 + i, KeyModifiers.Alt); for (int i = 0; i < 10; i++) HotKeyManager.RegisterHotKey(Keys.D0 + i, KeyModifiers.Alt);
@@ -261,6 +301,17 @@ namespace DD2Switcher {
// return; // 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; int index;
if (e.Key >= Keys.D0 && e.Key <= Keys.D9) { if (e.Key >= Keys.D0 && e.Key <= Keys.D9) {
index = e.Key - Keys.D0; index = e.Key - Keys.D0;