I have written an AHK script that sends a set of keyboard keys when the hotkey ^!e:: is pressed. This script needs a couple of seconds to complete, but sometimes I need to stop the execution before the script has finished doing its task. I know that this can be done by setting the ExitApp function to another hotkey, but this will close the script. So, if I need to trigger the script again, it will be necessary to open the .ahk file anew.
How could the script be stopped without closing it? I would also need that, when it is triggered again, it sends the keys from the beginning, and not from where it left off when I stopped it.
Edit: let's take as an example this code,
^!q::
SoundBeep, 208
SoundBeep, 155
SoundBeep, 208
SoundBeep, 196
SoundBeep, 208
return
What you can do to resolve the issue is to have two different script-files.
MainScript.ahk:
; Starts the secondary-file into its own process:
^!l::run SecondaryScript.ahk ; Ctrl+Alt+L
SecondaryScript.ahk:
#SingleInstance Force
SoundBeep, 208
SoundBeep, 155
SoundBeep, 208
SoundBeep, 196
SoundBeep, 208
ExitApp
^!q::ExitApp ; Ctrl+Alt+Q
After the SecondaryScript has finished its auto-executing of code, then it will issue ExitApp which will kill the process that the file is currently being executed inside of.
If you want to abort the execution, then you can use the hotkey that is defined under the Auto-Execute portion of the script.
to do that:
1.go to the system tray ()
2.right-click on the symbol.
3.click on suspend hotkeys
What happens when you click on pause script? Something horrible.
ex:if you remapped the
key, that useful key will be shutdown.
hope this helped.
I found a trick to relaunch current script without a separate launcher script:
Esc::
Reload
ExitApp
Reload, obviously, reloads the current script.
(NOTE: Command-line parameters aren't passed down to the new instance.)
And then ExitApp makes sure current instance dies right away and even if the reload fails.
The effect is similar to the solution by Hampus, but doesn't require hardcoding or otherwise specifying any file names within the scripts, and is much more convenient for scripts with multiple long-running hotkeys, which would require a separate script file each with the launcher approach.
Related
when I‘m making a script, I can‘t make two functions happen.
All details first: I‘m using Auto Hot Key‘s current version.
code:
<+<!`::Sound beep 627,92
Sleep, 100
Sound beep 837,92
return
only Sound beep at pitch 627 is happening, not the other beep.
I try other‘s suggestions by copy pasting them, they work, but my handwritten script doesn‘t work.
did I do any mistake? thanks.
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:
<+<!`::
SoundBeep 627,92
Sleep, 100
SoundBeep 837,92
return
For more details, read the Hotkey page.
I have a script that assign F1 for a global task:
f1::Run D:\Download
A program needs to use that key, so I put this:
#IfWinActive, ahk_exe inkscape.exe
F1::send {f1}
return
However when I press it, this error hits:
If yes, nothing happens. If no, the script exits.
Do you know what happens?
The problem is that your hotkey keeps triggering itself over and over a again in a loop. The $ modifier will fix it. That way the hotkey wont get triggered when the source of the key press is a Send command.
However, you don't actually need this at all.
You should use the #IfWinNotActive directive.
#IfWinNotActive, ahk_exe inkscape.exe
F1::Run, D:\Download
#IfWinNotActive
Alternatively, you could just not create a context sensitive hotkey, and use the ~ modifier. That way the hotkey will always retain its normal functionality when you press it.
~F1::Run, D:\Download
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!
Say I have an autohotkey script C:\path\to\my\script running. Is there a way to define a hotkey that re-starts it?
In order to prevent duplicate instances, I normally do not re-launch a script but use the build-in function Reload. I launch this with Ctrl+Win+Alt+R and use Ctrl+Win+Alt+E to edit the main AHK script.
^#!r::Reload
Actually, my script looks like this:
^#!r::
Send, ^s ; To save a changed script
Sleep, 300 ; give it time to save the script
Reload
Return
^!#e::Edit
As a matter of fact, all the way at the top of my script I have this to give me a visual and audio indication that the script was restarted:
#SingleInstance Force
#installKeybdHook
#Persistent
Menu, Tray, Icon , Shell32.dll, 25, 1
TrayTip, AutoHotKey, Started, 1
SoundBeep, 300, 150
Return
Make a hotkey that runs a script, which in this case is the same script and then exit.
somehotkey::
Run, C:\path\to\my\script.ahk
ExitApp
return
I found this to be the safest option of them all, because it takes care that the correct script is reloaded when you have multiple scripts running simultaneously, which was a recurring issue for me. The combination of the following also ensures that only one instance of a script will ever run at a time. The ScriptFullPath variable includes the name of the script.
#SingleInstance Force ;put this at the top of the script
^r::run, %A_ScriptFullPath%
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