Hotkey should run ONLY in Fullscreen mode Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
MaxEnrik
Posts: 15
Joined: 07 May 2019, 19:36

Hotkey should run ONLY in Fullscreen mode

10 May 2019, 13:13

I would like to my special hotkeys should run ONLY in Fullscreen mode otherwise don't need to run hotkeys.

Code: Select all

#SingleInstance, force
#NoEnv
SetBatchLines, -1
;@Icarus - I copied and modified/customized.
;I don't achieved my goal.

IfWinExist ahk_exe XYplorer.exe
    WinActivate
;Sleep 1500
MsgBox Press OK to Start!

isFullScreen := isWindowFullScreen( "A" )
MsgBox % isFullScreen ? "Full Screen" : "Windowed"
MsgBox %isFullScreen%

if(%isFullScreen% = 1)
{
	IfWinActive ahk_exe XYplorer
		MsgBox Full Screen Mode now run your hotkeys!
		1::F8
}
else
	if(%isFullScreen% = 0)
		MsgBox Full Screen Mode OFF now Don't Change Hotkeys!
		Return

isWindowFullScreen( winTitle )
{   
	winID := WinExist( winTitle )
	If ( !winID )
		Return false
	
	WinGet style, Style, ahk_id %WinID%
	WinGetPos ,,,winW,winH, %winTitle%
	Return ((style & 0x20800000) or winH < A_ScreenHeight or winW < A_ScreenWidth) ? false : true
}
Any help and even code scripts would be greatly appreciated.
( I spent 12hrs to achieve my goal but failed )
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Hotkey should run ONLY in Fullscreen mode

10 May 2019, 20:06

You need to use the #If directives with hotkeys. Try this:

Code: Select all

#If WinActive("ahk_exe XYplorer") and isFullScreen
1::F8
Also, just FYI, when you use If(), what goes inside the parentheses must be in expression syntax, which means no % signs around variables (unless you're double dereferencing, but that's not common). So, if(%isFullScreen% = 1) is invalid and should be if(isFullScreen = 1). It doesn't matter here, since we're using #If instead of If(), but I'm just pointing it out for your future reference.
MaxEnrik
Posts: 15
Joined: 07 May 2019, 19:36

Re: Hotkey should run ONLY in Fullscreen mode

10 May 2019, 20:32

Thanks for your reply and explanation, highly appreciate that.
So I tried that but doesn't run either in Fullscreen or Restored mode.
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Hotkey should run ONLY in Fullscreen mode

10 May 2019, 20:54

I just noticed your MsgBox % isFullScreen ? "Full Screen" : "Windowed" line, which is probably messing it up. Either remove that or try:

Code: Select all

#If WinActive("ahk_exe XYplorer") and isFullScreen = "Full Screen"
1::F8
or

Code: Select all

#If WinActive("ahk_exe XYplorer") and isWindowFullScreen("ahk_exe XYplorer")
1::F8
The latter would be more reliable, since it would evaluate whether the window is full screen more than just once when the script was started.
MaxEnrik
Posts: 15
Joined: 07 May 2019, 19:36

Re: Hotkey should run ONLY in Fullscreen mode

10 May 2019, 21:38

I remove all MsgBox so still doesn't work.
Also I changed hotkey to but still doesn't run correctly.

Code: Select all

1:: Send {PgUp}
gregster
Posts: 9001
Joined: 30 Sep 2013, 06:48

Re: Hotkey should run ONLY in Fullscreen mode

10 May 2019, 22:45

Try this:

Code: Select all

#SingleInstance, force
#NoEnv
SetBatchLines, -1

Space::MsgBox % isWindowFullScreen( "A" ) ? "Full Screen" : "Windowed"

#If isWindowFullScreen( "A" )	; creates context-sensitive hotkey(s)
1::PgDn
#If				; end of context-sensitive hotkey section

