AHK: Errors in Shell messages on windows creations and destructions - autohotkey

I am trying to catch the events when the windows on my system are created and destroyed.
However, I am facing some strange results:
Everythink works fine with some windows/applications, but with certain windows/applications the events I receive seem all mixed up.
For exemple, everything seems fine with Notepad.exe, but not with Calc.exe.
With Calc.exe, when I open it I get 2 x HSHELL_WINDOWCREATED and 1 x HSHELL_WINDOWDESTROYED.
When I close it I get 4 x HSHELL_WINDOWDESTROYED.
I am lost!
Here is in detail the result I get from the "ShellHook Messages" script (https://www.autohotkey.com/board/topic/32628-tool-shellhook-messages/):
(I just added the "Title" column. The titles are in French.)
I get the same stange behaviours when I track the events with :
the script "WinHook" (https://www.autohotkey.com/boards/viewtopic.php?f=6&t=59149)
or with a more direct approach (https://www.autohotkey.com/board/topic/80644-how-to-hook-on-to-shell-to-receive-its-messages/)
This is the script I use to open my windows:
#SingleInstance Force
#NoEnv
^j::
run notepad.exe
return
^k::
run calc.exe
return
I have Windows 10 Pro 21H2 19044.1645.
Any help appreciated!
Thank you!

Related

Win + mouse wheel keeps showing/hiding start menu

I wanted to add volume control to my mouse with the following scripts
#WheelUp::
Send {Volume_Up}
Return
#WheelDown::
Send {Volume_Down}
Return
However, when I use it, the start menu keeps appearing/disappearing. Is there anyway to consistently suppress the windows key with this script?
I tried to recreate your problem, but the code works on my machine. If I hold the Windows key down and use the mousewheel it adjusts the volume (Windows 10). You might be having problems with a competing command that you are running at the same time.
Also, the line breaks and return statements are unnecessary. It also works as:
#WheelUp::Send {Volume_Up}
#WheelDown::Send {Volume_Down}
Function SoundSet, apparently doesn't cause this issue:
#WheelUp::
SoundSet, +2
Return
#WheelDown::
SoundSet, -2
Return

Autohotkey daily macro doesn't work well

I created a macro in autohotkey that is able to copy at 7:40 a.m. the last file created in a shared disk into a dropbox folder. When I launch the macro to try it, setting the "time to meet" 2 minutes later for example, it works perfectly. The problem is that the day after the macro doesn't start. Could you help me please?
Thanks
Marco
SetTimer, Chronos, 59900
Return
Chronos:
FormatTime, TimeToMeet,,HHmm
If TimeToMeet = 740 ; If you wanted the script to start at 7 am put change 1006 to 700
{
run O:\research\
winactivate, research
sleep 1000
MouseClick, left, 289, 586
send {PgDn 6}
clipboard =
Send ^c
clipwait
sleep, 1000
FileCopy, %clipboard%,C:\Dropbox\
sleep 2000
winclose research
return
}
Return
Unattended user interface automation like this may not be the most reliable aproach.
I would recommend using the Windows Task Scheduler to handle launching the process. I think this might be safer than having the script running 24 / 7 waiting to go. Even more importantly, it looks like you are doing very basic file manipulation by automating the UI. This type of work may be better acomplished with a Windows batch file or Autohotkey's functions for files. Note that batch files are less fussy about screensavers and being logged in. I love AutoHotkey, but that seems to be a weak spot. Check out the documentation for each of the functions that start with the word file. I'd be surprised if you couldn't hook some of those up to do what you need. Since you seem to be looking for a file, check this one out:
http://www.autohotkey.com/docs/commands/LoopFile.htm
I think this thread may be of help as it finds the most recent file in a folder:
http://www.autohotkey.com/board/topic/57475-open-most-recent-file-date-created-in-a-folder/
Good luck!

How to simulate key press on a background window

I need to force Outlook which is in the background, to check for new emails every 2 seconds. So I wrote a following script but it doesn't do it. What's wrong? I don't want the script to disturb what I do and give the focus to the Outlook window. The "ahk_class rctrl_renwnd32" is correct, I checked it with "WinActivate, ahk_class rctrl_renwnd32" and it worked.
Loop
{
ControlSend,, {F9}, ahk_class rctrl_renwnd32
Sleep 2000 ; Wait 2 seconds
}
There is no error in your code. The problem might be in the receiving application. When I test this in Notepad, an F5 (Print time & date) is executed when the Notepad window is open somewhere on my second screen. As soon as I minimize Notepad, it will no longer execute F5, but it still accepts a string like A{Enter}B{Enter}C{Enter} when minimized.
!q::
ControlSend,, {F5}A{Enter}B{Enter}C{Enter}, ahk_class Notepad
Return
Solution, Try if this works when you keep the window on screen somewhere (no need to have the focus).
I have used Outlook in the past and remember that F9 took some time to execute. Running this every 2 seconds looks like overkill.
If getting your e-mail in time is THAT important that you are willing to "kill" the mail server with refresh requests, I would discuss a solution with your IT support.

Autohotkey - Repetitive hotkey [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
how to send a letter as itself when it is used as hotkey in autohotkey
When I try to run the script:
v::Send tv
it starts typing t repetitively.
And while playing with autohotkey I probably did something wrong and now it quits saying
Error at line 1.
Line Text: ; v::Send tv
Error : Invalid hotkey.
The program will exit.
(I made it a comment using ; later, the problem persisted before that.)
Can someone tell me what I am doing wrong?
The hotkey is triggering itself when it sends a v. Try using $:
$v::Send tv
I'm not sure what caused it to say "Invalid hotkey".
Alternatively you can use:
:?*:v::tv ; * = no need to press enter, ? = will execute vene if letters have been typed before.
To avoid problems of not being able to use the letter v anywhere else, I normally use v\ this combination because it is easy and unique:
:?*:v\::tv
And if you want to limit the behaviour of the v key to just one application, I would use the AutoHotKey spy to get the application specific ID or name and place the hotkey inside an #ifwinactive. You will need to put SetTitleMatchMode, 2 at the top of your script. This sets the string mathing behaviour for functions like #ifwinactive. In this example v\ only works in Google Chrome.
SetTitleMatchMode, 2
#ifWinActive, Chrome ; limits the use of the following hotkey(s) to Chrome only.
:?*:v\::tv
#ifWinActive

How can I make AutoHotkeys's functions stop working as soon as the .exe is closed?

I'm testing AutoHotkeys as a way to block user's usage of Ctrl, Alt and Windows Key while an application is running. To do this, I compiled the code:
LAlt::return
RAlt::return
LControl::return
RControl::return
RWin::Return
LWin::Return
into an .exe using the compiler that comes with AutoHotkeys.
My problem is that normally when I close the .exe file (either by code using TerminateProcess(,) or manually) the keys are not released immediately. The Windows Key, for example, may take something like 10 seconds to be finely "unlocked" and become able to be used again, and for me this is unacceptable.
So I got two questions:
Is there a way to fix this problem? How can I make the keys to be released as soon as the .exe is closed?
Would there be any improvement if I tryed to get the same functionality by code? Or if I create the hooks by myself I would get the same problem I'm having with AutoHotkeys?
Thanks,
Momergil
AutoHotkey has a built-in command ExitApp for terminating your scripts.
This example makes Esc your termination hotkey:
Esc::ExitApp
It seems like the delay you are experiencing might be related to how long it's taking the process to close.
You could try making the hotkeys conditional with the #If command*
(i.e. they are only blocked when Flag = 1).
Then you can have the script quickly change the context just before ExitApp by using OnExit. The OnExit subroutine is called when the script exits by any means (except when it is killed by something like "End Task"). You can call a subroutine with a hotkey by using the GoSub command.
Flag := 1
OnExit, myExit
Esc::GoSub, myExit
#If Flag
LAlt::return
LCtrl::return
x::return
#If
myExit:
Flag := 0
Exitapp
* The #If command requires Autohotkey_L.
The other option that will be more verbose, but work for AHK basic, is the hotkey command.
Another option is to have AutoHotkey run the target application, and upon application exit, AutoHotkey exits as well. Here's an example with Notepad. When the user closes Notepad, the script gracefully exits.
RunWait, Notepad.exe
ExitApp ; Run after Notepad.exe closes
LAlt::return
RAlt::return
LControl::return
RControl::return
RWin::Return
LWin::Return
I would use winactive to disable these keys. In this example the modyfier keys are disabled for "Evernote". As soon as you switch to another program the keys are restored and when you switch back to Evernote the modifier keys are disabled again.
SetTitleMatchMode, 2 ; Find the string Evernote anywhere in the windows title
#ifWinActive Evernote
LAlt::return
RAlt::return
LControl::return
RControl::return
RWin::Return
LWin::Return
#ifWinActive