diff --git a/WinJump.sln b/WinJump.sln index 0da8f48..fefdfe7 100644 --- a/WinJump.sln +++ b/WinJump.sln @@ -10,13 +10,13 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {22774C1D-A813-493F-AB33-AE3A4049FADC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {22774C1D-A813-493F-AB33-AE3A4049FADC}.Debug|Any CPU.Build.0 = Debug|Any CPU {22774C1D-A813-493F-AB33-AE3A4049FADC}.Release|Any CPU.ActiveCfg = Release|Any CPU {22774C1D-A813-493F-AB33-AE3A4049FADC}.Release|Any CPU.Build.0 = Release|Any CPU - {153091D1-8AF0-4609-AA38-DB01F11D0C4A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {153091D1-8AF0-4609-AA38-DB01F11D0C4A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {22774C1D-A813-493F-AB33-AE3A4049FADC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {22774C1D-A813-493F-AB33-AE3A4049FADC}.Debug|Any CPU.Build.0 = Debug|Any CPU {153091D1-8AF0-4609-AA38-DB01F11D0C4A}.Release|Any CPU.ActiveCfg = Release|Any CPU {153091D1-8AF0-4609-AA38-DB01F11D0C4A}.Release|Any CPU.Build.0 = Release|Any CPU + {153091D1-8AF0-4609-AA38-DB01F11D0C4A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {153091D1-8AF0-4609-AA38-DB01F11D0C4A}.Debug|Any CPU.Build.0 = Debug|Any CPU EndGlobalSection EndGlobal diff --git a/WinJump/Program.cs b/WinJump/Program.cs index 8b0d515..4ae4c58 100644 --- a/WinJump/Program.cs +++ b/WinJump/Program.cs @@ -1,32 +1,34 @@ using System; using System.Diagnostics; +using System.Runtime.InteropServices; using System.Threading; using System.Windows.Forms; using VirtualDesktop.VirtualDesktop; namespace WinJump { internal static class Program { + [DllImport("user32.dll")] + private static extern bool SetForegroundWindow(IntPtr hWnd); + + [DllImport("user32.dll")] + private static extern IntPtr GetForegroundWindow(); + [STAThread] public static void Main() { var killExplorer = Process.Start("cmd.exe", "/c taskkill /f /im explorer.exe"); killExplorer?.WaitForExit(); - - VirtualDesktopWrapper vdw = VirtualDesktopManager.Create(); - + var thread = new STAThread(); // Start a thread that can handle UI requests KeyboardHook hook = new KeyboardHook(); - + hook.KeyPressed += (sender, args) => { if (args.Key < Keys.D0 || args.Key > Keys.D9 || args.Modifier != ModifierKeys.Win) return; int index = args.Key == Keys.D0 ? 10 : (args.Key - Keys.D1); - - thread.Invoke(new Action(() => { - vdw.JumpTo(index); - })); + thread.JumpTo(index); }; for(var key = Keys.D0; key <= Keys.D9; key++) { @@ -45,6 +47,9 @@ public static void Main() { // Credit https://stackoverflow.com/a/21684059/4779937 private sealed class STAThread : IDisposable { + private readonly IntPtr[] LastActiveWindows = new IntPtr[10]; + private readonly VirtualDesktopWrapper vdw = VirtualDesktopManager.Create(); + public STAThread() { using (mre = new ManualResetEvent(false)) { var thread = new Thread(() => { @@ -62,11 +67,18 @@ public void BeginInvoke(Delegate dlg, params Object[] args) { if (ctx == null) throw new ObjectDisposedException("STAThread"); ctx.Post((_) => dlg.DynamicInvoke(args), null); } - public object Invoke(Delegate dlg, params Object[] args) { + public void JumpTo(int index) { if (ctx == null) throw new ObjectDisposedException("STAThread"); - object result = null; - ctx.Send((_) => result = dlg.DynamicInvoke(args), null); - return result; + + ctx.Send((_) => { + LastActiveWindows[vdw.GetDesktop()] = GetForegroundWindow(); + vdw.JumpTo(index); + // Give it just a little time to let the desktop settle + Thread.Sleep(10); + if (LastActiveWindows[index] != IntPtr.Zero) { + SetForegroundWindow(LastActiveWindows[index]); + } + }, null); } private void Initialize(object sender, EventArgs e) {