Skip to content

Commit

Permalink
Fix foreground issue between the windows
Browse files Browse the repository at this point in the history
  • Loading branch information
widavies committed Dec 14, 2022
1 parent bb86df9 commit f3cf0ed
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
8 changes: 4 additions & 4 deletions WinJump.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
36 changes: 24 additions & 12 deletions WinJump/Program.cs
Original file line number Diff line number Diff line change
@@ -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++) {
Expand All @@ -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(() => {
Expand All @@ -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) {
Expand Down

0 comments on commit f3cf0ed

Please sign in to comment.