How to send keystrokes to minimized window

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
seamoon
Posts: 36
Joined: 02 Mar 2016, 13:15

How to send keystrokes to minimized window

31 Aug 2022, 09:37

I want to send {ALTDOWN}{F4}{ALTUP} to minimized application without bring to front. I can minimize it but can't do the rest of it.

Code: Select all

DetectHiddenWindows On

Run, "c:\Program Files\KYE\LuxeMate525\LuxeMate525.exe",,min ; 
ControlSend,, {ALTDOWN}{F4}{ALTUP}, Genius Gaming Keyboard LuxeMate 525;
#NoTrayIcon
gregster
Posts: 9111
Joined: 30 Sep 2013, 06:48

Re: How to send keystrokes to minimized window

31 Aug 2022, 10:54

It's better to put a space between keyname and state (although AHK perhaps accepts your syntax, not sure): {ALT DOWN}
But is there really a ; at the end of the wintitle Genius Gaming Keyboard LuxeMate 525; ? If not, remove it! You don't put semicolons at the end of AHK code lines.
(it would be okay, if would put a space or tab before the ; - then it would start an inline comment.)
seamoon
Posts: 36
Joined: 02 Mar 2016, 13:15

Re: How to send keystrokes to minimized window

01 Sep 2022, 06:15

No luck. Any solution offer. It shouldn't be so hard to send keystrokes or close command to minimized app.
thinkRand
Posts: 44
Joined: 02 Mar 2022, 04:16
Location: Somewhere in Venezuela
Contact:

Re: How to send keystrokes to minimized window

01 Sep 2022, 07:38

This should work

Code: Select all

Run, "c:\Program Files\KYE\LuxeMate525\LuxeMate525.exe",,min, pid 
WinWait ahk_pid %pid%
ControlSend,ahk_parent, {ALT DOWN}{F4}{ALT UP}, ahk_pid %pid%
#NoTrayIcon
Discord: thinkRand#7433 (UID 681695022600814642)
seamoon
Posts: 36
Joined: 02 Mar 2016, 13:15

Re: How to send keystrokes to minimized window

01 Sep 2022, 10:17

thinkRand wrote:
01 Sep 2022, 07:38
This should work

Code: Select all

Run, "c:\Program Files\KYE\LuxeMate525\LuxeMate525.exe",,min, pid 
WinWait ahk_pid %pid%
ControlSend,ahk_parent, {ALT DOWN}{F4}{ALT UP}, ahk_pid %pid%
#NoTrayIcon
I created this but it didn't send keystrokes.

Code: Select all

Run, "c:\Program Files\KYE\LuxeMate525\LuxeMate525.exe",,min, 1548 
WinWait ahk_1548 %pid%
ControlSend,ahk_parent, {ALT DOWN}{F4}{ALT UP}, ahk_1548 %pid%
#NoTrayIcon
pto
Posts: 29
Joined: 24 Aug 2022, 17:21

Re: How to send keystrokes to minimized window

01 Sep 2022, 20:38

; is for only commenting in code. Every character afterwards until the end of the line is mostly for readability and understanding of code execution.

Since your goal is a minimized window (hence not hidden) you don't need DetectHiddenWindows On

Code: Select all

#NoTrayIcon ; if you don't want a related to this script icon on your desktop tray
exeApp := "C:\Program Files\KYE\LuxeMate525\LuxeMate525.exe"
string := "{ALTDOWN}{F4}{ALTUP}"

Run % exeApp,, min, exeAppPId ; run application and get process id to the variable "exeAppPId"
ControlSend,, % string, % "ahk_pid " . exeAppPId ; send the "string" variable to the program binded by PID
seamoon
Posts: 36
Joined: 02 Mar 2016, 13:15

Re: How to send keystrokes to minimized window

02 Sep 2022, 06:45

pto wrote:
01 Sep 2022, 20:38
; is for only commenting in code. Every character afterwards until the end of the line is mostly for readability and understanding of code execution.

Since your goal is a minimized window (hence not hidden) you don't need DetectHiddenWindows On

Code: Select all

#NoTrayIcon ; if you don't want a related to this script icon on your desktop tray
exeApp := "C:\Program Files\KYE\LuxeMate525\LuxeMate525.exe"
string := "{ALTDOWN}{F4}{ALTUP}"

Run % exeApp,, min, exeAppPId ; run application and get process id to the variable "exeAppPId"
ControlSend,, % string, % "ahk_pid " . exeAppPId ; send the "string" variable to the program binded by PID
The code doesn' work, it only minimize, can't close the app.
thinkRand
Posts: 44
Joined: 02 Mar 2022, 04:16
Location: Somewhere in Venezuela
Contact:

Re: How to send keystrokes to minimized window

02 Sep 2022, 09:00

seamoon wrote:
01 Sep 2022, 10:17
thinkRand wrote:
01 Sep 2022, 07:38
This should work

Code: Select all

Run, "c:\Program Files\KYE\LuxeMate525\LuxeMate525.exe",,min, pid 
WinWait ahk_pid %pid%
ControlSend,ahk_parent, {ALT DOWN}{F4}{ALT UP}, ahk_pid %pid%
#NoTrayIcon
I created this but it didn't send keystrokes.

Code: Select all

Run, "c:\Program Files\KYE\LuxeMate525\LuxeMate525.exe",,min, 1548 
WinWait ahk_1548 %pid%
ControlSend,ahk_parent, {ALT DOWN}{F4}{ALT UP}, ahk_1548 %pid%
#NoTrayIcon

