Compare commits
3 Commits
603bc488e8
...
458a466c15
Author | SHA1 | Date | |
---|---|---|---|
458a466c15 | |||
5e95937ae9 | |||
48d3af7c94 |
@@ -57,6 +57,7 @@
|
||||
<DependentUpon>Form1.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="HotKeyManager.cs" />
|
||||
<Compile Include="KeyboardHook.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<EmbeddedResource Include="Properties\Resources.resx">
|
||||
|
64
DD2Switcher/KeyboardHook.cs
Normal file
64
DD2Switcher/KeyboardHook.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Windows.Forms;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
class KeyboardHook {
|
||||
private const int WH_KEYBOARD_LL = 13;
|
||||
private const int WM_KEYDOWN = 0x0104;
|
||||
private const int WM_KEYUP = 0x0101;
|
||||
private static LowLevelKeyboardProc _proc = HookCallback;
|
||||
private static IntPtr _hookID = IntPtr.Zero;
|
||||
|
||||
public static int previousEvent = 0;
|
||||
public static event EventHandler<int> KeyDown;
|
||||
public static event EventHandler<int> KeyUp;
|
||||
|
||||
public static void Start() {
|
||||
_hookID = SetHook(_proc);
|
||||
}
|
||||
|
||||
public static void Stop() {
|
||||
UnhookWindowsHookEx(_hookID);
|
||||
}
|
||||
|
||||
private static IntPtr SetHook(LowLevelKeyboardProc proc) {
|
||||
using (Process curProcess = Process.GetCurrentProcess()) using (ProcessModule curModule =
|
||||
curProcess.MainModule) {
|
||||
return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
|
||||
}
|
||||
}
|
||||
|
||||
private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
|
||||
|
||||
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) {
|
||||
if (nCode >= 0) {
|
||||
int vkCode = Marshal.ReadInt32(lParam);
|
||||
int pero = (int)wParam * 1000 + vkCode;
|
||||
if (pero != previousEvent) {
|
||||
if (wParam == (IntPtr)WM_KEYDOWN) {
|
||||
KeyDown?.Invoke(null, vkCode);
|
||||
} else if (wParam == (IntPtr)WM_KEYUP) {
|
||||
KeyUp?.Invoke(null, vkCode);
|
||||
}
|
||||
previousEvent = pero;
|
||||
} else {
|
||||
Console.WriteLine("Same event");
|
||||
}
|
||||
}
|
||||
return CallNextHookEx(_hookID, nCode, wParam, lParam);
|
||||
}
|
||||
|
||||
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
||||
private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
|
||||
|
||||
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
||||
[return:MarshalAs(UnmanagedType.Bool)]
|
||||
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
|
||||
|
||||
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
||||
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
|
||||
|
||||
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
||||
private static extern IntPtr GetModuleHandle(string lpModuleName);
|
||||
}
|
@@ -4,15 +4,21 @@ using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Windows.Forms;
|
||||
using System.Threading;
|
||||
|
||||
namespace DD2Switcher {
|
||||
internal static class Program {
|
||||
private static int NumProc = 19;
|
||||
private static Process[] windows = new Process[NumProc];
|
||||
private static int ActiveIndex = -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();
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
public static extern IntPtr GetForegroundWindow();
|
||||
@@ -144,21 +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}");
|
||||
}
|
||||
|
||||
// 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;
|
||||
@@ -199,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;
|
||||
}
|
||||
}
|
||||
@@ -221,6 +235,9 @@ namespace DD2Switcher {
|
||||
Console.WriteLine($"Window at index {index} does not exist, removing from tracked windows");
|
||||
windows[index] = null;
|
||||
} else {
|
||||
if (ActiveIndex != -1)
|
||||
PushHistory(ActiveIndex);
|
||||
|
||||
SetForegroundWindow(window.MainWindowHandle);
|
||||
ActiveIndex = index;
|
||||
AdjustAffinities();
|
||||
@@ -228,10 +245,43 @@ namespace DD2Switcher {
|
||||
}
|
||||
}
|
||||
|
||||
private static void TabToPrevious() {
|
||||
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();
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static bool IsCapsLockOn() {
|
||||
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();
|
||||
@@ -245,6 +295,27 @@ namespace DD2Switcher {
|
||||
Process.GetCurrentProcess().Kill();
|
||||
}
|
||||
|
||||
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);
|
||||
// Register main number keys (0-9)
|
||||
for (int i = 0; i < 10; i++) HotKeyManager.RegisterHotKey(Keys.D0 + i, KeyModifiers.Alt);
|
||||
@@ -256,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) {
|
||||
@@ -273,14 +345,16 @@ namespace DD2Switcher {
|
||||
if (e.Modifiers == KeyModifiers.Alt) {
|
||||
if (IsCapsLockOn()) {
|
||||
Swap(index);
|
||||
ToggleCapsLock();
|
||||
} else {
|
||||
TabTo(index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user