R
R
River00512020-03-10 18:41:23
PowerShell
River0051, 2020-03-10 18:41:23

How to activate the exe program window and click on the button?

Good afternoon, there are several program windows with the same process name, how can I make each window active in turn and click on the button in the window? does not become active and the button is pressed in another window, but on some it works, tell me how you can make it work always and everywhere, I probably made a mistake somewhere in the script or missed something I

did this exe.bat

FOR /F "tokens=*" %%G IN ('wmic process where name^="game.exe" get ProcessId ^| FINDSTR /v ProcessId ^| FINDSTR /r /v "^$"') DO (
  %nircmdc% win activate process /!PIDS!%%G
  %nircmdc% sendkeypress f1
  TIMEOUT /T 1 /NOBREAK
)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
AUser0, 2020-03-10
@River0051

So after activating the window, pause after TIMEOUT so that the window has time to receive this activation ...

M
MaxKozlov, 2020-03-11
@MaxKozlov

An example of sending the F1 button to the Visoal Studio Code window using PowerShell
To work directly with the Windows window API, use the
https://github.com/MVKozlov/Powershell-WindowHelper module . \Modules\WindowHelper " or in " C:\Users\%Username%\Documents\WindowsPowerShell\Modules\WindowHelper "

# Тут можно импортировать модуль напрямую, если не класть его по вышеуказанным путям
#Import-Module C:\Path\To\Module\WindowHelper.psm1
# Или просто импортировать, если есть в путях (необязательно)
#Import-Module WindowHelper

# Импортируем типы для использования SendKeys
Add-Type -AssemblyName System.Windows.Forms

# Ищем среди всех окон окно с нужным заголовком
$w = Get-ChildWindows | ? { $_.Text -eq 'Visual Studio Code' }
# Клавиши, которые нужно нажимать
# Брать отсюда - https://docs.microsoft.com/ru-ru/dotnet/api/system.windows.forms.sendkeys
$KeySequence = '{F1}'
# Активируем нужное окно
Set-ForegroundWindow $w.Handle
# Небольшая задержка
Start-Sleep -Milliseconds 100
# Посылаем клавиши
[System.Windows.Forms.SendKeys]::SendWait($KeySequence)

PS In the process of preparation, I remembered that the process and powershell should be in the same context - you don’t need to launch it specially from under the admin

I
Igor_Ya, 2021-04-08
@Igor_Ya

$wshell = New-Object -ComObject wscript.shell;
$winName='Untitled - Notepad'
$rez=$wshell.AppActivate($winName);
$rez

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question