Skip to content

Commit 22b7f57

Browse files
committed
Send enterkey to battle.net client in background
* changed sendkey method to work even in background
1 parent a07a2a0 commit 22b7f57

File tree

2 files changed

+81
-10
lines changed

2 files changed

+81
-10
lines changed

bnetlauncher/Clients/BnetClient2.cs

+14-9
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,23 @@ public override bool Launch(string cmd)
7878
DateTime start = DateTime.Now;
7979
while (DateTime.Now.Subtract(start).TotalMinutes < 1)
8080
{
81-
if (Windows.IsForegroundWindowTitle("Blizzard Battle.net"))
81+
// Agressivly scans for the battle.net client window to hit the foreground
82+
foreach (var window in Process.GetProcesses())
8283
{
83-
// Small pause to give time for UI to update before
84-
// sending the keypress, no wait will case it to launch
85-
// the last game opened.
86-
Thread.Sleep(500);
84+
if (window.MainWindowTitle == "Blizzard Battle.net")
85+
{
86+
Logger.Information($"Found windows for battle.net client.");
8787

88-
SendKeys.SendWait("{ENTER}");
89-
return true;
90-
}
88+
// Small pause to give time for UI to update before
89+
// sending the keypress, no wait will case it to launch
90+
// the last game opened.
91+
Thread.Sleep(500);
9192

92-
Thread.Sleep(10);
93+
// Force the battle.net client to the foreground.
94+
Windows.SendEnterByHandle(window.MainWindowHandle);
95+
return true;
96+
}
97+
}
9398
}
9499

95100
Logger.Error("Failed to detect Battle.net client window in the foreground.");

bnetlauncher/Utils/Windows.cs

+67-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics;
23
using System.Runtime.InteropServices;
34
using System.Text;
45

@@ -8,6 +9,9 @@ public static class Windows
89
{
910
internal static class NativeMethods
1011
{
12+
public const int WM_KEYDOWN = 0x100;
13+
public const int VK_RETURN = 0x0D;
14+
1115
[DllImport("user32.dll")]
1216
public static extern IntPtr GetForegroundWindow();
1317

@@ -16,10 +20,16 @@ internal static class NativeMethods
1620

1721
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
1822
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString,int nMaxCount);
23+
24+
[DllImport("User32.dll")]
25+
public static extern int SetForegroundWindow(IntPtr point);
26+
27+
[DllImport("user32.dll", CharSet = CharSet.Auto)]
28+
public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, IntPtr lParam);
1929
}
2030

2131

22-
static public bool IsForegroundWindowTitle(string title)
32+
static public bool IsForegroundWindowByTitle(string title)
2333
{
2434
IntPtr hwnd = NativeMethods.GetForegroundWindow();
2535

@@ -36,5 +46,61 @@ static public bool IsForegroundWindowTitle(string title)
3646
Logger.Information($"Foreground Window title = '{windowtitle.ToString()}'");
3747
return (windowtitle.ToString().Equals(title, StringComparison.OrdinalIgnoreCase));
3848
}
49+
50+
static public bool SetForegroundWindowByTitle(string title)
51+
{
52+
try
53+
{
54+
var client = Process.GetProcessesByName(title)[0];
55+
return NativeMethods.SetForegroundWindow(client.MainWindowHandle) != 0;
56+
}
57+
catch (Exception ex)
58+
{
59+
Logger.Error($"Exception wile trying to bring '{title}' to the foreground.", ex);
60+
}
61+
62+
return false;
63+
}
64+
65+
static public bool SetForegroundWindowByHandle(IntPtr handle)
66+
{
67+
if (handle != null)
68+
{
69+
return NativeMethods.SetForegroundWindow(handle) != 0;
70+
}
71+
return false;
72+
}
73+
74+
static public bool SendEnterByTitle(string title)
75+
{
76+
var windows = Process.GetProcesses();
77+
78+
bool sent = false;
79+
foreach (var window in windows)
80+
{
81+
if (window.MainWindowTitle == title)
82+
{
83+
NativeMethods.SendMessage(window.MainWindowHandle,
84+
NativeMethods.WM_KEYDOWN, NativeMethods.VK_RETURN, IntPtr.Zero);
85+
86+
Logger.Information($"Sending Enter to '{window.MainWindowTitle}'");
87+
sent = true;
88+
}
89+
}
90+
91+
return sent;
92+
}
93+
94+
static public void SendEnterByHandle(IntPtr handle)
95+
{
96+
if (handle == IntPtr.Zero)
97+
{
98+
Logger.Error($"Givne null handle. aborting...");
99+
return;
100+
}
101+
102+
Logger.Information("Sending enter key to window");
103+
NativeMethods.SendMessage(handle, NativeMethods.WM_KEYDOWN, NativeMethods.VK_RETURN, IntPtr.Zero);
104+
}
39105
}
40106
}

0 commit comments

Comments
 (0)