Use capslock instead of scrollock and code format (sadly in one...)

This commit is contained in:
2025-08-15 14:13:33 +02:00
parent cf804a6c8b
commit 603bc488e8
8 changed files with 495 additions and 516 deletions

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?> <?xml version="1.0" encoding="utf-8"?>
<configuration> <configuration>
<startup> <startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/>
</startup> </startup>
</configuration> </configuration>

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')"/> <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup> <PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
@@ -8,11 +8,12 @@
<OutputType>WinExe</OutputType> <OutputType>WinExe</OutputType>
<RootNamespace>DD2Switcher</RootNamespace> <RootNamespace>DD2Switcher</RootNamespace>
<AssemblyName>DD2Switcher</AssemblyName> <AssemblyName>DD2Switcher</AssemblyName>
<TargetFrameworkVersion>v4.8.1</TargetFrameworkVersion> <TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment> <FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic> <Deterministic>true</Deterministic>
<LangVersion>9.0</LangVersion> <LangVersion>9.0</LangVersion>
<TargetFrameworkProfile />
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>x64</PlatformTarget> <PlatformTarget>x64</PlatformTarget>
@@ -36,17 +37,17 @@
<Prefer32bit>false</Prefer32bit> <Prefer32bit>false</Prefer32bit>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="System"/> <Reference Include="System" />
<Reference Include="System.Core"/> <Reference Include="System.Core" />
<Reference Include="System.Xml.Linq"/> <Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions"/> <Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp"/> <Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data"/> <Reference Include="System.Data" />
<Reference Include="System.Deployment"/> <Reference Include="System.Deployment" />
<Reference Include="System.Drawing"/> <Reference Include="System.Drawing" />
<Reference Include="System.Net.Http"/> <Reference Include="System.Net.Http" />
<Reference Include="System.Windows.Forms"/> <Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml"/> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Form1.cs"> <Compile Include="Form1.cs">
@@ -55,9 +56,9 @@
<Compile Include="Form1.Designer.cs"> <Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon> <DependentUpon>Form1.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="HotKeyManager.cs"/> <Compile Include="HotKeyManager.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">
<Generator>ResXFileCodeGenerator</Generator> <Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput> <LastGenOutput>Resources.Designer.cs</LastGenOutput>
@@ -78,10 +79,10 @@
</Compile> </Compile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="App.config"/> <None Include="App.config" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="beep.wav"/> <Content Include="beep.wav" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets"/> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project> </Project>

View File

@@ -1,5 +1,5 @@
namespace DD2Switcher { namespace DD2Switcher {
partial class Form1 { partial class Form1 {
/// <summary> /// <summary>
/// Required designer variable. /// Required designer variable.
/// </summary> /// </summary>
@@ -32,5 +32,5 @@ partial class Form1 {
} }
#endregion #endregion
} }
} }

View File

@@ -1,7 +1,9 @@
using System.Windows.Forms; using System.Windows.Forms;
namespace DD2Switcher { namespace DD2Switcher {
public partial class Form1 : Form { public partial class Form1 : Form {
public Form1() { InitializeComponent(); } public Form1() {
} InitializeComponent();
}
}
} }

View File

