How To Detect Ctrl+V In AHK? - autohotkey

I am trying to detect Ctrl+V. Then, make Xbutton1act as Enter for a few seconds, but I can't get it working.
Transform, CtrlV, Chr, 3
Input, OutputVar, L1 M
XButton1::
if OutputVar = CtrlV
{
SetTimer, SendEnter, 0
Sleep, 2000
SetTimer, SendEnter, Off
}
else
{
Send ^t
}
Return
SendEnter:
Send {Enter}
Return

~^v::lastPaste := A_TickCount ;stores counter when ctrl+v is pressed
Xbutton1::
If A_TickCount - lastPaste < 2000 ;checks if 2 seconds gone after ctrl+v was clicked
{
Send, {Enter} ;sends enter
Return
}
else
{
Send, ^t ;sends ctrl+t
Return
}

Related

AutoHotKey - Multiply variables not working

I have problems with an AutoHotKey script. When I press F1, the left mouse button is held but A is also pressed. Does anyone know how I can fix this?
#MaxThreadsPerHotkey, 2
Toggle := 0
Toggle2 := 0
F1::
Toggle := !Toggle
If (Toggle){
Click, Down
} else {
Click, Up
}
F2::
Toggle2 := !Toggle2
If (Toggle2){
send {a down}
} else {
send {a up}
}
You need to tell autohotkey that you are done writing the code that should be executed when a hotkey is pressed by putting a Return after the last part you want to execute.
From the docs:
Returns from a subroutine to which execution had previously jumped via
function-call, Gosub, Hotkey activation, GroupActivate, or other
means.
So for your script:
#MaxThreadsPerHotkey, 2
Toggle := 0
Toggle2 := 0
F1::
Toggle := !Toggle
If (Toggle){
Click, Down
} else {
Click, Up
}
return
F2::
Toggle2 := !Toggle2
If (Toggle2){
send {a down}
} else {
send {a up}
}
return

Sending a key while other key is pressed doesn't spam the pressed key

Im trying to make a script to send a button while another one is pressed, like
$c::
send {c down}
sendQ()
send {c up}
return
sendQ()
{
if GetKeyState("c","P") {
sendinput {q}
sleep 2222
}
}
it works, but not as intended.
It send cq with a 2222 interval like cq..cq..cq
I need the result like: cqcccccccqccccccccqcccc
is there a way to spam 'c' while pressing 'q' with interval?
This seems to work:
isCPressed := 0
c::
Send c
if (!isCPressed) { ; Avoid resetting the timer
SetTimer, SendQ, 2222
}
isCPressed := 1
Return
c Up::
SetTimer, SendQ, Delete
isCPressed := 0
Return
SendQ() {
SendInput q
}
In most situations, you can also replace
c::
Send c
with
~c::
Faking multi-threading in ahk can produce the required output. This way the script can send both c and q in their individual threads, after waiting for the specified time.
#Persistent
~c::
Send, q ;~ for initial cq
SetTimer, sendC, 100 ;~ frequency of sending c
SetTimer, sendQ, 2222 ;~ frequency of sending q
return
;~ Sending c
sendC:
Send, c
return
;~ Sending q
sendQ:
Send, q
return

How to change the loop?

The script is searching for a certain text in the clipboard. When found, it shows a MsgBox.
I would like this script to stop when the text has been found. How to achieve that?
#Persistent
MouseMove, 821, 700
Sleep, 500
MouseClick, Left
Sleep, 500
Loop, 5
{
Send, ^c
Sleep, 500
Send, {PgDn}
}
OnClipboardChange:
If InStr( Clipboard, "Part3" )
SetTimer, PopupMsgBox, -1
Return
PopupMsgBox:
Msgbox, Part3 Found
Return
#Persistent
clip_search := "Part3"
MouseMove, 821, 700
Sleep, 500
MouseClick, Left
Sleep, 500
Loop, 5
{
Send, ^c
ClipWait
If InStr( Clipboard, clip_search )
{
MsgBox, % clip_search " Found."
Break
}
Sleep, 500
Send, {PgDn}
}

AutoHotKey Clicking Script

I've only just started with AutoHotKey, and I'm looking to make a script that will click once per second 10 times, then hold right mouse button for 3 seconds, before resetting. I intend it to active on alt+c, and break if I press the left mouse button.
The script I came up with it
LButton::
BreakLoop = 1
return
!c::
Loop
{
if (BreakLoop = 1)
break
;
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Send, {RButton Down}
Sleep, 3000
Send, {RButton Up}
Return
}
However, this is not working. Is there a way to fix this, or have I taken the completely wrong approach for this script?
You did make a Mistake in the code, On the Bottom You did have the Return Command into the Loop that is not possible. (This Return Command Will be needed for !c:: and it must outsite the loop command)
The Code must be Like:
~LButton::
BreakLoop = 1
return
!c::
Loop
{
if (BreakLoop = 1)
break
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Send, {RButton Down}
Sleep, 3000
Send, {RButton Up}
}
Return
Tip: if you change Lbutton:: into ~Lbutton:: then The Default LeftButton is also active.
I was actually able to find a way to compact it significantly (and break the loop faster) by nesting a loop within a loop
!s::
BreakLoop = 1
return
!c::
BreakLoop = 0
Loop
{
Loop 10
{
if (BreakLoop = 1)
break
;
Click
Sleep, 900
}
Send, {RButton Down}
Sleep, 3000
Send, {RButton Up}
}
if (BreakLoop = 1)
Break
;
Return
A better method is to use SetTimer, this allows you to break out of the loop at any point in your sequence of actions.
Try:
!c::setTimer, doAction, 1000
!s::SetTimer, doAction, Off
doAction:
i += (i <= 14 ? 1 : -13)
if (i == 14)
send, {RButton Up}
else if (i == 11 )
Send, {RButton Down}
else if (i <= 10)
click
return

Changing AHK Response, Everytime I move my mouse :(

Here's my sample script
$f2::
loop, 1
while GetKeyState("f2", "P")
{
setkeydelay, 1
send, {f2}
click
}
return
I have 3 delays Normal, Fast, Faster everytime I move my mouse it changes its delay.What should I add in the script to make the response consistent?
ListLines, Off
CoordMode, Mouse
cnt:=1, arr:=[50 ; normal
, 25 ; fast
, 0] ; faster
$F2::
;~ TrayTip,, % "delay is: "arr[cnt]
MouseGetPos, xPos, yPos
xPosPrev:=xPos, yPosPrev:=yPos
While, GetKeyState("F2", "P")
{
MouseGetPos, xPos, yPos
If (xPosPrev!=xPos Or yPosPrev!=yPos)
{
`(cnt=3) ? cnt:=1:cnt++, xPosPrev:=xPos, yPosPrev:=yPos, bool:=False
;~ TrayTip,, % "delay changed to: "arr[cnt]
While, !bool
{
MouseGetPos, xPos, yPos
bool:=(xPosPrev=xPos And yPosPrev=yPos) And A_Index>50 ? True:False
Sleep, GetKeyState("F2", "P") ? 25:-1
xPosPrev:=xPos, yPosPrev:=yPos
}
}
SetKeyDelay, arr[cnt]
Send, {F2}{Click}
Sleep, GetKeyState("F2", "P") ? 25:-1
}
Return