Fix the just now implemented functionality

This commit is contained in:
2025-08-26 21:00:02 +02:00
parent 5e95937ae9
commit 458a466c15

View File

@@ -11,9 +11,11 @@ namespace DD2Switcher {
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 bool AltPressed = false;
// Simple list to track last 5 windows
private static List<int> lastWindows = new List<int>();
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);
private static readonly KeyboardHook keyboardHook = new(); private static readonly KeyboardHook keyboardHook = new();
@@ -148,22 +150,32 @@ namespace DD2Switcher {
// Add the new process at the first null slot // Add the new process at the first null slot
windows[firstNullIndex] = process; windows[firstNullIndex] = process;
PushHistory(firstNullIndex);
ActiveIndex = firstNullIndex;
Console.WriteLine($"Added {process.ProcessName} to tracked windows at index {firstNullIndex}"); Console.WriteLine($"Added {process.ProcessName} to tracked windows at index {firstNullIndex}");
} }
// TODO: Restore this private static void TrackProcess() {
// private static void TrackWows() { CleanWindows();
// CleanWindows(); var foregroundWindow = GetForegroundWindow();
// var processes = Process.GetProcesses().OrderBy(p => p.Id).ToList(); var foregroundProcess = Process.GetProcesses().FirstOrDefault(p => p.MainWindowHandle == foregroundWindow);
// foreach (var process in processes) { if (foregroundProcess == null) {
// if (process.ProcessName == "UWow-64") { Console.WriteLine("No foreground process found");
// Console.WriteLine($"Found UWow-64 at pid {process.Id}"); return;
// if (ProcessTracked(process.Id)) }
// continue; Console.WriteLine($"Foreground process: {foregroundProcess.ProcessName} PID: {foregroundProcess.Id}");
// Track(process);
// } var processes = Process.GetProcesses().OrderBy(p => p.Id).ToList();
// } foreach (var process in processes) {
// } // Console.WriteLine($"Checking {process.ProcessName} at pid {process.Id}");
if (process.ProcessName == foregroundProcess.ProcessName) {
Console.WriteLine($"Found {foregroundProcess.ProcessName} at pid {process.Id}");
if (ProcessTracked(process.Id))
continue;
Track(process);
}
}
}
private static void Swap(int index) { private static void Swap(int index) {
index = (index - 1) % NumProc; index = (index - 1) % NumProc;
@@ -204,9 +216,6 @@ namespace DD2Switcher {
windows[i] = windows[index]; windows[i] = windows[index];
windows[index] = window; windows[index] = window;
Console.WriteLine($"Swapped window at index {i} to {index}"); Console.WriteLine($"Swapped window at index {i} to {index}");
// Toggle caps lock off
keybd_event(0x14, 0, 0, 0); // KEYEVENTF_KEYDOWN
keybd_event(0x14, 0, 0x0002, 0); // KEYEVENTF_KEYUP
return; return;
} }
} }
@@ -226,7 +235,9 @@ 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; if (ActiveIndex != -1)
PushHistory(ActiveIndex);
SetForegroundWindow(window.MainWindowHandle); SetForegroundWindow(window.MainWindowHandle);
ActiveIndex = index; ActiveIndex = index;
AdjustAffinities(); AdjustAffinities();
@@ -235,26 +246,24 @@ namespace DD2Switcher {
} }
private static void TabToPrevious() { private static void TabToPrevious() {
if (PreviousIndex == -1) { var foreground = GetForegroundProcess();
if (!ProcessTracked(foreground.Id)) {
Console.WriteLine("Foreground process not tracked, skipping");
return;
}
if (lastWindows.Count == 0) {
Console.WriteLine("No previous window to switch to"); Console.WriteLine("No previous window to switch to");
return; return;
} }
CleanWindows(); CleanWindows();
Console.WriteLine($"Tab to previous window at index {PreviousIndex}");
var window = windows[PreviousIndex]; for (int i = lastWindows.Count - 1; i >= 0; i--) {
if (window == null || window.MainWindowHandle == IntPtr.Zero) { int index = lastWindows[i];
Console.WriteLine( if (index != ActiveIndex) {
$"Previous window at index {PreviousIndex} does not exist, removing from tracked windows"); TabTo(index+1); // Our windows are 1-indexed because... I don't remember why
windows[PreviousIndex] = null; break;
PreviousIndex = -1; }
} else {
var tempIndex = ActiveIndex;
SetForegroundWindow(window.MainWindowHandle);
ActiveIndex = PreviousIndex;
PreviousIndex = tempIndex;
AdjustAffinities();
AdjustPriorities();
} }
} }
@@ -262,6 +271,17 @@ namespace DD2Switcher {
return (GetKeyState(0x14) & 1) == 1; return (GetKeyState(0x14) & 1) == 1;
} }
private static void ToggleCapsLock() {
keybd_event(0x14, 0, 0, 0); // KEYEVENTF_KEYDOWN
keybd_event(0x14, 0, 0x0002, 0); // KEYEVENTF_KEYUP
}
private static void PushHistory(int index) {
lastWindows.Add(index);
if (lastWindows.Count > 50)
lastWindows.RemoveAt(0);
}
[STAThread] [STAThread]
private static void Main() { private static void Main() {
// AllocConsole(); // AllocConsole();
@@ -307,10 +327,11 @@ namespace DD2Switcher {
HotKeyManager.HotKeyPressed += HotKeyManager_HotKeyPressed; HotKeyManager.HotKeyPressed += HotKeyManager_HotKeyPressed;
void HotKeyManager_HotKeyPressed(object sender, HotKeyEventArgs e) { void HotKeyManager_HotKeyPressed(object sender, HotKeyEventArgs e) {
// if (e.Key == Keys.Oemtilde && e.Modifiers == KeyModifiers.Alt) { if (e.Key == Keys.Oemtilde && e.Modifiers == KeyModifiers.Alt && IsCapsLockOn()) {
// TrackWows(); TrackProcess();
// return; ToggleCapsLock();
// } return;
}
int index; int index;
if (e.Key >= Keys.D0 && e.Key <= Keys.D9) { if (e.Key >= Keys.D0 && e.Key <= Keys.D9) {
@@ -324,6 +345,7 @@ namespace DD2Switcher {
if (e.Modifiers == KeyModifiers.Alt) { if (e.Modifiers == KeyModifiers.Alt) {
if (IsCapsLockOn()) { if (IsCapsLockOn()) {
Swap(index); Swap(index);
ToggleCapsLock();
} else { } else {
TabTo(index); TabTo(index);
} }