@@ -4,7 +4,7 @@ using System.Threading;
using System.Windows.Forms; using System.Windows.Forms;
namespace DD2Switcher { namespace DD2Switcher {
public static class HotKeyManager { public static class HotKeyManager {
private static volatile MessageWindow _wnd; private static volatile MessageWindow _wnd;
private static volatile IntPtr _hwnd; private static volatile IntPtr _hwnd;
private static readonly ManualResetEvent _windowReadyEvent = new(false); private static readonly ManualResetEvent _windowReadyEvent = new(false);
@@ -12,8 +12,7 @@ public static class HotKeyManager {
private static int _id; private static int _id;
static HotKeyManager() { static HotKeyManager() {
var messageLoop = var messageLoop = new Thread(delegate() { Application.Run(new MessageWindow()); });
new Thread(delegate() { Application.Run(new MessageWindow()); });
messageLoop.Name = "MessageLoopThread"; messageLoop.Name = "MessageLoopThread";
messageLoop.IsBackground = true; messageLoop.IsBackground = true;
messageLoop.Start(); messageLoop.Start();
@@ -24,18 +23,15 @@ public static class HotKeyManager {
public static int RegisterHotKey(Keys key, KeyModifiers modifiers) { public static int RegisterHotKey(Keys key, KeyModifiers modifiers) {
_windowReadyEvent.WaitOne(); _windowReadyEvent.WaitOne();
var id = Interlocked.Increment(ref _id); var id = Interlocked.Increment(ref _id);
_wnd.Invoke(new RegisterHotKeyDelegate(RegisterHotKeyInternal), _hwnd, id, _wnd.Invoke(new RegisterHotKeyDelegate(RegisterHotKeyInternal), _hwnd, id, (uint)modifiers, (uint)key);
(uint)modifiers, (uint)key);
return id; return id;
} }
public static void UnregisterHotKey(int id) { public static void UnregisterHotKey(int id) {
_wnd.Invoke(new UnRegisterHotKeyDelegate(UnRegisterHotKeyInternal), _hwnd, _wnd.Invoke(new UnRegisterHotKeyDelegate(UnRegisterHotKeyInternal), _hwnd, id);
id);
} }
private static void RegisterHotKeyInternal(IntPtr hwnd, int id, private static void RegisterHotKeyInternal(IntPtr hwnd, int id, uint modifiers, uint key) {
uint modifiers, uint key) {
RegisterHotKey(hwnd, id, modifiers, key); RegisterHotKey(hwnd, id, modifiers, key);
} }
@@ -49,14 +45,12 @@ public static class HotKeyManager {
} }
[DllImport("user32", SetLastError = true)] [DllImport("user32", SetLastError = true)]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
uint fsModifiers, uint vk);
[DllImport("user32", SetLastError = true)] [DllImport("user32", SetLastError = true)]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id); private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
private delegate void RegisterHotKeyDelegate(IntPtr hwnd, int id, private delegate void RegisterHotKeyDelegate(IntPtr hwnd, int id, uint modifiers, uint key);
uint modifiers, uint key);
private delegate void UnRegisterHotKeyDelegate(IntPtr hwnd, int id); private delegate void UnRegisterHotKeyDelegate(IntPtr hwnd, int id);
@@ -83,9 +77,9 @@ public static class HotKeyManager {
base.SetVisibleCore(false); base.SetVisibleCore(false);
} }
} }
} }
public class HotKeyEventArgs : EventArgs { public class HotKeyEventArgs : EventArgs {
public readonly Keys Key; public readonly Keys Key;
public readonly KeyModifiers Modifiers; public readonly KeyModifiers Modifiers;
@@ -99,14 +93,8 @@ public class HotKeyEventArgs : EventArgs {
Key = (Keys)((param & 0xffff0000) >> 16); Key = (Keys)((param & 0xffff0000) >> 16);
Modifiers = (KeyModifiers)(param & 0x0000ffff); Modifiers = (KeyModifiers)(param & 0x0000ffff);
} }
} }
[Flags] [Flags]
public enum KeyModifiers { public enum KeyModifiers { Alt = 1, Control = 2, Shift = 4, Windows = 8, NoRepeat = 0x4000 }
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8,
NoRepeat = 0x4000
}
} }

View File

