Compare commits
3 Commits
603bc488e8
...
458a466c15
Author | SHA1 | Date | |
---|---|---|---|
458a466c15 | |||
5e95937ae9 | |||
48d3af7c94 |
@@ -57,6 +57,7 @@
|
|||||||
<DependentUpon>Form1.cs</DependentUpon>
|
<DependentUpon>Form1.cs</DependentUpon>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="HotKeyManager.cs" />
|
<Compile Include="HotKeyManager.cs" />
|
||||||
|
<Compile Include="KeyboardHook.cs" />
|
||||||
<Compile Include="Program.cs" />
|
<Compile Include="Program.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<EmbeddedResource Include="Properties\Resources.resx">
|
<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.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 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();
|
||||||
|
|
||||||
[DllImport("user32.dll")]
|
[DllImport("user32.dll")]
|
||||||
public static extern IntPtr GetForegroundWindow();
|
public static extern IntPtr GetForegroundWindow();
|
||||||
@@ -144,21 +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}");
|
||||||
}
|
}
|
||||||
|
|
||||||
// private static void TrackWows() {
|
private static void TrackProcess() {
|
||||||
// CleanWindows();
|
CleanWindows();
|
||||||
// var processes = Process.GetProcesses().OrderBy(p => p.Id).ToList();
|
var foregroundWindow = GetForegroundWindow();
|
||||||
// foreach (var process in processes) {
|
var foregroundProcess = Process.GetProcesses().FirstOrDefault(p => p.MainWindowHandle == foregroundWindow);
|
||||||
// if (process.ProcessName == "UWow-64") {
|
if (foregroundProcess == null) {
|
||||||
// Console.WriteLine($"Found UWow-64 at pid {process.Id}");
|
Console.WriteLine("No foreground process found");
|
||||||
// if (ProcessTracked(process.Id))
|
return;
|
||||||
// continue;
|
}
|
||||||
// Track(process);
|
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) {
|
private static void Swap(int index) {
|
||||||
index = (index - 1) % NumProc;
|
index = (index - 1) % NumProc;
|
||||||
@@ -199,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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -221,6 +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 {
|
||||||
|
if (ActiveIndex != -1)
|
||||||
|
PushHistory(ActiveIndex);
|
||||||
|
|
||||||
SetForegroundWindow(window.MainWindowHandle);
|
SetForegroundWindow(window.MainWindowHandle);
|
||||||
ActiveIndex = index;
|
ActiveIndex = index;
|
||||||
AdjustAffinities();
|
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() {
|
private static bool IsCapsLockOn() {
|
||||||
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();
|
||||||
@@ -245,6 +295,27 @@ namespace DD2Switcher {
|
|||||||
Process.GetCurrentProcess().Kill();
|
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);
|
HotKeyManager.RegisterHotKey(Keys.Capital, 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);
|
||||||
@@ -256,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) {
|
||||||
@@ -273,14 +345,16 @@ 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Console.CancelKeyPress += (sender, e) => { Process.GetCurrentProcess().Kill(); };
|
// Console.CancelKeyPress += (sender, e) => { Process.GetCurrentProcess().Kill(); };
|
||||||
while (true) System.Threading.Thread.Sleep(100000);
|
Application.Run();
|
||||||
|
KeyboardHook.Stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user