Hotkey doesn't loop with Send command - autohotkey

I have this code, and i want to whenever i hold ctrl+n it will click and drag. It doesn't loop with the Send commands in place. It only runs once, unless i let go of ctrl and n and press again. If i comment them out the hotkey loops perfectly fine when i hold it down. Heres my script:
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn ; Enable warnings to assist with detecting common errors.
; SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
^n::
CoordMode, Mouse, Screen
Send {LButton down} ;if i comment these two out it works fine
MouseMove, 0, 30, 20, R
Send {LButton up} ; ditto
return

The problem was ctrl key. When pressed down it stops new execution of hotkey. I tried it without ctrl prefix and used just n:: and it worked fine. It also works if you press ctrl+n then release it then press it again.
Anyways when you add Hotkey Modifier $ sign in front it works as you wanted.
Hotkey Modifier $ explanation:
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.
More info:
https://www.autohotkey.com/docs/Hotkeys.htm
$^n::
CoordMode, Mouse, Screen
Send {LButton down}
MouseMove, 0, 30, 20, R
Send {LButton up}
return

Related

ctrl+s not being sent in autohotkey script

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?

Cant make a toggle in AutoHotkey

I'm trying to make "]" a toggle for when I press "p" its presses "e" 3 times total with 10ms spaces. Then toggle off if I just want to press "t".
I have this but set to "p" just to press "e" 3 times quickly but without a toggle, in case I want to normally type "t".
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
SetKeyDelay , 10, 10 ; first is delay between keypresses, and second is press duration
; we are using ControlSend here because Send and SendInput is not affected by SetKeyDelay.
p::
ControlSend, , e, A
ControlSend, , e, A
ControlSend, , e, A
return,
/::
ExitApp
return,
Can I get help on making a toggle?
Sounds like to me you're simply looking to toggle the hotkey being enabled.
This is very easily doable with Suspend(docs).
Also, this is false:
we are using ControlSend here because Send and SendInput is not affected by SetKeyDelay.
SetKeyDelay(docs) works on normal Send just fine. You're actually using SendMode Input(docs) to change the default sendmode to SendInput. That's why it's not working.
I know those first 4 lines are auto-generated when you non-manually create a new ahk file.
It's good to know what they're actually doing.
So don't ControlSend unless you actually need to ControlSend, switch over to normal Send.
Also, your usage of SetKeyDelay(docs) is a bit weird there.
It's intended for the ability to add delay to a single command. With your approach you might as well have Sleeps there in-between.
Fixed/revised script:
SetKeyDelay , 10, 10
p::Send, eee
]::Suspend
/::ExitApp

LButton Hotkey seems to prevent Send, {LButton}

I'm at a loss here. As soon as I add the LButton hotkey, the Send, {LButton} doesn't seem to work, but they show up in recently executed lines.
Env. Windows 7x64, Disabled Touchpad, AHK v1.1.31.01.
I try to emulate the Wink application (from DebugMode) to capture screenshots for training purposes. For that reason, I want to capture a screenshot just before a mouse click. This looks easy, and I even vaguely remember doing similar mouse hotkeys in the past. However I can't get this to work.
Step 1: I just reduced it to this simple script:
#InstallKeybdHook
#InstallMouseHook
#UseHook
#Persistent
Return
a::
Send, {LButton}
Return
q::
ExitApp
When using this script, I can simulate clicking the Left Mouse Button through the a key. Nothing special.
However as soon as I add either a line with "Hotkey, $LButton, MySendClick", or "$LButton::" the previously working a hotkey no longer works. In the recently executed lines, you can see the "Send, {LButton}" lines, but nothing is being send. Unexpectedly, the a hotkey actually causes the "$LButton::" hotkey to trigger (without it sending {LButton}). When I change the a hotkey to send "RButton" and the $LButton:: to $RButton::, then Send {Click} works perfectly (eventhough the a hotkey should never be able to trigger $RButton::).
Originally I just wanted to have the following HotKey:
$LButton::
SoundBeep, 300, 150 ; For testing only
; Send, ^{PrintScreen} ; To trigger Greenshot in the background
Sleep, 100
Send, {LButton}
Return
I upgraded from AHK v1.1.22.04 to v1.1.31.01. No improvement.
I tried "Click", "sendInput, {LButton}", "Send {Click}", "MouseClick, Left".
I tried "$LButton::", "vk01sc000::", "Hotkey, $LButton, MyClick".
Is this an issue with my specific Windows 7 configuration or an "undocumented AHK feature"?
#InstallKeybdHook
#InstallMouseHook
#UseHook
#Persistent
Return
a::
Send, {LButton}
Return
$LButton::
SoundBeep, 300, 150 ; Should be Send, ^{PrintScreen} ; To trigger Greenshot in the background
MouseClick, Left
Return
q::
ExitApp
In this last test example, When $LButton:: is disabled, the a hotkey works like a charm, but as soon as I enable $LButton::, the a hotkey triggers $LButton:: and no mouse click is being sent to the windows applications.
I would appreciate it when other Windows 7 users could quickly test this issue.
In my experience, using keys that you still want the input to pass through need the Tilde prefix.
https://www.autohotkey.com/docs/Hotkeys.htm#Tilde
~LButton::
SoundBeep, 300, 150 ; Should be Send, ^{PrintScreen} ; To trigger Greenshot in the background
KeyWait, LButton ; Wait for lbutton to be released.
Return

