I have a simple macro, since i am very new to it. Literally started today. But i run this macro, and it runs once and stops. Why is that? Here it is:
q::
Send asdq
Esc::ExitApp
return
If someone could help me, I would really appreciate it.
Each hotkey you assign, that isn't a single line, requires a Return.
Since you have a definition of a Hotkey inside another, your second hotkey esc::exitapp is being executed when you press q and exiting your script.
Try:
q::Send, asdq
Esc::ExitApp
or:
q::
Send, asdq
Return
Esc::ExitApp
Edit:
q::
Loop
Send, asdq
Return
Esc:: ExitApp
Related
got a small test script
o::
Suspend
{
Send {JK} ;
}
Pause,,1
Return
where can i ass a loop so it loops JK and the hotkey o still works?
i tried to put loop in a few places like
Loop,
o::
Suspend
{
Send {JK} ;
}
Pause,,1
Return
it just didnt work wherever i put the Loop
Or is there another way that could possibly be done doing it not the way I tried? Kinda new to this so hopefully I could get an answer
So if I understand correct, you want the script keep sending JK when you pressed o once and the o still works? If so, this should work. You can add sleep after send if it's too fast. You just need to add ~ before hotkey so it's not blocking the hotkey itself. And for looping JK, Loop should be inside the hotkey function.
~o::
Loop,
{
Suspend
Send, {JK}
Pause,,1
}
Return
i.e. hitting ctrl+s should activate ctrl+s...the below does not work. Neither does the simpler
SendInput, ^s. Goal is to have ctrl+s save current doc and then load another via more code, the saving part never works tho. The bad code, depending on where i put sleep or no sleep, either returns s or shift s (in 1 code editor anyways) or nothing. I basically want a hotkey that mimics itself.
F4::ExitApp
<^s::
send, {lctrl down}
SLEEP 300
SEND s
SLEEP 300
SEND {lctrl up}
return
I would think that the issue your program is running into is that having the ^s send another ^s inside of itself is creating an infinite recursive loop in which nothing is ever able to run past the place you invoke ^s. To prevent this, we can use the $ modifier as so:
$<^s::
SendInput ^s
return
From the relevant section of the Modifier section of the docs:
This is usually only necessary if the script uses the Send command to
send the keys that comprise the hotkey itself, which might otherwise
cause it to trigger itself. The $ prefix forces the keyboard hook to
be used to implement this hotkey, which as a side-effect prevents the
Send command from triggering it. The $ prefix is equivalent to having
specified #UseHook somewhere above the definition of this hotkey.
Edit: it seems to work fine for me even if I remove the $ modifier. Testing the following code shows me there appears to be no problems regarding code execution before, after, or during the SendInput statement.
<^s::
MsgBox no
SendInput, ^s
MsgBox yes
return
Maybe check your version or installation of AHK?
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 have a very long script and I want to stop it with ESC key if it goes astray.
So I have put in the script this line:
Esc::ExitApp
But, of course, sometimes I have to press ESC key even when not running the script and when I need AutoHotkey, it's gone.
How do I tie ExitApp to ESC key just for a part of the script that's running?
You have to add ExitApp as a command in your script! It is a special kind of label, not a function. If the label doesn't exist in your script, you can't call it.
To make it exist, just add another line with the label on it. -->>**actually, I was wrong to say that. In the docs it gives the example that you can use simply Esc::ExitApp just like that to exit the running script ExitApp exists in the script even if you don't create it first because it's built into AutoHotkey. However, with your new code from your comment, you can use a custom label (which does have to exist in your script before you can call it):
Esc::goto DoSomething
DoSomething:
MsgBox, 4, , Are we exiting?
IfMsgBox, No
return ;after this return, the next line will not be called.
ExitApp ;as soon as this is called, the script ends completely and no more of the script will ever be called
return
this code is working:
Esc::goto ExitThisProgramQuestion
ExitThisProgramQuestion:
MsgBox, 4, , Are we exiting?
IfMsgBox, No
return
ExitApp
return
So I have no idea what I'm really doing, but I need help with a very simple script.
The basic idea is to press a key, such as k, four times each second, repeating until the script is turned off with a command (Alt-x).
I'm using Autohotkey script editor.
You can use Timers.
Example code:
F1:: ;key that launches this code
SetTimer, SendKeyK,250 ;set timer to repeat every 250 miliseconds
return ;end
F2::
SetTimer, SendKeyK,Off ;turn of the timer
return
SendKeyK:
Send, {k} ;timer sends (presses) the key k
return
As you can see there is no need to exit the script.