isWindowFullScreen( winTitle )
{   
	winID := WinExist( winTitle )
	If ( !winID )
		Return false
	
	WinGet style, Style, ahk_id %WinID%
	WinGetPos ,,,winW,winH, %winTitle%
	Return ((style & 0x20800000) or winH < A_ScreenHeight or winW < A_ScreenWidth) ? false : true
}
You can press Space now to check if the isWindowFullScreen( ) function is working.

If the active window is fullscreen, 1 is now remapped to the Pgdn (PageDown) key - I tested it briefly in Firefox fullscreen mode (press F11 to toggle it in Firefox).

You didn't mention what this is for... if it's a game, there might be further complications, but then I am probably the wrong guy to ask
(please see then https://www.autohotkey.com/boards/viewtopic.php?t=11084)
MaxEnrik
Posts: 15
Joined: 07 May 2019, 19:36

Re: Hotkey should run ONLY in Fullscreen mode

10 May 2019, 23:03

You just awesome!
I tested it works correctly I just need ask just for sure that below whole script is safety to use after my minor changes? ( but still works )

Code: Select all

#SingleInstance, force
#NoEnv
SetBatchLines, -1

#IfWinActive ahk_exe XYplorer.exe	; I just need to this hotkey should work only for this XYplorer program when this program is in Fullscreen

#If isWindowFullScreen( "A" )	; creates context-sensitive hotkey(s)
1::PgDn
#If				; end of context-sensitive hotkey section

isWindowFullScreen( winTitle )
{   
	winID := WinExist( winTitle )
	If ( !winID )
		Return false
	
	WinGet style, Style, ahk_id %WinID%
	WinGetPos ,,,winW,winH, %winTitle%
	Return ((style & 0x20800000) or winH < A_ScreenHeight or winW < A_ScreenWidth) ? false : true
}
gregster
Posts: 9001
Joined: 30 Sep 2013, 06:48

Re: Hotkey should run ONLY in Fullscreen mode

10 May 2019, 23:23

I just need ask just for sure that below whole script is safety to use after my minor changes?
I really have no clue what that means in the context of this single remapping...!?
What are you asking exactly?
MaxEnrik
Posts: 15
Joined: 07 May 2019, 19:36

Re: Hotkey should run ONLY in Fullscreen mode

11 May 2019, 00:41

I am really noob for scripting/coding. And I really confused about #If and if() and I just needed to be sure if I added #IfWinActive ahk_exe XYplorer.exe correctly to that great script.
So I am worrying because in a bit early before your first post in this topic, I made a mistake when I needed to try another script scenario and so there was some code caused infinity looping some action so I wasn't able to stop it, I restarted machine then removed that scenario.

And sorry I missed your question about if I tried something for game. Answer is no I don't use anything for Game in this top. This topic is all about XYplorer File Manager for Windows.
gregster
Posts: 9001
Joined: 30 Sep 2013, 06:48

Re: Hotkey should run ONLY in Fullscreen mode  Topic is solved

11 May 2019, 01:55

Ah, I see now what you did.
I think the #IfWinActive ahk_exe XYplorer.exe is cancelled out by the #If isWindowFullScreen( "A" ) line, so that the hotkey will still work on all fullscreen windows. You can't stack these directives.
https://autohotkey.com/docs/commands/_If.htm#Basic_Operation wrote:Like the #IfWin directives, #If is positional: it affects all hotkeys and hotstrings physically beneath it in the script. #If and #IfWin are also mutually exclusive; that is, only the most recent #If or #IfWin will be in effect.
#If and #Ifwin... directives, these with the #, are only suitable for handling/creating (context-sensitive) hotkeys and hotstrings. In all other cases, you will need to use the normal if.

I would try it like this, strictly looking at this XYplorer window:

Code: Select all

#SingleInstance, force
#NoEnv
SetBatchLines, -1

#If isActiveWindowFullScreen( "ahk_exe XYplorer.exe" )	; creates context-sensitive hotkey(s)
1::PgDn
#If				; end of context-sensitive hotkey section

isActiveWindowFullScreen( winTitle )
{   
	If !(winID := WinActive(wintitle))
		return false
	
	WinGet style, Style, ahk_id %WinID%
	WinGetPos ,,,winW,winH, %winTitle%
	Return ((style & 0x20800000) or winH < A_ScreenHeight or winW < A_ScreenWidth) ? false : true
}

I made a mistake when I needed to try another script scenario and so there was some code caused infinity looping some action so I wasn't able to stop it, I restarted machine then removed that scenario.
Next time, at least for testing, you could add a hotkey like Esc::ExitApp to your script, so that the Esc key (or some other key combo you want) closes your script. If this fails, it should always be possible to close all autohotkey.exe processes in the Windows task manager. Then, restarting the computer is usually not necessary.

Edited code again
MaxEnrik
Posts: 15
Joined: 07 May 2019, 19:36

Re: Hotkey should run ONLY in Fullscreen mode

11 May 2019, 02:22

I always looks for compact code script when I was looking for any code. So your last change and attempt was just amazing.
I really highly appreciate that.

Thank you so much more once again!
gregster
Posts: 9001
Joined: 30 Sep 2013, 06:48

Re: Hotkey should run ONLY in Fullscreen mode

11 May 2019, 02:46

Thanks!
Yeah, if you use functions with #If, it is always important that the function evaluates quickly (to prevent lag). So it could be that there are even more efficient methods (perhaps using #IfWinActive for creating the context-sensitive hotkey and then calling a function, that just checks for fullscreen, from the hotkey routine itself)... but this should work.
MaxEnrik
Posts: 15
Joined: 07 May 2019, 19:36

Re: Hotkey should run ONLY in Fullscreen mode

11 May 2019, 04:53

Thanks for your useful explanation, that is really good to know that. And look like even there is other methods. Sounds good :)
MaxEnrik
Posts: 15
Joined: 07 May 2019, 19:36

