How to close/minimize/maximize or send keys to window or popup with Autohotkey? - window

How to use Autohotkey to automatically close, minimize, maximize or send keys to a window as soon as it pops up? I can detect a dialog and close it with this:
WinWaitActive, TITLE
WinClose, TITLE
But this doesn't work if the window isn't open on script execution.

This is a very common task AHK is used for.
First you need the title of the window you want to address. Read How to get the title of a window with AHK?.
The code
For the basic functionalitiy of closing a window we need Loop, WinWaitActive and WinClose.
Example for a Firefox window with Stack Overflow open.
Loop {
WinWaitActive, Stack Overflow - Mozilla Firefox
WinClose,
}
Explanation
The Loop repeats the process to close the window multiple times. WinWaitActive waits until the the window gets activated (pops up) and WinClose closes it.
Hint: If you don't specify a specifiy window title like with WindowClose the last found window, which is the one from WinWaitActive is used.
minimize/maximize
Instead of WinClose use WinMaximize or WinMinimize to perform the corresponding action.
Sending Keys
If you want to send specific keys (e.g. Enter) to the window use Send
Loop {
WinWaitActive, Stack Overflow - Mozilla Firefox
send {Enter}
}
Additions
If the basic version does not work or you want to create an more advanced script, here are some possible modifications.
More Force
If WinClose does not work try WinKill or Send, !{F4} to use more force.
As Admin
Admin rights might be necessary to close the window, use this code snippet on top of your script to make sure it runs with full access.
If not A_IsAdmin ;force the script to run as admin
{
Run *RunAs "%A_ScriptFullPath%"
ExitApp
}
Other matching methods
On default the window title has to be an exact match. To change this behavior and allow partial or start with matches use SetTitleMatchMode on top of your script, e.g. SetTitlematchMode, 2 for partial match.
Instead of title, the window class (ahk_class) or .exe (ahk_exe) from Window Spy can be used.
WinWaitActive, ahk_class MozillaWindowClass
or
WinWaitActive, ahk_exe firefox.exe
Select the one which suits your needs carefully to only react to the correct window.

Related

AHK Cannot ControlClick on hidden elements in nested child window

I'm trying to automate a many clicking process, just to narrow it to the user input.
I encountered problems in controlClicking interface elements, which seems not to be standard Windows GUI elements.
When pointing them with WinSpy they don't appear as separate buttons, but I can point the whole child Window which is drawn in the main program window.
As on pic1, I pointed the whole window and I can find each tab/button by it's text inside and on pic2 I can inspect the ClassNN of that element and it's ID.
As far as clicking other buttons in the main menu bar of the program works, a simple:
ControlClick, ClaTab_01000000H26, WindowName
doesn't work. I think during the day, and many possibilities I tried, I could ControlClick the above button by pointing it with its ID, but that ID changes every instance. I could confirm that tomorrow if it works by ID.
Of course I tried SetControlDelay -1 and ,NN option. But don't take that for granted, I can try any of your suggestions tomorrow.
Both tabs marked with purple color, are to find in the Windows->SiblingWindows tab. I really don't want using x,yCoords (that actually work), but I need the script to be as reliable as possible.
So my questions are:
Am I missing something or you have any suggestions how to click that elements?
Is it correct, that no matter how deep the child windows get (one has buttons to open another on top of it), all the time the WinName stays the same pointing to the main program ***.exe?
Could you provide an example from the web or yours, to find an element's ID by providing the text attached to the button (pic1-red line and also pic2 in "text")?
I also cannot maximize the child window. Double clicking it works, but I can't find the appropriate ClassNN of the window to call.
Could you provide an example, how to use the Messages tab? I assume, if I find the button as on the pictures, I could send a message with controlClick and see if there's a reaction?
1.Ugh. I found the solution, which is awesome, but a little frustrating that with a bit of luck I tried another aproach that's not that logical for a newbie like me:
instead:
ControlClick, ClaTab_01000000H6, ahk_class ClaWin01000000H_2,,,, NA
it's just
ControlFocus, ClaTab_01000000H6, ahk_class ClaWin01000000H_2,,,, NA
2._Yep. One child window creates another and another and another, but winTitle stays the same. In my case:
ahk_class ClaWin01000000H_2
3._Code below returns the handle/ID of the element you specify. Change ClaTab and ClaWin to your chouice.
ControlGet, OutputVar, hwnd,, ClaTab_01000000H1, ahk_class ClaWin01000000H_2
MsgBox, %OutputVar%`
Probably to be continued.
I highly recomend to both use
WinSpy https://www.autohotkey.com/boards/viewtopic.php?t=28220
SimpleSpy https://www.the-automator.com/downloads/simple-spy/
First one has lots of useful information and the window tab provides information of hidden buttons/windows. Second one in a more clear way indicates the parent window and its class.

AutoHotKey - "Continue" and "Cancel" Buttons

I am a newbie with AutoHotKey, and to this point I consider myself fortunate to have created a script to automate about 90% of the data entry in a window. I'm now trying to kick it up a notch and add a warning/caution GUI to my data entry script. I want to pause the running script and open a GUI that will alert me to look at what has already been entered, with one button to allow things to proceed if a particular entry looks OK, and another to dump out of both the GUI and the remainder of the script if the entry is incorrect.
Here's some code I came up with, mixed with some pseudocode to help show what I want to do.
(Previous data entry script executes to this point)
Stop the script
Gui, New
Gui, Add, Text, 'n Check Authorization Number to be sure it is A1234 (FY 15). ; Wraps text
Gui, Add, Button, Default, Continue
Gui, Add, Button, Quit
Gui, Show, IMPORTANT!
If Continue button is clicked
----Continue with script
If Abort button is clicked
----Close the GUI
----Exit the entire script
(Resume where I left off with rest of the data entry script)
I've read the AHK Help file and am stumped about how to make these buttons work, as well as how to properly return from the GUI back to the script if I hit continue, or quit the whole thing if I spot a problem. I can tweak things like the size of the GUI and the button placement; what's most important is getting the GUI code right. Can someone help with the code? (The last programming class I took was in 2003, so I have forgotten a whole lot!)
In your case, a GUI would be overkill. AHK (and many other languages at that) provide standard dialogs for simple user interaction called message boxes.
Message boxes in AHK can be displayed in two ways:
MsgBox, This is a simple message. Please click "OK".
MsgBox, 4, Attention, Here`, you can choose between "Yes" and "No".
In the second case, we declared an option which controls the buttons that are displayed. You can use IfMsgBox in order to detect what the user has clicked:
IfMsgBox, Yes
MsgBox, 4, Really?, Did you really mean "Yes"?
Putting these pieces together, we can ask the user to decide if they want to continue, without the need to create a GUI, in a few lines:
; Option "1" is "OK/Cancel"
MsgBox, 1, IMPORTANT!, Check Authorization Number to be sure it is A1234 (FY 15).
IfMsgBox, Cancel
{
ExitApp
}
; do stuff
This code addresses each of your other problems, too:
A message box halts the current thread until the user dismisses the dialog or it times out (if you specifically declared a timeout).
To completely stop the execution of our script, we use ExitApp.

