Suspend shortcut for Autohotkey - autohotkey

I would like to set the "Scroll Lock key" to be a toggle shortcut to suspend and resume
my Autohotkey script. I mean to get all the lines in a currently running ahk script suspended and resumed by a toggle shortcut.
Is this possible?

Toggle suspend:
f1::suspend
Close the script:
f2::exitapp
Reload the script:
f3::reload
Edit the script:
f4::edit
I find that including this speeds up editing / testing:
back = open the script in notepad
forward = save and exit notepad
refresh = reload
browser_back::edit
browser_forward::
send ^{s}
send !{f4}
return
browser_refresh::reload
add reload at bottom of browser_forward hotkey, i say having it seperate

The short, precise answer is that yes, it's possible.
The long, complicated answer is to add the following line to your script
scrolllock:: Pause

Related

Automatically reload AutoHotkey script when modified

When testing AutoHotkey scripts, I sometimes forget to reload my scripts after making changes. This leads to me accidentally testing old, outdated versions of my scripts.
Instead of manually reloading the script, I would like to have scripts automatically reload if they have been modified.
How can I make AutoHotkey reload the current script any time a .ahk file is modified?
Somewhere near start of the script, in the auto-execute section
#SingleInstance force
FileGetTime ScriptStartModTime, %A_ScriptFullPath%
SetTimer CheckScriptUpdate, 100, 0x7FFFFFFF ; 100 ms, highest priority
Anywhere in the script (usually somewhere at the bottom):
CheckScriptUpdate() {
global ScriptStartModTime
FileGetTime curModTime, %A_ScriptFullPath%
If (curModTime == ScriptStartModTime)
return
SetTimer CheckScriptUpdate, Off
Loop
{
reload
Sleep 300 ; ms
MsgBox 0x2, %A_ScriptName%, Reload failed. ; 0x2 = Abort/Retry/Ignore
IfMsgBox Abort
ExitApp
IfMsgBox Ignore
break
} ; loops reload on "Retry"
}
This is how I've done it:
#If WinActive("AHK.ahk - Notepad") or WinActive("*AHK.ahk - Notepad")
~^s::
Reload
Return
#If
Checks if the current window is the script that I want autoreloaded whenever I hit Ctrl-S.
The ~ means the default action of Ctrl-S (saving the file) is preserved, and then we simply reload it.
I'm still new at AHK but here's what I've come with.
If you're using Notepad++ to edit AHKS this will run any ahk that's open and currently in focus in Notepad++ on saving with Ctrl-S and won't effect any other file types.
This script only has to be in one running ahk to work on all ahks being modified in Notepad++.
This can be used on multiple text editor programs too like win Notepad just get rid of the ++ associated with Notepad++
~^s:: ; Saves and Runs ANY AHK open in Notepad++
Sleep 150
WinGetTitle, Title, A
Needle := "ahk - Notepad++"
IfInString, Title, %Needle%
{
StringReplace, xxx, Title,- Notepad++, , All
run %xxx%
return
}
else
return

How can I wait for only one of three events (mouse input, keyboard input, or WinWaitClose) in AutoHotkey?

In my scenario, either a window will close automatically, or the user will provide an input via mouse or keyboard.
If the window closes automatically, I want to open a PDF. This is easy:
WinWaitClose, ahk_id %cmdHwnd%
Run, "C:\Program Files\SumatraPDF\SumatraPDF.exe" "%path%\cv.pdf"
But if the window doesn't close automatically (i.e. the PDF failed to compile), then the user's going to close that window manually, e.g. hit Enter or click the close button. In this case, I do not want the above WinWaitClose to trigger!
So another way of putting my question is, how do I "cancel" a WinWaitClose listener upon mouse or keyboard input?
I suppose you could rely on the Seconds timer in WinWaitClose, and if that fails, you can rely on the ErrorLevel which will be set to 1.
; Set WinWaitTimer to wait 2 seconds before timing out.
WinWaitClose, ahk_id %cmdHwnd%,, 2
if (ErrorLevel) {
; Do something.
}
else
{
Run, "C:\Program Files\SumatraPDF\SumatraPDF.exe" "%path%\cv.pdf"
}

Hotkey to restart autohotkey script?

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%

Mouse Move application add command line arguments to auto start when windows starts

I have the application
http://movemouse.codeplex.com/
and I want to launch the app at startup (which I can do) but I also want to automatically hit the "Start" button so it turns on as soon as windows starts.
I need to have command line options to do this.
Anyone know of a command line option to do this? And/or a command line list of options available for this program?
Thank You
From looking at the settings, there are three options, that have to be set:
Automatically start Move Mouse on LAUNCH (same as press start when Move Mouse is opened)
Automatically launch Move Mouse at Windows LogOn
Minimize on start
I assume that the options work as presented.
Otherwise, as said before writing your own MouseMover in AutoHotKey is easy. and compiling it to an exe is easy too.
Here is some AutoHotKey code:
#Persistent
#SingleInstance force
SetTimer MoveMyMouse, 60000 ; 1000 ms = 1 sec.
Return
MoveMyMouse:
MouseMove, 1, 0, 1, R ;Move the mouse one pixel to the right
MouseMove, -1, 0, 1, R ;Move the mouse back one pixel
Return
^!#Pause::ExitApp
Load the code in AutoHotKey and test it, once your'e happy compile to your own MouseMove.exe
What is does:
During startup it starts a timer that executes the little sub-routine labeled MoveMyMouse every 60 seconds. This little sub-routine will move your mouse one pixel back and forth.
I also added an escape by pressing Ctrl+Alt+Win+Pause.
create a cmd file that starts your app and sends a key press to either the left or right "Windows" keys
triggering the windows key will open the start menu
to trigger the windows key, you need to download nircmd.zip and unzip its folder somewhere, then cd the path to that folder
paths may be case sensitive
start "C:\path-to-your-apps\app-name.exe"
cd "C:\path-to-nircmd-folder\"
nircmd sendkey lwin press
do not run your app on startup
instead run the cmd file on startup
to run the cmd file on startup, you first need to make a desktop shortcut to it.
do not right click cmd file > create shortcut
right click the cmd file > send to > desktop (create shortcut)
a window should appear
place the following into the browse field
C:\Windows\System32\cmd.exe /C "C:\path-to-cmd-folder\cmd-name.cmd"
name the shortcut then press the enter key
next, right click your new desktop shortcut > properties
navigate to shortcut tab
set "Run" to "Minimized" so you don't see the command prompt pop up as it runs your cmd file
finally, copy or move your shortcut into your startup folder
"C:\Windows\User\your-user-name\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"

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