You modified the script wrong.

Code: Select all

Run, "c:\Program Files\KYE\LuxeMate525\LuxeMate525.exe",,min, pid  ;pid is the name of the variable in which to store the newly launched program's unique Process ID (PID).

;After the Run command retrieves a PID, any windows to be created by the process might not exist yet. To wait for at least one window to be created, use
;WinWait ahk_pid %OutputVarPID%.

WinWait ahk_pid %pid% 
ControlSend, ahk_parent, {ALT DOWN}{F4}{ALT UP}, ahk_pid %pid%  ;Use ahk_pid PID in WinTitle to identify a window belonging to a specific process
#NoTrayIcon
Discord: thinkRand#7433 (UID 681695022600814642)
pto
Posts: 29
Joined: 24 Aug 2022, 17:21

Re: How to send keystrokes to minimized window

02 Sep 2022, 09:54

seamoon wrote: The code doesn' work, it only minimize, can't close the app.
You don't need ControlSend to close an application if you have its process id, wich you can easely get when you use Run command to launch:

Code: Select all

exeApp := "C:\Program Files\KYE\LuxeMate525\LuxeMate525.exe"

Run, % exeApp,, min, exeAppPId ; run application and get process id to the variable "exeAppPId"

WinClose, % "ahk_pid " . exeAppPId ; sends a close message to a window  by providing its process id
seamoon
Posts: 36
Joined: 02 Mar 2016, 13:15

Re: How to send keystrokes to minimized window

06 Sep 2022, 04:59

pto wrote:
02 Sep 2022, 09:54
seamoon wrote: The code doesn' work, it only minimize, can't close the app.
You don't need ControlSend to close an application if you have its process id, wich you can easely get when you use Run command to launch:

Code: Select all

exeApp := "C:\Program Files\KYE\LuxeMate525\LuxeMate525.exe"

Run, % exeApp,, min, exeAppPId ; run application and get process id to the variable "exeAppPId"

WinClose, % "ahk_pid " . exeAppPId ; sends a close message to a window  by providing its process id
I've pasted your code as it is (without any change) but didn't close the app.
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: How to send keystrokes to minimized window

06 Sep 2022, 06:23

A few issues with PIDs: some Run commands will not return a PID. In other cases, programs can use multiple processes, making it more difficult to identify the right PID. You might want to use a window handle instead. This can be retrieved after a WinWait.
seamoon
Posts: 36
Joined: 02 Mar 2016, 13:15

Re: How to send keystrokes to minimized window

06 Sep 2022, 10:24

mikeyww wrote:
06 Sep 2022, 06:23
A few issues with PIDs: some Run commands will not return a PID. In other cases, programs can use multiple processes, making it more difficult to identify the right PID. You might want to use a window handle instead. This can be retrieved after a WinWait.
But how can I add winwait command.
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: How to send keystrokes to minimized window

06 Sep 2022, 10:54

Code: Select all

GroupAdd, np, ahk_exe notepad.exe
WinMinimize, ahk_group np
Run, notepad
WinWaitActive, ahk_group np,, 5
If ErrorLevel
 MsgBox, 48, Error, An error occurred while waiting for the window.
hWnd := WinActive()
SoundBeep, 1500
Sleep, 500
WinClose, ahk_id %hWnd%
SoundBeep, 1000
seamoon
Posts: 36
Joined: 02 Mar 2016, 13:15

Re: How to send keystrokes to minimized window

07 Sep 2022, 04:38

mikeyww wrote:
06 Sep 2022, 10:54

Code: Select all

GroupAdd, np, ahk_exe notepad.exe
WinMinimize, ahk_group np
Run, notepad
WinWaitActive, ahk_group np,, 5
If ErrorLevel
 MsgBox, 48, Error, An error occurred while waiting for the window.
hWnd := WinActive()
SoundBeep, 1500
Sleep, 500
WinClose, ahk_id %hWnd%
SoundBeep, 1000
But in your code, the application window visible for a half-second. I want to launch it minimized to tray then without seeing the window close it.
Launching as minimized is possible in my app with this command but I can't close it in minimized state.
Run, "c:\Program Files\KYE\LuxeMate525\LuxeMate525.exe",,min
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: How to send keystrokes to minimized window

07 Sep 2022, 05:11

Code: Select all

winTitle = ahk_exe notepad.exe
WinGet, before, List, %winTitle%
Run, notepad,, Min
Loop {
 Sleep, 50
 WinGet, after, List, %winTitle%
} Until (after > before)
newest := after1
SoundBeep, 1500
Sleep, 500

; F3::
WinClose, ahk_id %newest%
SoundBeep, 1000
Return
Windows TASKKILL is another way to end a process (broadly). Also: Process.
seamoon
Posts: 36
Joined: 02 Mar 2016, 13:15

Re: How to send keystrokes to minimized window

07 Sep 2022, 13:40

mikeyww wrote:
07 Sep 2022, 05:11

Code: Select all

winTitle = ahk_exe notepad.exe
WinGet, before, List, %winTitle%
Run, notepad,, Min
Loop {
 Sleep, 50
 WinGet, after, List, %winTitle%
} Until (after > before)
newest := after1
SoundBeep, 1500
Sleep, 500

; F3::
WinClose, ahk_id %newest%
SoundBeep, 1000
Return
Windows TASKKILL is another way to end a process (broadly). Also: Process.
Thank you very much mikeyww. It worked great.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Kodakku and 359 guests