Execute a MEL command after a window has opened

I'm writing a MEL script which involves opening the grease pencil UI toolbar. I want to remove the close button on that toolbar. I tried doing
GreasePencilTool;
window -edit -tbm 0 greasePencilFloatingWindow;
but get Error: line 2: window: Object 'greasePencilFloatingWindow' not found.
Further tests reveal that running
GreasePencilTool;
window -q -exists greasePencilFloatingWindow;
will return a result of 0.
Running GreasePencilTool; and then window -edit -tbm 0 greasePencilFloatingWindow; at separate times works as expected, as does running window -edit -tbm 0 greasePencilFloatingWindow; when the toolbar is already open.
However, I need to be able to remove the close button immediately when the toolbar opens.
The closest thing I can think of that illustrates what I want to do are Javascript callback functions, where another function can be executed once the current function is finished... but is there a way to do something like that in MEL?
I've also tried using the evalDeferred command without success.
The grease pencil tool is launched asynchronously so the window will not be present for some unknown length of time. This means the best you could do is trigger a function which would check periodically and do it the next time you find the correctly named window; you could attach this to an idle time script job.
It's ugly. But it is probably the only way since there's no event that will notify when thje window arrives. If you do that, make the script job suicide after it fires so it's not sitting there on every idle check till the end of time.

Pressing Alt hangs the application

