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 Process[] windows = new Process[NumProc];
private static int ActiveIndex = -1;
private static int PreviousIndex = -1;
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 fullAffinity = new(0xFFFFFFFF);
private static readonly KeyboardHook keyboardHook = new();
@@ -148,22 +150,32 @@ namespace DD2Switcher {
// Add the new process at the first null slot
windows[firstNullIndex] = process;
PushHistory(firstNullIndex);
ActiveIndex = firstNullIndex;
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();
// foreach (var process in processes) {
// if (process.ProcessName == "UWow-64") {
// Console.WriteLine($"Found UWow-64 at pid {process.Id}");
// if (ProcessTracked(process.Id))
// continue;
// Track(process);
// }
// }
// }
private static void TrackProcess() {
CleanWindows();
var foregroundWindow = GetForegroundWindow();
var foregroundProcess = Process.GetProcesses().FirstOrDefault(p => p.MainWindowHandle == foregroundWindow);
if (foregroundProcess == null) {
Console.WriteLine("No foreground process found");
return;
}
Console.WriteLine($"Foreground process: {foregroundProcess.ProcessName} PID: {foregroundProcess.Id}");
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) {
index = (index - 1) % NumProc;
@@ -204,9 +216,6 @@ namespace DD2Switcher {
windows[i] = windows[index];
windows[index] = window;
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;
}
}
@@ -226,7 +235,9 @@ namespace DD2Switcher {
Console.WriteLine($"Window at index {index} does not exist, removing from tracked windows");
windows[index] = null;
} else {
PreviousIndex = ActiveIndex;
if (ActiveIndex != -1)
PushHistory(ActiveIndex);
SetForegroundWindow(window.MainWindowHandle);
ActiveIndex = index;
AdjustAffinities();
@@ -235,26 +246,24 @@ namespace DD2Switcher {
}
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");
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();
for (int i = lastWindows.Count - 1; i >= 0; i--) {
int index = lastWindows[i];
if (index != ActiveIndex) {
TabTo(index+1); // Our windows are 1-indexed because... I don't remember why
break;
}
}
}
@@ -262,6 +271,17 @@ namespace DD2Switcher {
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]
private static void Main() {
// AllocConsole();
@@ -307,10 +327,11 @@ namespace DD2Switcher {
HotKeyManager.HotKeyPressed += HotKeyManager_HotKeyPressed;
void HotKeyManager_HotKeyPressed(object sender, HotKeyEventArgs e) {
// if (e.Key == Keys.Oemtilde && e.Modifiers == KeyModifiers.Alt) {
// TrackWows();
// return;
// }
if (e.Key == Keys.Oemtilde && e.Modifiers == KeyModifiers.Alt && IsCapsLockOn()) {
TrackProcess();
ToggleCapsLock();
return;
}
int index;
if (e.Key >= Keys.D0 && e.Key <= Keys.D9) {
@@ -324,6 +345,7 @@ namespace DD2Switcher {
if (e.Modifiers == KeyModifiers.Alt) {
if (IsCapsLockOn()) {
Swap(index);
ToggleCapsLock();
} else {
TabTo(index);
}