Show - Hide Toggle of AlwayOnTop Window

Get help with using AutoHotkey and its commands and hotkeys
ark
Posts: 32
Joined: 20 Jan 2018, 18:23

Show - Hide Toggle of AlwayOnTop Window

20 Jan 2018, 20:47

Installed AHK today and have been doing some reading. What I need to do is develop a script that will let me designate a window as always on top, and then be able to toggle that window on and off (show and hide) whether are not it has focus (whether active or not). And when on or shown, I don't want the focus to be automatically transferred to the window.
I know how to use WinSet to designate the window as always on top. Then, based on what I've been able to learn, I need to assign a Title to the window so I can identify it in order to toggle it on and off using WinHide and WinShow. And if it is hidden, I have to make it visible first. So the approach to the script would be something like:

1. Use WinSet, alwayson top, , A to designate the always on top window
2. Use WinGetActiveTitle, Outputvar to get the title of this window
3. Not sure how to use Outputvar -- can I use this variable for the Title parameter in WinShow and WinHide? Do I need to get the windows unique ID/HWND and use it somehow?
Then before trying to toggle the window on or off
3. Turn on DetectHiddenWindows
4. If the window if off, use WinShow, Title to turn it on (show it),
and if the window is on use WinHide ,Title to turn it off ( hide it)

As I write this, it also occurs to me I'm not sure about the difference between minimize/restore and hide/show. It may be WinMinimize and WinRestore that I want to use, but I don't want the focus to shift to the restored window, I just want it readable.

That's about my limited understanding at this point. Thanks for any guidance.

Edit: It now seems to me I will need two scripts, one to designate the alwaysontop window, and the other to implement the toggle hide/show function for that window. If so, how do I let the toggle script know the title of the alwaysontop window -- some kind of global title parameter?

Al
User avatar
noname
Posts: 507
Joined: 19 Nov 2013, 09:15

Re: Show - Hide Toggle of AlwayOnTop Window

21 Jan 2018, 03:05

example: be aware that a title of a window can change after you got the value , you can use SetTitleMatchMode if needed.