Re: Hotkey should run ONLY in Fullscreen mode

08 Jun 2019, 13:45

When I hit 1 kbhotkey that part of scripts triggering even XYplorer doesn't exist. Is it possible to prevent it?
gregster
Posts: 9001
Joined: 30 Sep 2013, 06:48

Re: Hotkey should run ONLY in Fullscreen mode

08 Jun 2019, 13:55

Which code are you using?
MaxEnrik
Posts: 15
Joined: 07 May 2019, 19:36

Re: Hotkey should run ONLY in Fullscreen mode

08 Jun 2019, 14:43

This code.
Thanks for your reply.

Code: Select all

; @evilC
SetTitleMatchMode, 2
#IfWinActive  - Google Chrome ahk_exe chrome.exe
!`::
SetTray(!toggle_var)
Return

SetTray(state)
{
    global toggle_var
    toggle_var := state
    menu, tray, icon, % (toggle_var ? "enabled_01.ico" : "disabled_01.ico")
}

#If (toggle_var) && WinActive(" - Google Chrome")
1::
    WinGetPos, X, Y, Width, Height, A
    MouseMove, Width * 0.97, Height * 0.12
    Send, {Right}
Return
gregster
Posts: 9001
Joined: 30 Sep 2013, 06:48

Re: Hotkey should run ONLY in Fullscreen mode

08 Jun 2019, 19:47

With this code I can't reproduce anything strange. Toggling only works if Chrome.exe is active and the remap of 1 only works, if the toggle is on/true and a window with the partial title " - Google Chrome" is active (the Chrome browser again, in my case). 🤷‍♂️

Edit:
When I hit 1 kbhotkey that part of scripts triggering even XYplorer doesn't exist. Is it possible to prevent it?
Uh, why shouldn't it?
I don't see any reference to XYplorer in the code you posted today - so this wouldn't surprise me at all ;) This code behaves indifferent to XYplorer's existence...

❓

Code: Select all

#If (toggle_var) && WinActive(" - Google Chrome") && WinExist("ahk_exe XYplorer.exe")
(hotkey is active if "Toggle is on/true and Chrome window active and XYplorer window exists")
MaxEnrik
Posts: 15
Joined: 07 May 2019, 19:36

Re: Hotkey should run ONLY in Fullscreen mode

08 Jun 2019, 20:24

So sorry, I think I didn't explained very well / clearly (oh my poor English).
So I actually need if XYplorer.exe is active window run below script. Don't Trigger 2nd part of code when I hit 1.

Code: Select all

#If isActiveWindowFullScreen( "ahk_exe XYplorer.exe" )
1::PgDn
...

isActiveWindowFullScreen( winTitle )
{ ... }

And if #IfWinActive - Google Chrome ahk_exe chrome.exe run below script. Don't Trigger 1st part of code when I hit 1.

Code: Select all

#IfWinActive  - Google Chrome ahk_exe chrome.exe
!`::
...

