AHK script for a timed autofire function - timer

Goal of the script: to continually press Numpad0 for 10 seconds each time hotkey is pressed.
Current code:
toggle = 0
#MaxThreadsPerHotkey 2
timerToggle:
Toggle := !Toggle
sleep 10000
Toggle := !Toggle
F12::
SetTimer, timerToggle, -1
While Toggle{
send {NumPad0}
sleep 100
}
return
At present, the script will run as intended, but only once. Attempting to run it again after the first time does nothing. What am I missing?

I would rather use SetTimers instead of a 10 sec. long while like so
F12::
Send {Numpad0}
SetTimer, start, 100
SetTimer, stop, -10000
return
start:
Send {Numpad0}
return
stop:
SetTimer, start, off
return

Your script likely doesn't toggle your variable correctly. Here is a cleaner version of what you are trying to do which uses A_TickCount:
F12::SetTimer, HoldNumPad, -1
HoldNumPad:
kDown := A_TickCount
While ((A_TickCount - kDown) < 10000)
{
Send {Numpad0}
Sleep 100
}
Return
Note that pressing F12 while the label is running will not have any affect.
EDIT: Made SetTimer use -1 period to run only once, thanks to MCL.

Related

AHK, how to capture Rbutton in loop

Could someone help me understand how to capture Rbutton in Loop?
The idea is when I click GUI button its activate loop and waiting for some time for example 15 sec until RButton will be clicked, if loop detects that RBUtton been doubleclicked, should show me msg box
w::SetTimer Test, 5000
Test:
Loop { ; waiting when RButton will be clicked
Keywait,RButton
A := GetKeyState("Rbutton","P")
if (!%A%) {
MsgBox, this part should continue and wait for RButton do nothing and continue to wait
} else if (%A%) {
MsgBox, you clicked double RButton
Send, this is text
break
}
}
Msgbox end of script
SetTimer Test, Off
Return
My idea:
Use While loop as timer, ie, while within duration (eg 15 secs), keep checking status of double right button. If criteria met within duration, break the loop and show message box.
Use SetTimer and GetKeyState to constantly check and update number of right button clicked.
Note to below script. It is assumed that when GUI is clicked, DetectDoubleClick() will be fired.
Counter_RButton := 0
SecondsToWait := 15
DetectDoubleClick()
{
Global Counter_RButton
Now := A_TickCount
End := A_TickCount + (SecondsToWait * 1000)
SetTimer, CaptureRButton, 150
While (End >= Now)
{
if (Counter_RButton = 2)
{
Counter_RButton := 0
MsgBox,,,Double RButton Detected!
break
}
Now := A_TickCount
}
SetTimer, CaptureRButton, Off
return
}
CaptureRButton:
ButtonIsDown := GetKeyState("RButton")
If ButtonIsDown
Counter_RButton := Counter_RButton + 1
Exit

AutoHotkey - Running Loop for a Limited Amount of Time

I'm trying to run my loop for two seconds. Within those two seconds, if I click left a message box gets activated, telling me that I've clicked left. If the 2 seconds are up another message box is supposed to appear, telling me that I've been waiting enough. However, after 2 seconds nothing happens ;(
:*:tcc::
start := A_TickCount
totalTime := stop - start
Loop {
stop := A_TickCount
if (totalTime > 2000)
{
MsgBox, enough waiting!
return
}
else if GetKeyState("LButton")
{
MsgBox, you clicked left
return
}
}
The variable "totalTime" has to be created within the loop, every time the loop stops:
:*:tcc::
start := A_TickCount
Loop {
stop := A_TickCount
totalTime := stop - start
if (totalTime > 2000)
{
MsgBox, enough waiting!
return
}
else if GetKeyState("LButton")
{
MsgBox, you clicked left
return
}
}
return

reboot in a click mouse loop not working

