I am launching a game using powershell for my arcade cabinet, the script works just how I want, but the program “XArcade_XInput.exe” opens the browser whenever it is opened. Is there a way to add something to this script to either have the browser window not show up, like a “Hide UI” like you can do with a bat, or a way to at least have it open minimized and close when exit the script, like how the script closes “XArcade_XInput” at the same time it closes MK11.
# FORCE Window to Focus
function Show-Window {
param(
[Parameter(Mandatory)]
[string] $ProcessName
)
# As a courtesy, strip '.exe' from the name, if present.
$ProcessName = $ProcessName -replace '\.exe$'
# Get the PID of the first instance of a process with the given name
# that has a non-empty window title.
# NOTE: If multiple instances have visible windows, it is undefined
# which one is returned.
$hWnd = (Get-Process -ErrorAction Ignore $ProcessName).Where({ $_.MainWindowTitle }, 'First').MainWindowHandle
if (-not $hWnd) { Throw "No $ProcessName process with a non-empty window title found." }
$type = Add-Type -PassThru -NameSpace Util -Name SetFgWin -MemberDefinition @'
[DllImport("user32.dll", SetLastError=true)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", SetLastError=true)]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll", SetLastError=true)]
public static extern bool IsIconic(IntPtr hWnd); // Is the window minimized?
'@
# Note:
# * This can still fail, because the window could have bee closed since
# the title was obtained.
# * If the target window is currently minimized, it gets the *focus*, but its
# *not restored*.
$null = $type::SetForegroundWindow($hWnd)
# If the window is minimized, restore it.
# Note: We don't call ShowWindow() *unconditionally*, because doing so would
# restore a currently *maximized* window instead of activating it in its current state.
if ($type::IsIconic($hwnd)) {
$type::ShowWindow($hwnd, 9) # SW_RESTORE
}
}
# Hide PowerShell Console
Add-Type -Name Window -Namespace Console -MemberDefinition '
[DllImport("Kernel32.dll")]
public static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
'
$consolePtr = [Console.Window]::GetConsoleWindow()
[Console.Window]::ShowWindow($consolePtr, 0)
$EXEFile="D:\Games\Mortal Kombat 11\Binaries\Retail\MK11.exe"
$DebugPreference='silentlyContinue'
$ErrorActionPreference='ignore'
$EXEBaseName=Get-ChildItem $EXEFile
$EXEBaseName=$EXEBaseName.BaseName
# Gets the simple name of the EXE to use to check the process status
#$InitialWaitTime=1
#This is the amount of time for the initial load, and the amount of time it will continue to try if EXE takes too long or is
#updating (a common issue with Steam type games)
$ApplicationNotLoaded=$True
# Flap to detect if the game ever initially loaded
Add-Type -TypeDefinition '
using System;
using System.IO;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace KeyLogger {
public static class Program {
private const int WH_KEYBOARD_LL = 13;
private const int WM_KEYDOWN = 0x0100;
private static HookProc hookProc = HookCallback;
private static IntPtr hookId = IntPtr.Zero;
private static int keyCode = 0;
[DllImport("user32.dll")]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll")]
private static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("kernel32.dll")]
private static extern IntPtr GetModuleHandle(string lpModuleName);
public static int WaitForKey() {
hookId = SetHook(hookProc);
Application.Run();
UnhookWindowsHookEx(hookId);
return keyCode;
}
private static IntPtr SetHook(HookProc hookProc) {
IntPtr moduleHandle = GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName);
return SetWindowsHookEx(WH_KEYBOARD_LL, hookProc, moduleHandle, 0);
}
private delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) {
if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN) {
keyCode = Marshal.ReadInt32(lParam);
Application.Exit();
}
return CallNextHookEx(hookId, nCode, wParam, lParam);
}
}
}
' -ReferencedAssemblies System.Windows.Forms
Write-Host "Launching XArcade_XInput"
Start-Process "D:\EmulatorStuff\xarcade-xinput\XArcade_XInput" -WindowStyle Minimized
Write-Host "Launching $EXEBaseName"
& $EXEFile
write-host "Waiting an initial $InitialWaitTime seconds for the EXE to load"
Start-Sleep $InitialWaitTime
While ($ApplicationNotLoaded) {
IF (get-process -name $EXEBaseName) {
write-host "EXE Loaded"
$ApplicationNotLoaded=$False
}
if ($ApplicationNotLoaded -eq $False) {
write-host "Leaving Process"
Break
} ELSE {
write-host "Application still not loaded, waiting $InitialWaitTime seconds."
Start-Sleep $InitialWaitTime
}
}
write-host "Base Name: " $EXEBaseName
write-host "Monitoring for key presses"
Show-Window "Mortal Kombat 11"
Trap {"" ;continue}
while (get-process -name $EXEBaseName) {
$key = [System.Windows.Forms.Keys][KeyLogger.Program]::WaitForKey()
if ($key -eq [char]27) {
# ESC was pressed close the EXE and exit the loop
write-host "Key Pressed, killing process"
& stop-process -Name $EXEBaseName
break
}
#Switch ($key) {
# X {
# Write-Host "X Pressed"
# }
#}
}
write-host "Stopping XArcade_XInput"
stop-process -Name "XArcade_XInput"
#stop-process -Name "Steam"