SetTray(state)
{ ... }

#If (toggle_var) && WinActive(" - Google Chrome")
1::
...
Return

Finally I need to share screenshot of reports. There XYplorer.exe triggering when I hit 1 on Chrome and vise versa.
i.snag.gy/sU8Ava.jpg
( Image didn't loaded. )
gregster
Posts: 9001
Joined: 30 Sep 2013, 06:48

Re: Hotkey should run ONLY in Fullscreen mode

08 Jun 2019, 21:12

MaxEnrik wrote:
08 Jun 2019, 20:24
Finally I need to share screenshot of reports. There XYplorer.exe triggering when I hit 1 on Chrome and vise versa.
i.snag.gy/sU8Ava.jpg
So the actual problem is that you see these lines executed and not that PgDn is actually sent?!?

ifactive.jpg
ifactive.jpg (14.89 KiB) Viewed 6384 times
This just means that your script checks if XYplorer is active and in fullscreen - but as you can see, this check always returns false in these cases, so that this hotkey won't trigger... if your script wouldn't check, it wouldn't know. So, this is totally normal and necessary.

This is because #If works a bit different than the other #IfWin... directives - it will have to often check a certain expression (in this case, it will evaluate a function) while the #IfWin... directives syntactically don't even accept expressions. I briefly alluded to the different techniques that are used internally, here: https://www.autohotkey.com/boards/viewtopic.php?f=76&t=64433#p276467

Some background from the docs about the differences:
#IfWin...
https://www.autohotkey.com/docs/commands/_IfWinActive.htm#gen wrote:For performance reasons, #IfWin does not continuously monitor the activation or existence of the specified windows. Instead, it checks for a matching window only when you type a hotkey or hotstring. If a matching window is not present, your keystroke or mouse click is allowed to pass through to the active window unaltered.
versus #If:
https://www.autohotkey.com/docs/commands/_If.htm#General_Remarks wrote:When the key, mouse or joystick button combination which forms a hotkey is pressed, the #If expression is evaluated to determine if the hotkey should activate.

Note: Scripts should not assume that the expression is only evaluated when the key is pressed (see below).

The expression may also be evaluated whenever the program needs to know whether the hotkey is active. For example, the #If expression for a custom combination like a & b:: might be evaluated when the prefix key (a in this example) is pressed, to determine whether it should act as a custom modifier key.
I hope this clears some things up. The repeated evaluation of these functions is necessary. As long as it doesn't return true when it shouldn't, everything is fine!
vsub
Posts: 541
Joined: 29 Sep 2015, 03:39

Re: Hotkey should run ONLY in Fullscreen mode

09 Jun 2019, 00:28

Fullscreen windows are always at x0 y0 screen position,and when they are maximized,it is always x-8 y-8 so you can just use WinGetPoS

Hmm,looks like XYPlorer fullscreen preview don't follow the usual rules,the window position is always x-7 y-7 and at a size A_ScreenWidth+14 and A_ScreenHeight+14

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Lamron750, songdg and 267 guests