AutoHotKey: Run code on Window Event (Close) - events

I work with a local application which when ESCAPE is pressed, the current window is closed. I wanted to avoid that so I made a script with AutoHotKey that detects the name of the window within the app and, if I press ESCAPE, the window doesn't close (Unless I press the X).
#IfWinActive ahk_class name_of_app_class_here
Escape::return
Now I would like to apply a code for when that window is closed (by pressing the X) but I cuoldn't find any solution for that in the forum or in the help.
Example:
#IfWinActive ahk_class name_of_app_class_here
OnWindowClose
`Do_something`
Hope it is well explained.
Thanks!

The code below should do what you want to achieve:
;tested on Notepad (Windows 7)
;note: may not work correctly if aero mode is on
;note: if you click a Notepad window that is *not* the active window, it will be closed
;note: some programs don't return the standard NCHITTEST value for a close button,
;a workaround is to compare the cursor position against the window coordinates
#IfWinActive, ahk_class Notepad
LButton::
WinGet, hWnd, ID, A
WinGetClass, vWinClass, ahk_id %hWnd%
if vWinClass not in Notepad
Return
CoordMode, Mouse, Screen
MouseGetPos, vPosX, vPosY, hWnd2
if (hWnd = hWnd2)
{
SendMessage, 0x84, 0, vPosX|(vPosY<<16), , ahk_id %hWnd% ;WM_NCHITTEST
vNCHITTEST := ErrorLevel ;(8 min, 9 max, 20 close)
if (vNCHITTEST = 20)
{
MsgBox can't close me! ;put your code here
Return
}
}
SendInput {LButton Down}
KeyWait, LButton
SendInput {LButton Up}
Return
#IfWinActive
Note:
The code posted in the link below, achieves something related, catching close buttons for all windows, not just for one program, whether they are active or inactive.
Is it possible to catch the close button and minimize the window instead? AutoHotKey

Related

How to focus on browser window with AHK hotkeys?

Goal's to refresh and focus onto an existing Mozilla Firefox page (away from any current focus); achieved former, but not latter:
^!s::ControlSend,,{F5},ahk_class MozillaWindowClass
WinActivate, ahk_class MozillaWindowClass
return
Any clues?
To have more than one command executed by a hotkey, put the first line beneath the hotkey definition and make the last line a return:
^!s::
ControlSend,,{F5},ahk_class MozillaWindowClass
WinActivate, ahk_class MozillaWindowClass
return
https://autohotkey.com/docs/Hotkeys.htm#Intro

Autohotkey: Hotkeys on Firefox not doing what they're supposed to

I just want W to correspond to the Up Arrow key and S to correspond to the Down Arrow key, and work just like they do, and for A/D to move the focus to the last/next tab.
When i hold S, the page skips down erratically and then Firefox opens the "Save as" window multiple times.
When i hold W, the page skips up erratically and then multiple tabs are closed.
D does what it's supposed to, and A straight up doesn't work.
#IfWinActive ahk_exe firefox.exe
w::
Send {Up} ; Move page up.
s::
send {down} ; Move page down.
a::
send, ^{pgup} ; Go to tab on the left.
d::
send, ^{pgdn} ; Go to tab on the right.
#IfWinActive
Return
What exactly is happening? It should work normally but it isn't.
#IfWinActive ahk_exe firefox.exe
w:: Send {Up} ; Move page up.
s:: send {down} ; Move page down.
#IfWinActive ; turn off context sensitivity
The above examples are known as single-line hotkeys because each consists of only one command.
To have more than one command executed by a hotkey, put the first line beneath the hotkey definition and make the last line a return. For example:
#n::
Run http://www.google.com
Run Notepad.exe
return
https://autohotkey.com/docs/Hotkeys.htm#Intro

Is there anyway to tell autohotkey that it has to work only on the desktop, and nowhere else?

I want Autohotkey to be able to distinguish when I am on the desktop, and to run a specific set of shortcuts on the desktop only, and pause those shortcuts when I am not on the Desktop. Is there anyway to do so? I work on a Windows 10 machine.
I have attached a simple drawing if my language is not very clear.
Some ideas:
A loop that checks the active window.
A context-sensitive hotkey that works only when Desktop is active.
Loop
{
WinGetClass, vWinClass, A
if vWinClass in Progman,WorkerW
{
;do this
}
else
{
;do that
}
Sleep 5000
}
;==================================================
GroupAdd, vGroupDesktop, ahk_class Progman ;desktop
GroupAdd, vGroupDesktop, ahk_class WorkerW ;desktop
#IfWinActive, ahk_group vGroupDesktop
^q::
;do this
Return
#IfWinActive
Note:
Two separate scripts running at the same time might be an idea to consider.

ahk how to keep visible a window though not active

The below script works but minimizes a screen inactive, but I would like to keep the inactive window visible. what should I do?
F9::
IfWinExist ahk_class Forge10MDIClass
WinMove ahk_class Forge10MDIClass
WinActivate ahk_class Forge10MDIClass
Send {space} ;; pauses audio file (toggles)
Send !{Esc} ;; Alt + Esc in Windows 8; but it is minimizing and I dont want that
return
You could do this in 2 different ways:
First: Store the active window and switch back after toggling the audio file
F9::
WinGet, active_id, ID, A
IfWinExist ahk_class Forge10MDIClass
{
WinActivate ahk_class Forge10MDIClass
Send {space} ;; pauses audio file (toggles)
WinActivate, ahk_id %active_id%
}
Return
Second: Use ControlSend to send Space directly to the window without having to activate it.
F9::ControlSend, ahk_parent, {Space}, ahk_class Forge10MDIClass
Note: I do not have this software, so I cannot confirm that ControlSend works properly with it.
Hope this helps.

script for focusing on a highlighted text

What is wrong with my script?
Context: Inbox folder of Outlook. I want to click (focus) just only one email (the one with the gray color), but my script ALSO "colors" all emails the are above it. I'm not sure what is the matter.
#IfWinActive ahk_class rctrl_renwnd32
+F7::
PixelSearch, Px, Py, 14,98,754,962, 0xE1E1E1, 3, Fast
if ErrorLevel
MsgBox, That color was not found in the specified region.
else
Click, %Px%, %Py%
return
#IfWinActive
It seems that you are oh, say, scrolling or moving the cursor around in the preview panel, and now you want to jump the cursor to that same email's entry in the mailbox listing.
If this be the case, then here is a possible solution for you:
Okay, so we know that by default, whatever message you are viewing in the preview pane should be the highlighted entry in the mailbox list. That means all we have to do is to set focus to the mailbox list pane - when that pane receives focus, then the highlighted entry should be able to receive your immediate keyboard commands since it is already selected.
Therefore, all you need to do with your ahk script is create a hotkey that sets focus to the mailbox pane. Use your window spy to spy the class of the mailbox pane and just set focus to it. You shouldn't have to worry about colours at all.
Maybe something like this for Office 2010:
+F7::
ControlFocus, SUPERGRID2, ahk_class rctrl_renwnd32
return
or, for Office 2013:
+f7::
ControlFocus, OutlookGrid1, ahk_class rctrl_renwnd32
if ErrorLevel
MsgBox, You don't seem to be in context.
return
Or, instead of hacking the window, you could just push ctrl+1 - Outlook has a built-in keyboard command to set focus to the mailbox list.

Resources