Change the hotkeys they are impracticable , but my edit software steals all the standard ones :(

Code: Select all

run,notepad.exe   ; just for demo 
toggle:=0

numpad0::   ; select window to hide example here is active window
WinGetActiveTitle, a_title
WinSet, alwaysontop ,on,%a_title%
return

numpad1::
toggle:=!toggle
if toggle
  WinHide, %a_title%
else
  WinShow, %a_title%
return

esc::exitapp
ark
Posts: 32
Joined: 20 Jan 2018, 18:23

Re: Show - Hide Toggle of AlwayOnTop Window

21 Jan 2018, 13:36

Making progress --thanks noname!
Two issues:
1. I need to make the WinHide toggle work even when the alwaysontop window does not have focus. So I guess I somehow have to make additional use of the window title or ID to give the window focus( if necessary) just before the WinHide command.
2. I tried (couple of ways) to use SetTitle MatchMode but keep getting the warning that the global variable %a_title% has not been assigned a value. Here's one of the codings I tried: EDIT:This warning problem seems to have gone away!?
But I don't really know if what I've done will handle the situation if the name of the window does change.

Code: Select all

toggle:=0
^SPACE::   ; select window to hide example here is active window
WinGetActiveTitle, a_title
WinSet, alwaysontop ,toggle,%a_title%
return

+t::
WinSetTitle, %a_title%
SetTitleMatchMode, 3
toggle:=!toggle
if toggle
  WinHide, %a_title%
else
  WinShow, %a_title%
return
esc::exitapp
User avatar
noname
Posts: 507
Joined: 19 Nov 2013, 09:15

Re: Show - Hide Toggle of AlwayOnTop Window

21 Jan 2018, 14:15

You can use the handle to the window instead of the title.It should work focus or not.

Code: Select all

toggle:=0  ; choose a window  to show/hide and make it active
^SPACE::   ; you only have to do this once 
winget , hwnd,ID,A  ; this will set hwnd to the handle of the window 
WinSet, alwaysontop ,on,ahk_id %hwnd%
return

+t::
toggle:=!toggle
if toggle
  WinHide, ahk_id %hwnd%
else
  WinShow, ahk_id %hwnd%
return
esc::exitapp
ark
Posts: 32
Joined: 20 Jan 2018, 18:23

Re: Show - Hide Toggle of AlwayOnTop Window

21 Jan 2018, 14:56

Unfortunately that didn't work. If after designating the alwaysontop window I put the focus on another window, the alwaysontop window will not hide.
I tried adding the line WinActivate, ahk_id %hwnd% to the WinHide block but that didn't help. Also tried adding #WinActivateForce to the script, but didn't help. Perhaps I need a delay between WinActive and WinHide?
Al

Code: Select all

toggle:=0  ; choose a window  to show/hide and make it active
^SPACE::   ; you only have to do this once 
winget , hwnd,ID,A  ; this will set hwnd to the handle of the window 
WinSet, alwaysontop ,toggle,ahk_id %hwnd%
return

+t::
toggle:=!toggle
if toggle
 {
  WinActivate, ahk_id %hwnd%
  WinHide, ahk_id %hwnd%
 }
else
  WinShow, ahk_id %hwnd%
return
esc::exitapp
User avatar
noname
Posts: 507
Joined: 19 Nov 2013, 09:15

Re: Show - Hide Toggle of AlwayOnTop Window

21 Jan 2018, 15:15

Sorry i misunderstood that it should "auto" hide when not active.

Code: Select all

toggle:=0  ; choose a window  to show/hide and make it active
^SPACE::   ; you only have to do this once 
winget , hwnd,ID,A  ; this will set hwnd to the handle of the window
WinSet, alwaysontop ,on,ahk_id %hwnd%
SetTimer, hide,500
return

hide:
IfWinNotActive, ahk_id %hwnd%
winhide ,ahk_id %hwnd%
return


+t::
WinShow, ahk_id %hwnd%
WinActivate, ahk_id %hwnd%
WinSet, alwaysontop ,on,ahk_id %hwnd%
return
esc::exitapp
ark
Posts: 32
Joined: 20 Jan 2018, 18:23

Re: Show - Hide Toggle of AlwayOnTop Window

21 Jan 2018, 15:30

I'm sorry, I was not clear. It should not "auto" hide when the focus is elsewhere, but hide when toggled if the focus is elsewhere, just as it should if it has the focus. So in other words, the user should be able to use a key (+t) to toggle the alwaysontop window between Hide and Show whether or not the window has focus. Tried the below, didn't work.

Thanks for the time you have put into this!
Al

Code: Select all

toggle:=0  ; choose a window  to show/hide and make it active
^SPACE::   ; you only have to do this once 
winget , hwnd,ID,A  ; this will set hwnd to the handle of the window 
WinSet, alwaysontop ,toggle,ahk_id %hwnd%
return

+t::
toggle:=!toggle
if toggle = 1
 {
  IfWinNotActive, ahk_id %hwnd%
  WinHide, ahk_id %hwnd%
 }
else
  WinShow, ahk_id %hwnd%
return
esc::exitapp
ark
Posts: 32
Joined: 20 Jan 2018, 18:23

Re: Show - Hide Toggle of AlwayOnTop Window

21 Jan 2018, 22:07

Below is the another script I tried, but again I could not hide the inactive window. And, as I been working on this, I've come to realize that for my application, when the hidden window is restored (Shown), it needs to be inactive so as not to take the focus from the 'main' window. So in short the alwaysontop window must not take the focus away from the main window when hidden or shown. Perhaps the best way to think of the overall application is that the smaller alwaysontop window has information (say instructions) for using the large(main) window. So you would like to keep the information in view while operating with the main window. At times, however, you want to hide the instructions and then later make them viewable again all without losing focus on the main window.
Al

Code: Select all

toggle:=0  ; choose a window  to show/hide and make it active
^SPACE::   ; you only have to do this once 
WinGet , hwnd,ID,A  ; this will set hwnd to the handle of the window 
WinSet, alwaysontop ,toggle,ahk_id %hwnd%
return

+t::
toggle:=!toggle
if toggle = 1                                 ;hide active or inactive window
 {
   IfWinNotActive, ahk_id %hwnd%
   {
     WinActivate, ahk_id %hwnd%  ; make active so can be hidden?
     WinHide, ahk_id %hwnd%
   } 
   WinHide, ahk_id %hwnd%         ;hide if window was active
 }
else
  WinShow, ahk_id %hwnd%         ;show hidden window 
return
esc::exitapp
User avatar
noname
Posts: 507
Joined: 19 Nov 2013, 09:15

Re: Show - Hide Toggle of AlwayOnTop Window

22 Jan 2018, 03:10

Instead of hiding it seems easier to switch the alwaysontop, it keeps the main window as active window unless you click in the "small" window.

example using notepad and wordpad as mainwindow , i used part of title as "wintitle" be aware title is case sensitve!

Code: Select all


settitlematchmode 2

;------- create windows wordpad main window ----------
run, wordpad.exe
winwait,WordPad
WinMaximize, WordPad
Run, notepad.exe
winwait ,Notepad
return



+t::
toggle:=!toggle
if toggle
{
WinSet, alwaysontop,on, WordPad
WinSet, alwaysontop,off,Notepad
WinActivate, WordPad
}
else
WinSet, alwaysontop,on,Notepad
WinSet, alwaysontop,off, WordPad
WinActivate, WordPad
return

esc::exitapp
ark
Posts: 32
Joined: 20 Jan 2018, 18:23

Re: Show - Hide Toggle of AlwayOnTop Window

22 Jan 2018, 13:00

Clever idea! I will experiment with this when I get home later today and report back.
Thx,
Al
ark
Posts: 32
Joined: 20 Jan 2018, 18:23

Re: Show - Hide Toggle of AlwayOnTop Window

22 Jan 2018, 17:06

Tried a number of things without success. Besides the code below, I also tried using the set of instructions you see commented out on the right.
Setup procedure was:
I set the main window active, and then pushed Shift+F1.
I then popped up the small window (web site) made sure it was active and pushed Shift+F2.
Then trying the toggle with Shift+t, nothing usually happened although a few times I got an error message saying the window titles (or handles) had not been initialized.
I also tried putting % signs around the titles in the toggle code area, e.g, WinSet, alwaysontop, on , %main_win%.
Al
EDIT: Found out the Shift+F1 and Shift+F2 hotkeys below were being trapped by something and were not seen by the script, so doing some more experimenting.

Code: Select all

settitlematchmode 2
toggle:=0  ; choose a window 

+F1::   ; select main window
WinGetActiveTitle, main_win               ;WinGet, main_win,ID,A  this will set big_win to the handle of the main window 
return

+F2::   ; select small window
WinGetActiveTitle, sm_win                 ;WinGet, sm_win,ID,A     this will set sm_win to the handle of the small active window 
return

+t::
toggle:=!toggle
if toggle
{
WinSet, alwaysontop, on, main_win        ;WinSet, alwaysontop, on, ahk_id %main_win%     
WinSet, alwaysontop, off, sm_win         ;WinSet, alwaysontop, off, ahk_id %sm_win%
WinActivate, main_win                    ;WinActivate, ahk_id %main_win%
}
else
{
WinSet, alwaysontop, on, sm_win           ;WinSet, alwaysontop, on, ahk_id %sm_win% 
WinSet, alwaysontop, off, main_win        ;WinSet, alwaysontop, off, ahk_id %main_win% 
WinActivate, main_win                     ;WinActivate, ahk_id %main_win%
}
return
esc::exitapp
ark
Posts: 32
Joined: 20 Jan 2018, 18:23

Re: Show - Hide Toggle of AlwayOnTop Window

22 Jan 2018, 21:13

I put the MsgBox lines in as a debugging aid to try and see what the script was doing. I get the first two messages about the window IDs, and then if I hit +t I get the toggling message and the message for the Main_Win, but nothing after that. Pushing +t again does nothing, no more toggling messages. So it seems the script hangs for some reason (I do close the MsgBox).
Al

toggle:=0 ; choose a window

^SPACE:: ; select main window
WinGet, main_win,ID,A ;this will set big_win to the handle of the main window
MsgBox, The active window's ID is "%main_win%"
return

+SPACE:: ; select small window
WinGet, sm_win,ID,A ;this will set sm_win to the handle of the small active window
MsgBox, The active window's ID is "%sm_win%"
return

+t::
MsgBox, toggling
toggle:=!toggle
if toggle
{
WinSet, alwaysontop, off, ahk_id %sm_win%
WinSet, alwaysontop, on, ahk_id %main_win%
WinActivate, ahk_id %main_win%
MsgBox, Main_Win always on_top
}
else
{
WinSet, alwaysontop, off, ahk_id %main_win%
WinSet, alwaysontop, on, ahk_id %sm_win%
WinActivate, ahk_id %main_win%
MsgBox, Sm_Win always on_top
}
return

esc::exitapp
ark
Posts: 32
Joined: 20 Jan 2018, 18:23

Re: Show - Hide Toggle of AlwayOnTop Window

22 Jan 2018, 22:06

As a point of interest, the code below has the same strange behavior as in the post above -- can only activate toggling once.

EDIT: The toggling seems to work fot this script and the one above, if the small window is active on top of the main window. And, strangely, if both windows are minimized the toggling works ( meaning I get the MsgBox messages ) although neither of the windows become visible. Very odd.

Code: Select all

toggle:=0  ; choose a window  to show/hide and make it active
^SPACE::   ; you only have to do this once 
winget , hwnd,ID,A  ; this will set hwnd to the handle of the window 
WinSet, alwaysontop ,on,ahk_id %hwnd%
MsgBox, Handle of small show/hide window is "%hwnd%"
return

+SPACE::
toggle:=!toggle
MsgBox, Toggling
if toggle = 1
 {
  WinHide, ahk_id %hwnd%
  MsgBox, hide small window
 }
else
{
  WinShow, ahk_id %hwnd%
  WinSet, alwaysontop ,on,ahk_id %hwnd%
  MsgBox, show small window
}
return
esc::exitapp
ark
Posts: 32
Joined: 20 Jan 2018, 18:23

Re: Show - Hide Toggle of AlwayOnTop Window

23 Jan 2018, 00:46

Well, it seems the toggling problem is tied to the particular application I'm trying to use as the 'large or main' window, which is Microsoft's flight simulator program. If I use another application, like WORD for the main window, or a website as the main window, then the two scripts above seem to work. If the flight simulation window has the focus, the toggling doesn't work....sigh.
Al
User avatar
noname
Posts: 507
Joined: 19 Nov 2013, 09:15

Re: Show - Hide Toggle of AlwayOnTop Window

23 Jan 2018, 05:42

As an alternative you could move the small window offscreen ( and back).
WinMove, WinTitle, WinText, X, Y [, Width, Height, ExcludeTitle, ExcludeText]

Be aware of the syntax ! The parameter after WinTitle is Wintext ( or just empty ,, )
ark
Posts: 32
Joined: 20 Jan 2018, 18:23

Re: Show - Hide Toggle of AlwayOnTop Window

23 Jan 2018, 10:59

Hmm... another interesting idea. I will certainly give movong the little window a try. But if I'm understanding, that also means I will need to 'sneak' a keystroke past the simulator window, and apparently when the simulator window has focus it is trapping any keystroke, even if it is not a key, or key combination, that it uses. Is that typical for a "game"? Is there any way to 'elevate' the AHK script so it has first shot at a keystroke?

There is clearly something (probably lots!) that I don't understand. In particular, when a script first starts and the simulator window has focus, a keystroke (like ^SPACE) to retrieve the window's handle via WinGet works, meaning the window did not trap the keystroke. Yet if I rearrange keystroke assignments to use ^SPACE as the toggle key, it seems to be trapped by the window when it has focus. Why is that -- why did the keystroke get through to the script one time, but not the other time? What am I missing?

Or, maybe something else is going on besides key trapping when the main window has focus?

Thx,
Al
User avatar
noname
Posts: 507
Joined: 19 Nov 2013, 09:15

Re: Show - Hide Toggle of AlwayOnTop Window

23 Jan 2018, 11:48

If it is difficult to find a practical hotkey that is not "stolen" you can always use a "hotcorner" .I find it really easy to have hotspots when the mouse enters the area a command is executed like a hotkey.( Unless real Mouseposition is also stolen..... :o )

You find code for hotspots on the forum but you can use a timer and mousegetpos to code one.

When i use PSPad editor almost all hotkeys are usurped and i am left with numpad keys which is troublesome..........
ark
Posts: 32
Joined: 20 Jan 2018, 18:23

Re: Show - Hide Toggle of AlwayOnTop Window

23 Jan 2018, 15:26

I'm going to hunt further to see if I can find a key or key combination that won't be captured. Unfortunately, one of the goals for the flight application I'm working on is to NOT have to use the mouse, but rather to eventually send the necessary key codes from a joystick or flight yoke type of device.
Al
ark
Posts: 32
Joined: 20 Jan 2018, 18:23

Re: Show - Hide Toggle of AlwayOnTop Window

23 Jan 2018, 21:03

Noname,
The key combo Ctrl+Spacebar works for the toggle function. I should of thought of trying that earlier.
So it seems things are working now, at least well enough to test when actually using the flight simulator. :P
Thanks very much for what you have taught me over the past few days!
Al

Return to “Ask For Help”

Who is online

Users browsing this forum: Ernest and 128 guests