[SOLVED]Hide/Unhide window with hotkeys

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
F13key
Posts: 7
Joined: 30 Dec 2015, 08:12

[SOLVED]Hide/Unhide window with hotkeys

Post by F13key » 14 Aug 2022, 11:20

I'm trying to write a script to hide/unhide a specific window but am having no luck.

I followed the examples in the help file and am able to get the script working - providing the window title is an exact match (it is a browser window). Once you start navigating various web pages the script fails.

Code: Select all

WinWait, *Mypal

NumpadAdd::WinHide
Numpad0::WinShow

RCtrl::
suspend
return
I would like the script to hide/unhide the window with the title ending in Mypal since that's the only consistency but am not sure how to do it. Alternately, I would like to do it with the process name (mypal.exe) but haven't had any luck.

I'm sure it's something simple but I'm stumped.
Last edited by F13key on 14 Aug 2022, 11:43, edited 1 time in total.

User avatar
boiler
Posts: 16923
Joined: 21 Dec 2014, 02:44

Re: Hide/Unhide window with hotkeys

Post by boiler » 14 Aug 2022, 11:31

Code: Select all

SetTitleMatchMode, RegEx

NumpadAdd::WinHide, Mypal$
Numpad0::WinShow, Mypal$

RCtrl::Suspend

Or:

Code: Select all

NumpadAdd::WinHide, ahk_exe mypal.exe
Numpad0::WinShow, ahk_exe mypal.exe

RCtrl::Suspend

F13key
Posts: 7
Joined: 30 Dec 2015, 08:12

Re: Hide/Unhide window with hotkeys

Post by F13key » 14 Aug 2022, 11:42

Wow! Thanks! That was fast!!! Just tested, works perfectly.

Thanks again for the fast response! :D :bravo:

F13key
Posts: 7
Joined: 30 Dec 2015, 08:12

Re: [SOLVED]Hide/Unhide window with hotkeys

Post by F13key » 21 Mar 2023, 10:38

I have been using this script for a while now and it works well, thanks again to boiler.

Code: Select all

SetTitleMatchMode, RegEx

NumpadAdd::WinHide, Mypal$
Numpad0::WinShow, Mypal$

RCtrl::Suspend
I want to modify it so when the script is suspended it will display a different icon other than the stock. I have two .ico files and have tried the following:

Code: Select all

SetWorkingDir %A_ScriptDir%

SetTitleMatchMode, RegEx

NumpadAdd::WinHide, Mypal$
Numpad0::WinShow, Mypal$
RCtrl::
suspend
GoTO refreshICON
Sus:
Suspend, Toggle      
refreshICON:
if (A_IsSuspended)
Menu, Tray, Icon , red_s.ico
Else
Menu, Tray, Icon , normal.ico
Return
The script still works but the icons are still the stock icons, not the custom ones I am pointing too. What am I doing wrong?

User avatar
boiler
Posts: 16923
Joined: 21 Dec 2014, 02:44

Re: [SOLVED]Hide/Unhide window with hotkeys

Post by boiler » 21 Mar 2023, 13:41

I don't have time to debug/test, but some quick things I see:

Don't ever use Goto (unless you have to, and if you think you do, the answer is almost always that you don't). Use Gosub.

When you move the command off of the same line as the hotkey label and add more commands below, then add a return at the end.

Why do you have a "Sus:" label but never refer to it?

F13key
Posts: 7
Joined: 30 Dec 2015, 08:12

Re: [SOLVED]Hide/Unhide window with hotkeys

Post by F13key » 21 Mar 2023, 13:57

Thanks for the reply. It was from an example I found on the web. I've made a few mods including removing the SUS label but still the icons are stock, not the ones I have in the script.

Code: Select all

SetWorkingDir %A_ScriptDir%

SetTitleMatchMode, RegEx

NumpadAdd::WinHide, Mypal$
Numpad0::WinShow, Mypal$

RCtrl::suspend
GoSub refreshICON
refreshICON:
if (A_IsSuspended)
Menu, Tray, Icon , red_s.ico
Else
Menu, Tray, Icon , normal.ico
Return

User avatar
boiler
Posts: 16923
Joined: 21 Dec 2014, 02:44

Re: [SOLVED]Hide/Unhide window with hotkeys

Post by boiler » 21 Mar 2023, 15:21

Now that Gosub can never execute because you put the line above it back on the same line as the hotkey label, so it’s the only line associated with the hotkey.

off
Posts: 176
Joined: 18 Nov 2022, 21:54

Re: [SOLVED]Hide/Unhide window with hotkeys

Post by off » 21 Mar 2023, 18:57

Code: Select all

SetWorkingDir %A_ScriptDir%

SetTitleMatchMode, RegEx

NumpadAdd::WinHide, Mypal$
Numpad0::WinShow, Mypal$

RCtrl::
suspend
GoSub refreshICON
; without return here, RCtrl can also trigger refreshICON without GoSub.
refreshICON:
if (A_IsSuspended)
Menu, Tray, Icon , red_s.ico
Else
Menu, Tray, Icon , normal.ico
Return
My Creations
IMG2HotString - Send image file easily with your hotstring!
CtrlSend - A small solution for sending keys to window in background that doesn't accept ControlSend's key
ControlProcess

User avatar
boiler
Posts: 16923
Joined: 21 Dec 2014, 02:44

Re: [SOLVED]Hide/Unhide window with hotkeys

Post by boiler » 21 Mar 2023, 19:22

off wrote:
21 Mar 2023, 18:57

Code: Select all

; without return here, RCtrl can also trigger refreshICON without GoSub.
In fact, you should either put the return there or remove the Gosub because the way you have it, it will execute the Menu command twice, which won’t cause anything noticeable, but there’s no reason to do that.

F13key
Posts: 7
Joined: 30 Dec 2015, 08:12

Re: [SOLVED]Hide/Unhide window with hotkeys

Post by F13key » 22 Mar 2023, 11:51

Thanks to both of you for your help - I've tried both suggestions, the script still functions but the tray icons are still stock.

I want to post another working script I have where the icons do in fact change to the ones I have specified. Bear in mind this and the other script are launched from the same directory, the .ico files are in that directory and both using AutoHotKey v1.1.22.07. Please also bear in mind I am no coder, using Autohotkey for the most basic of scripts - the very first script I wrote was to disable the Win key which was a huge nuisance during my FPS gaming sessions.

Code: Select all

#NoEnv
#persistent
SetWorkingDir %A_ScriptDir%
#SingleInstance force

 
Menu, Tray, NoStandard
Menu, Tray, Add , Pause, Pau
Menu, Tray, Add , Suspend, Sus
Menu, Tray, Add , Reload, Rel
Menu, Tray, Add , Exit, Exi
Menu, Tray, Default, Exit
Menu, Tray, Icon , normal.ico,, 1
Return


F10::Media_Prev
F12::Media_Next
F11::Media_Play_Pause
F1::Volume_Mute
F2::Volume_Down
F3::Volume_Up

 
Pau:
MsgBox Test
Return
 
RCtrl::
suspend
GoTO refreshICON
Sus:
Suspend, Toggle      
refreshICON:
if (A_IsSuspended)
Menu, Tray, Icon , red_s.ico
Else
Menu, Tray, Icon , normal.ico
Return
Exi:
ExitApp
Return
Rel:
Reload
Return

Post Reply

Return to “Ask for Help (v1)”