I am trying to reboot PC at specific hour when a click mouse loop is running. I used if + else conditions and when I try to run both pieces of code separately the script works but when put together only the mouse clicking loop is running while the timer process takes no effect. Any ideas what’s wrong?
^+f::
SetTimer, Chronos, 500
return
Chronos:
FormatTime, TimeToMeet,,HHmm
If (TimeToMeet = 2018)
{
Run, %comspec% /c shutdown -r -f -t 0
}
else
{
loop
{
MouseClick, Left, 787, 512, 1,0
Sleep 10000
}
}
Return
k::Pause
The loop statement never exits.
You're using a timer so I assume you want your code to be event driven. If that's the case, make the MouseClick part of a timer instead of a sleeping loop.
If you don't want to use events, put your shutdown clause inside of the loop
This clicks (787,512) every 10 seconds until 20:18, at which time it shuts the computer down
^+f::
SetTimer, Chronos, 500
return
Chronos:
loop
{
FormatTime, TimeToMeet,,HHmm
If (TimeToMeet = 2018)
{
Run, %comspec% /c shutdown -r -f -t 0
}
MouseClick, Left, 787, 512, 1,0
Sleep 10000
}
k::Pause
This clicks (787,512) every 10 seconds until 20:18, at which time it shuts the computer down
F2:: start()
F3:: stop()
start()
{
global running := 1
while running {
FormatTime TimeToMeet,,HHmm
if (TimeToMeet = 1349 )
Shutdown 13
click 787,512
sleep 10000
}
}
stop()
{
global running := 0
}

Autohotkey time between clicks

Iv been recently interested in developing a way to measure the times between my mouse clicks for research however im unsure what functions autohotkey has available to help with this. I firstly tried to get a measure of the exact time using :
FormatTime, ssnow, %A_Now%, ss
The problem with this was that subtracting one time from another is apparently impossible in autohotkey according to some forums i have searched and the result when testing also produced an empty value.
Is there a way to initiate a counter when the left button is down and then stop the timer when the button is released?
Here is the code I have been working on:
clickTime := 0
lastClick := 0
~LButton::
FormatTime, ssnow, %A_THEN%, ss
lastClick=%A_THEN%
~LButton Up::
FormatTime, ssnow, %A_Now%, ss
clickTime=%A_Now%
MsgBox (%clickTime% - %lastClick% )
Try:
~LButton::
StartTime := A_TickCount
While(GetKeyState("LButton", "P"))
continue
ToolTip % A_TickCount - StartTime
return
or:
~LButton::
StartTime := A_TickCount
keywait, LButton, L
ToolTip % A_TickCount - StartTime
return

AutoHotKey run program on any key

I'd like to run a program when any key is pressed with AutoHotKey
Something like:
AnyKey::Run, D:\my\program\to\run\on\any\key.bat
EDIT2:
This code is working perfectly:
#InstallKeybdHook
SetTimer, AnyKeyPressed, 100
AnyKeyPressed:
if( A_TimeIdlePhysical < 100 ){
Run, D:\my\program\to\run\on\any\key.bat
}
^!p::pause
A simple solution:
#InstallKeybdHook ; this MUST be called at the start of your script
AnyKeyPressed() ; returns a 1 if any keyboard key is pressed, else returns 0
{
if( A_TimeIdlePhysical < 25 )
return 1
return 0
}
Note this function will return 1 if any key is pressed OR being held down, so change your code appropriately.
What happens is; the #InstallKeybdHook will change the behaviour of A_TimeIdlePhysical to only look for keyboard events.
You have to check A_TimeIdlePhysical periodically, not just once on script start:
#InstallKeybdHook
SetTimer, CheckActivity, 100
Exit
CheckActivity:
if(A_TimeIdlePhysical < 100) {
Run, myNastyPictureMaker.bat
ExitApp
}
return
You can use SetTimer for recurring tasks. The script stops when the first activity was detected; otherwise, it would take a picture every 100 ms (or whatever timeout you set).
P.S: I hope you only want to use such a script on your private PC and not some publically available computer...
Use Input, AnyKey, L1 to wait for any key to be pressed. L1 means after one key was pressed, without a [end] key required. You can check the content of AnyKey, but don't really need to.
Perhaps a list of known keys might work?
keys = ``1234567890-=qwertyuiop[]\asdfghjkl;'zxcvbnm,./
Loop Parse, keys
Run, D:\my\program\to\run\on\any\key.bat
return
This is what comes to mind.
#Persistent
#InstallKeyBDHook
SetTimer, WaitingForKey, 100
Return
WaitingForKey:
Input, LogChar, B I L1 V
LogWord := LogWord . LogChar
ToolTip, % LogWord
;Run, D:\my\program\to\run\on\any\key.bat
LogWord:=
Return
^!p::pause

Resources