AutoHotKey pause part of the script

I've got a script running which binds numpad numbers to inline numbers (in order to easily add Unicode chars on the fly because I'm on a laptop without the numpad) and that also binds Home and End to Ctrl+Left and Ctrl+Right respectively. Now, the whole script can be paused by pressing the Apps key, but I'd like the numpad bindings to be the only part toggled and keep Ctrl+Arrows binding always running instead. How can I modify this script to do that?
#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%
^Left::Send {Home}
+^Left::Send {LShift down}{Home}{LShift up}
^Right::Send {End}
+^Right::Send {LShift down}{End}{LShift up}
0::Numpad0
1::Numpad1
2::Numpad2
3::Numpad3
4::Numpad4
5::Numpad5
6::Numpad6
7::Numpad7
8::Numpad8
9::Numpad9
Appskey::
Suspend,Toggle
return
#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%
#SingleInstance Force
; These hotkeys are always active (even if the script is suspended):
^Left::
Suspend Toggle
Send {Home}
Suspend Toggle
return
+^Left::
Suspend Toggle
Send {LShift down}{Home}{LShift up}
Suspend Toggle
return
^Right::
Suspend Toggle
Send {End}
Suspend Toggle
return
+^Right::
Suspend Toggle
Send {LShift down}{End}{LShift up}
Suspend Toggle
return
Appskey:: Suspend,Toggle
; These hotkeys are inactive if the script is suspended:
0::Numpad0
1::Numpad1
2::Numpad2
3::Numpad3
4::Numpad4
5::Numpad5
6::Numpad6
7::Numpad7
8::Numpad8
9::Numpad9
https://autohotkey.com/docs/commands/Suspend.htm#Remarks:
Any hotkey/hotstring subroutine whose very first line is Suspend
(except Suspend On) will be exempt from suspension. In other words,
the hotkey will remain enabled even while suspension is ON. This
allows suspension to be turned off via such a hotkey.

How do I add a delay between SendInput commands in AutoHotkey?

I have an AutoHotkey script using SendInput which sends MouseClick commands too quickly for my program to handle. My script will send a MouseClick to focus an input field, then start typing before the field finishes focusing.
I've tried using SetKeyDelay to make my script run a bit slower, but this doesn't work with SendInput.
Note: SetKeyDelay is not obeyed by SendInput; there is no delay between keystrokes in that mode. This same is true for Send when SendMode Input is in effect.
Documentation for SetKeyDelay
My current workaround is to use sleep commands after each input, but this is less than ideal.
SendMode Input
F1::
MouseClick, left, 61, 50 ; select title field
sleep 100 ; artificial delay to prevent misfocused inputs
SendInput %user_input%{Enter} ; enter job title
sleep 100 ; artificial delay
MouseClick, left, 67, 408 ; select job
sleep 100 ; artificial delay
Return
Ideally I would like a more elegant solution for adding a delay between each SendInput command without manually using a sleep command each time.
How can I add a delay between SendInput commands in AutoHotkey without repeatedly using sleep?
Try using SendPlay instead of SendInput.
This sends text and mouse clicks with a 100ms delay following each click
user_input := "hello world"
SetMouseDelay 100, Play
SendPlay {Click 61,50}%user_input%{enter}{click 67,408}
From the documentation for SendPlay.
SendPlay
Note: SendPlay may have no effect at all if UAC is enabled, even if the script is running as an administrator. For more information, refer to the FAQ.
Like SendInput, SendPlay's keystrokes do not get interspersed with keystrokes typed by the user. Thus, if the user happens to type something during a SendPlay, those keystrokes are postponed until afterward.
Although SendPlay is considerably slower than SendInput, it is usually faster than the traditional SendEvent mode (even when KeyDelay is -1).
SendPlay does not use the standard settings of SetKeyDelay and SetMouseDelay. Instead, it defaults to no delay at all, which can be changed as shown in the following examples:
SetKeyDelay, 0, 10, Play ; Note that both 0 and -1 are the same in SendPlay mode.
SetMouseDelay, 10, Play