I'm programming a Windows application that doesn't have a menu. Every time I press Alt, it receives the WM_ENTERMENULOOP event and hangs until I press a key.
I've tried other applications without menu (like the MS .chm file viewer) and they exhibit the same behavior.
There is no difference between forwarding the event to DefWindowProc or processing it.
Is there a way to stop Windows from entering the menu loop if there is no menu? Alternatively, is there a way to exit it manually as soon as the event is received?
Process WM_SYSKEYDOWN and WM_SYSKEYUP manually (dont' pass them to DefWindowProc) if you want to disable entering menu loop.
Also, you may want to process WM_SYSCHAR and return TRUE for this message to avoiding beeps for keystrokes like Alt+SomeKey
Scalable code after parsing window message and return correct result with NO call DefWindowProc.
case WM_SYSKEYDOWN:
case WM_SYSKEYUP:
case WM_SYSCHAR:
return (LRESULT)1;
https://learn.microsoft.com/en-us/windows/win32/inputdev/wm-syskeydown
https://learn.microsoft.com/en-us/windows/win32/inputdev/wm-syskeyup
https://learn.microsoft.com/en-us/windows/win32/menurc/wm-syschar
Return value
An application should return zero if it processes this message.

capturing right-click+left-click with autohotkey; unexpected behaviour

I want to capture the key event "right mouse button pressed, then left mouse button pressed". No problem in autohotkey. However I am having trouble with still allowing the right-mouse key to work alone.
1) this works:
RButton & LButton::
Send X
Return
works as expected:
If I press right mouse button, then left mouse button, "X" is sent to the active window
right-click event is captured by Authotkey: no context menu appears when I press the right mouse button alone. This is the intended outcome
2) this works
~RButton & LButton::
Send Y
Return
works as expected:
If I press right mouse button, then left mouse button, "Y" is sent to the active window
right-click event is not captured by Authotkey: context menu does appear when I press the right mouse button alone or together with the left button. This is the intended outcome
3) Now I want to do different things depending on the active window.
this does not work (careful: this will disable righ-click in every application)
#If WinActive("ahk_class MozillaWindowClass")
RButton & LButton::
Send X
Return
#If !WinActive("ahk_class MozillaWindowClass")
~RButton & LButton::
Send Y
Return
does not work as expected:
in Firefox left-right sends X, in other applications left-right sends Y
however, right-click is disabled in every application
What am I doing wrong here?
edit:
the goal is this: I want a global hotkey on Right+left-click with RButton & LButton . In specific applications that I have tested for compatibility, I want right+left click to suppress sending right-click, and then send right-click manually using autohotkey. However, since some applications might have trouble processing mouseevents sent by autohotkey, in all untested applications I want to use ~RButton & LButton with the ~ to pass throught right-click events
Here's one that supports right click dragging!
Hotkey, LButton, off
#IfWinActive ahk_class MozillaWindowClass
RButton & LButton::
Send X
Return
RButton::return
#IfWinNotActive ahk_class MozillaWindowClass
~$RButton::
Hotkey, LButton, on
while GetKeyState("RButton", "P") {
continue
}
Hotkey, LButton, off
Return
LButton::Send Y
Return
It handles RButton manually. When RButton is pressed, it enables the LButton hotkey and waits for RButton to be released before deactivating it. The RButton hotkey uses ~, which passes the click through normally.
LButton is disabled at the start by the line at the top.
Another way would have been to send {RButton Down} at the start of the hotkey and {RButton Up} at the end.
In response to your edit, the only programs that reject Autohotkey's sent events should be those that rely on low level hooks... The real trouble with the method down at the bottom is it only sends a single click, not processing holding the button. This method, and sending down and up separately, should both do that properly.
The bug with active window described at the bottom of this answer still exists, but that's a problem with the #IfWin[Not]Active.
Old stuff
See the documentation on the ampersand (emphasis mine):
You can define a custom combination of two keys (except joystick buttons) by using " & " between them. In the below example, you would hold down Numpad0 then press the second key to trigger the hotkey:
Numpad0 & Numpad1::MsgBox You pressed Numpad1 while holding down Numpad0.
Numpad0 & Numpad2::Run Notepad
In the above example, Numpad0 becomes a prefix key; but this also causes Numpad0 to lose its original/native function when it is pressed by itself. To avoid this, a script may configure Numpad0 to perform a new action such as one of the following:
Numpad0::WinMaximize A ; Maximize the active/foreground window.
Numpad0::Send {Numpad0} ; Make the release of Numpad0 produce a Numpad0 keystroke. See comment below.
The presence of one of the above hotkeys causes the release of Numpad0 to perform the indicated action, but only if you did not press any other keys while Numpad0 was being held down.
So, following that example:
#If WinActive("ahk_class MozillaWindowClass")
RButton & LButton::
Send X
Return
RButton::return
#If !WinActive("ahk_class MozillaWindowClass")
RButton & LButton::
Send Y
Return
RButton::Send {RButton}
Note RButton requires a variant that does nothing in WinActive, at least with my testing (see below): RButton::return
Since I'm using Autohotkey standard, not Autohotkey_L, I don't have #If and the above was untested. The following I did test, and it works.
#IfWinActive ahk_class MozillaWindowClass
RButton & LButton::
Send X
Return
RButton::return
#IfWinNotActive ahk_class MozillaWindowClass
RButton & LButton::
Send Y
Return
RButton::Send {RButton}
An interesting bug I've noticed is the second (NotActive) variant applies occasionally to Firefox:
Another window is active
RButton down is sent
Firefox is not active, so the second variant is processed
<delay> (RButton is held down, though the delay could be imperceptible, in the order of milliseconds, to infinite)
Firefox becomes active
<delay> (still held down)
RButton up is sent, which sends RButton as per the documentation. Because Firefox became active in the delay between the active window check and when RButton is sent, RButton is sent to Firefox.
This happens when both Firefox and another window are visible, and the other window is the active one at the time of the click.
I've tried to fix this bug by adding an extra IfWinNotActive check within the RButton hotkey, but it did not seem to work.

Resources