@@ -6,7 +6,7 @@ using System.Runtime.InteropServices;
using System.Windows.Forms; using System.Windows.Forms;
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;
@@ -28,8 +28,7 @@ internal static class Program {
static extern bool AllocConsole(); static extern bool AllocConsole();
[DllImport("user32.dll")] [DllImport("user32.dll")]
private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
int dwExtraInfo);
private static void CleanWindows() { private static void CleanWindows() {
for (int i = 0; i < NumProc; i++) { for (int i = 0; i < NumProc; i++) {
@@ -37,8 +36,7 @@ internal static class Program {
if (window == null) if (window == null)
continue; continue;
if (window.MainWindowHandle == IntPtr.Zero) { if (window.MainWindowHandle == IntPtr.Zero) {
Console.WriteLine( Console.WriteLine($"Window at index {i} has no main window, removing from tracked windows");
$"Window at index {i} has no main window, removing from tracked windows");
windows[i] = null; windows[i] = null;
} }
} }
@@ -146,8 +144,7 @@ internal static class Program {
// Add the new process at the first null slot // Add the new process at the first null slot
windows[firstNullIndex] = process; windows[firstNullIndex] = process;
Console.WriteLine( Console.WriteLine($"Added {process.ProcessName} to tracked windows at index {firstNullIndex}");
$"Added {process.ProcessName} to tracked windows at index {firstNullIndex}");
} }
// private static void TrackWows() { // private static void TrackWows() {
@@ -189,8 +186,7 @@ internal static class Program {
for (int i = 0; i < NumProc; i++) { for (int i = 0; i < NumProc; i++) {
var window = windows[i]; var window = windows[i];
if (window == null) { if (window == null) {
Console.WriteLine( Console.WriteLine($"Adding foreground window to tracked at index {i}...");
$"Adding foreground window to tracked at index {i}...");
windows[i] = process; windows[i] = process;
break; break;
} }
@@ -203,9 +199,9 @@ internal static class Program {
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 scroll lock off // Toggle caps lock off
keybd_event(0x91, 0, 0, 0); // KEYEVENTF_KEYDOWN keybd_event(0x14, 0, 0, 0); // KEYEVENTF_KEYDOWN
keybd_event(0x91, 0, 0x0002, 0); // KEYEVENTF_KEYUP keybd_event(0x14, 0, 0x0002, 0); // KEYEVENTF_KEYUP
return; return;
} }
} }
@@ -222,8 +218,7 @@ internal static class Program {
var window = windows[index]; var window = windows[index];
if (window == null || window.MainWindowHandle == IntPtr.Zero) { if (window == null || window.MainWindowHandle == IntPtr.Zero) {
Console.WriteLine( Console.WriteLine($"Window at index {index} does not exist, removing from tracked windows");
$"Window at index {index} does not exist, removing from tracked windows");
windows[index] = null; windows[index] = null;
} else { } else {
SetForegroundWindow(window.MainWindowHandle); SetForegroundWindow(window.MainWindowHandle);
@@ -233,7 +228,9 @@ internal static class Program {
} }
} }
private static bool IsScrollLockOn() { return (GetKeyState(0x91) & 1) == 1; } private static bool IsCapsLockOn() {
return (GetKeyState(0x14) & 1) == 1;
}
[STAThread] [STAThread]
private static void Main() { private static void Main() {
@@ -243,20 +240,17 @@ internal static class Program {
var currentProcess = Process.GetCurrentProcess(); var currentProcess = Process.GetCurrentProcess();
foreach (var process in processes) foreach (var process in processes)
if (process.Id != currentProcess.Id && if (process.Id != currentProcess.Id && process.ProcessName == currentProcess.ProcessName) {
process.ProcessName == currentProcess.ProcessName) {
process.Kill(); process.Kill();
Process.GetCurrentProcess().Kill(); Process.GetCurrentProcess().Kill();
} }
HotKeyManager.RegisterHotKey(Keys.Scroll, 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++) for (int i = 0; i < 10; i++) HotKeyManager.RegisterHotKey(Keys.D0 + i, KeyModifiers.Alt);
HotKeyManager.RegisterHotKey(Keys.D0 + i, KeyModifiers.Alt);
// Register numpad keys (1-9) // Register numpad keys (1-9)
for (int i = 0; i < 9; i++) for (int i = 0; i < 9; i++) HotKeyManager.RegisterHotKey(Keys.NumPad1 + i, KeyModifiers.Alt);
HotKeyManager.RegisterHotKey(Keys.NumPad1 + i, KeyModifiers.Alt);
HotKeyManager.RegisterHotKey(Keys.Oemtilde, KeyModifiers.Alt); HotKeyManager.RegisterHotKey(Keys.Oemtilde, KeyModifiers.Alt);
HotKeyManager.HotKeyPressed += HotKeyManager_HotKeyPressed; HotKeyManager.HotKeyPressed += HotKeyManager_HotKeyPressed;
@@ -277,7 +271,7 @@ internal static class Program {
} }
if (e.Modifiers == KeyModifiers.Alt) { if (e.Modifiers == KeyModifiers.Alt) {
if (IsScrollLockOn()) { if (IsCapsLockOn()) {
Swap(index); Swap(index);
} else { } else {
TabTo(index); TabTo(index);
@@ -285,10 +279,8 @@ internal static class Program {
} }
} }
Console.CancelKeyPress += Console.CancelKeyPress += (sender, e) => { Process.GetCurrentProcess().Kill(); };
(sender, e) => { Process.GetCurrentProcess().Kill(); }; while (true) System.Threading.Thread.Sleep(100000);
while (true) }
System.Threading.Thread.Sleep(100000);
} }
} }
}

