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.
Related
If I run the following code, if I hit the ^+q it does not stop entering the numbers 1-100. Only after it completes does the script exit. Is there a way to get the script to stop even if it is in the middle of sending keystrokes?
^j::
ArrayCount := 100
Loop % ArrayCount
{
Send, %A_index%
}
return
^+q::ExitApp ; Exit script with Escape key
There are 2 issues with your code.
Sendreleases modifier keys when it simulates input, using it in a loop this way is going to interfere with autohotkey's hotkey detection. You can still activate ^+q if you press the 3 buttons simultaneously but it's much easier to use a hotkey without modifiers for example the Escape key. This is also what your comment says you're doing
^+q::ExitApp ; Exit script with Escape key
so as a bonus it will fix the discrepancy between your comment and your code ;).
The second problem is that the loop in which you execute the Send command is going to finish very quickly if you use SendInput and by the time ExitApp is executed all the numbers were already sent(even if you don't yet see the effect). In case of SendEvent there is some other problem which prevents other threads from being executed when you do it in a loop(don't know what causes it, might be a bug).
To solve it you need to add Sleep. At my system doing Sleep 1 works well. You can experiment with different numbers and send modes until you get the desired effect(you can also try 0 and -1.
Full code:
^j::
ArrayCount := 100
Loop % ArrayCount
{
Send %A_index%
Sleep 1 ; experiment with how long to sleep
}
return
Escape::ExitApp ; Exit script with Escape key
Whenever you press w you get into a loop that press e around every 10 seconds. Another button has to be pressed to get out of it again at any moment (and making it possible to start over again). This is what I have so far:
w::
Loop
{
Send, e
Random, SleepAmount, 9000, 10000
Sleep, %SleepAmount%
x::Break
}
Return
I don't understand why it is not working yet. It presses e once and doesn't do anything anymore after that.
x::Break
is the short form for
x::
break
return
and therefore terminates the current subroutine. Never define a hotkey within any other execution bodies. Instead, define the x-hotkey outside the w hotkey and make it stop the loop.
Example using goTo (note that goSub is different for the latter will not terminate the subroutine):
w::
Loop {
send e
Random, SleepAmount, 9000, 10000
Sleep, %SleepAmount%
}
after_loop:
return
x::
goTo after_loop
return
; or, more compact:
; x::goto after_loop
Since gotos are pretty bad programming style, you might consider using a timer (instead of the loop). But to be honest, it's not worth it because your sleep amount is not fix.
Assume this code:
Loop
{
if enabled
Send, /
}
m::
enabled := !enabled
Return
I want to toggle sending / to a Notepad for example. But if I run this code by pressing M on keyboard, then pressing the M key again does not disable sending.
Looks like the send command in the Loop cause this issue since Ive tried using msgbox which does not disable the m key.
How can I make this code to work? (SendInput and Play does not work too)
It's because your loop is blocking any other execution. Unless that loop is the only thing in your script, you generally want to avoid using loops and use timers instead.
Timers don't block further execution but act more like their own thread. Here's an example using a timer:
slashTimerActive := 0
m::
if (!slashTimerActive)
SetTimer, SendSlash, 100 ; Call the sub every 100ms
else
SetTimer, SendSlash, Off
slashTimerActive := !slashTimerActive ; Flip the variable
return
; Subroutine
SendSlash:
SendInput, /
return
Well i want to make a script with the objetive:
3{DOWN} key, and later hold or quickly press Z x3, and loop all that.
I have been trying to work with loop command but i just can't, im new with AutoHotkey and english it's not my native languague so it's been pretty hard.
Here is the code i tried but didn't work as i expect, since it press Z before the 3 {DOWN} keys.
#Persistent
SetTimer, Code, 150
Return
Code:
Send, Z{DOWN}
Return
If you know anyway to improve what i'm doing like, add a toggle like F8 to turn on/off, it would be aweosome.
Thanks for any help.
Helena.
Helena, What your script does right now is the following. As soon as the script starts it will start to send [Z] and [Arrow down] every 150 mili seconds. This is independent of what application is running at that time.
You write that you want to loop sending codes and that you want to toggle this ON/OFF.
Here is an example that comes closer to your goal.
#Persistent
F8:: ; This is your [F8] Toggle hotkey
If Toggle:=!Toggle ; Here you "test" the value of the variable "toggle" and after testing switch it to the opposite (true/false)
SetTimer, Trigger, -1 ; This is to create a separate thread for the loop. -1 means start in 1 ms but only do this one time, not every 1 ms's.
return
Trigger:
While (Toggle)
{
Send, +z{Down} ; + is the shift key, thus +z makes captial Z
Sleep, 500 ; Wait 500 ms (1/2 a second)
Send, +{z Down} ; Press Shift z Down. This will NOT start a repeat like ZZZZZZZZZZZZZZZZ
Sleep, 500 ; Wait 500 ms (1/2 a second)
Send, +{z Up} ; Lift Shift z Up
Sleep, 1 ; Required. Without it The F8 keypress to toggle off can not be read anymore
}
Return
I have to run few applications like browser and ticketing tool. But, I need to continuously press shift+F5 keystrokes for reloading the pages of my browser and ticketing tool.
Can I use any script through autohotkey for invoking 'Shift+F5' keys repeatedly for every 20 seconds regular interval?
Can anybody please help me with writing the script for autohotkey for invoking 'Shift+F5' keystrokes?
I assume that you want to be able to turn this feature on/off
#SingleInstance Force
#installKeybdHook
#Persistent
#F5:: ; [Win]+[F5] to start timer
SetTimer, RefreshPage, 20000
TrayTip, Refresh, Started, 1
ToolTip, Refresh Active
Return
+#F5:: ; [Shift]+[Win]+[F5] to stop timer
SetTimer, RefreshPage, Off
TrayTip, Refresh, Stopped, 1
ToolTip
Return
RefreshPage:
Send, +{F5}
Return