Cant make a toggle in AutoHotkey - 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

Related

Key not gettiing sent to background window ControlSend

I'm trying to send f keys to a backgrounded game while I browse the web. The script runs and doesn't error out
#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.
toggle = 0
#MaxThreadsPerHotkey 2
F8::
Toggle := !Toggle
While Toggle{
ControlSend [f,Entropia.exe]
}
return
IF I run this , activate the toggle, and press f with entropia opened and say notepad as the focus the Fs are showing up in notepad and not being sent to entropia
You cannot use AHK to send keys to a background window (just like you can't type on your keyboard into a background window).
Best you can do is quickly switch over to the window (you don't have to show the window or bring it forward, just activate it briefly) then send the key and then switch back to your current window.

Hotkey doesn't loop with Send command

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

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

Autohotkey pressing key in specific window

I run multiple windows of the same program and I want to make ahk press a specific button in all of them, for examle "0". I tried using alt tab command, it worked but only 1 time and I was not able to loop it properly. Maybe there is another better way to do make it press a key in every window?
As long as you use Internet Explorer and each IE window is separate (not all in different tabs in the same window), the following should show you how to loop through each open IE window. It will also show explorer windows, so your code can first determine what type of window before proceeding to send the keys.
#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.
SetTitleMatchMode, 2 ; approximate match
for x in ComObjCreate("Shell.Application").Windows {
{
MsgBox,,Open Windows, % "`n" x.locationName "`n`n" x.locationURL "`n`n" x.FullName "`n"
winactivate, % x.locationName
sleep, 1000
WinMaximize, % x.locationName
sleep, 1000
; do something here
}
}
return
Hth,

Send, SendInput, SendEvent functions entering single key multiple times

I have created a script in AutoHotKey.I have to Enter the entire file path to be opened in the
file browser.i have used send function but it passes the same keys multiple times. I tried using the function SetKeyDelay but still it enters the keys multiple times. I tried other alternatives as sendInput and SendEvent but still it didnt work.
Even if i terminate the script in between and if the control switches to some Input box or Editor, it starts entering the values into that area. Send function keeps on entering the keys even after the script execution is terminated.
Script:
;Open Adobe Acrobat 8.
run Acrobat.exe sleep, 1000
WinWait, Adobe Acrobat Professional,
Sleep, 1000
;Open Compare Documents Window
send, {ALT}A
Sleep, 1000
send, U
WinWait, Compare Documents,
;Enter File Path
IfWinNotActive, Compare Documents,
WinActivate, Compare Documents,
WinWaitActive, Compare Documents,
Sleep, 1000
send, !H
WinWaitActive, Open,,1000
sleep, 1000
SendEvent, "D:\Sample\a.pdf"
It Enters text something like this
CCCC:::::\\DDDDiiiiii
Things to try:
Update AutoHotkey to the latest version.
Don't use SendEvent. It allows other input to interrupt the text as it is being entered. Use SendInput instead. It is recommended to put the following at the top of all your scripts:
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
SendMode Input changes Send to use SendInput instead of SendEvent. It buffers the input and sends it all at once instead of letting other keyboard/scripts interrupt the text. See: Send for more information.
Restart your computer and make sure that there are no other AutoHotkey programs running, or that there isn't multiple instances of your Autohotkey script running. This is especially a problem when testing, as you might have launched the same script multiple times, and if the thread hasn't been properly terminated, you may have multiple instances there waiting for the Open window to be activated. Once the Open window has been activated, then you may have 4+ dormant scripts all trying to type at the same time, which would give you the output that you are describing.