View File

@@ -9,27 +9,26 @@
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
namespace DD2Switcher.Properties { namespace DD2Switcher.Properties {
using System; using System;
/// <summary>
/// A strongly-typed resource class, for looking up localized strings, etc.
/// </summary>
// This class was auto-generated by the StronglyTypedResourceBuilder
// class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project.
[global::System.CodeDom.Compiler.GeneratedCodeAttribute(
"System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class Resources {
/// <summary>
/// A strongly-typed resource class, for looking up localized strings, etc.
/// </summary>
// This class was auto-generated by the StronglyTypedResourceBuilder
// class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project.
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder",
"17.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class Resources {
private static global::System.Resources.ResourceManager resourceMan; private static global::System.Resources.ResourceManager resourceMan;
private static global::System.Globalization.CultureInfo resourceCulture; private static global::System.Globalization.CultureInfo resourceCulture;
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute( [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance",
"Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] "CA1811:AvoidUncalledPrivateCode")]
internal Resources() {} internal Resources() {}
/// <summary> /// <summary>
@@ -40,8 +39,7 @@ internal class Resources {
internal static global::System.Resources.ResourceManager ResourceManager { internal static global::System.Resources.ResourceManager ResourceManager {
get { get {
if (object.ReferenceEquals(resourceMan, null)) { if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager(
new global::System.Resources.ResourceManager(
"DD2Switcher.Properties.Resources", typeof(Resources).Assembly); "DD2Switcher.Properties.Resources", typeof(Resources).Assembly);
resourceMan = temp; resourceMan = temp;
} }
@@ -59,5 +57,5 @@ internal class Resources {
get { return resourceCulture; } get { return resourceCulture; }
set { resourceCulture = value; } set { resourceCulture = value; }
} }
} }
} }

View File

@@ -9,18 +9,16 @@
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
namespace DD2Switcher.Properties { namespace DD2Switcher.Properties {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute( [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
"Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", [global::System.CodeDom.Compiler.GeneratedCodeAttribute(
"11.0.0.0")] "Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.14.0.0")]
internal sealed partial class Settings internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
: global::System.Configuration.ApplicationSettingsBase {
private static Settings defaultInstance = private static Settings defaultInstance =
((Settings)(global::System.Configuration.ApplicationSettingsBase ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
.Synchronized(new Settings())));
public static Settings Default { public static Settings Default {
get { return defaultInstance; } get { return defaultInstance; }
} }
} }
} }