AHK v2 mouse clicking loop - autohotkey

global toggleVar := false
>^j::
{
global toggleVar :=! toggleVar
}
if (WinActive("Warframe") and toggleVar = true)
{
while (GetKeyState("LButton"))
{
Click
Sleep 250
}
}
I am absolute noob in autohotkey, installed it today, also got scared by a bunch of viruses. Anyways, I wanted to have a toggle variable and when I'm in a game I want to hold down LMB and it will continue clicking for me. But it does nothing and I have no idea why.

#HotIf WinActive("Warframe")
>^j::
{
static toggle := 0
HotIf 'WinActive("Warframe")' ; change the hotkey context for Hotkey function
Hotkey "LButton", SpamClick, (toggle := !toggle) ? "On" : "Off" ; enable or disable lbutton hotkey
SpamClick(ThisHotkey) {
while GetKeyState("LButton", "P") {
Click
Sleep 250
}
}
}
#HotIf
Apparently this is how it's supposed to be done, it worked for me. Thanks to "plankoe" from reddit.

Related

combining hotkeys

I'm trying to make an autoclicker in autohotkey but can't get it to work.
I have some knowledge about autohotkey but it is not very much:
<^LButton::
Loop
{
SetMouseDelay 0.001
Click
If(GetKeyState("LButton","P")=0)
Break
}
It works with <^LButton as hotkey, but not <^nLButton.
Therefore I need help with hotkey-combinations.
I get the errorcode:
Line Text: <^nLButtonSuspend
Error: This line does not contain a recognized action.
If you want to combinate Three keys as a Hotkey.
Click the Keys : [Ctrl] + [n] + [Lbutton] = Do a Action.
You can Try this:
example1.ahk
;#notrayicon
#SingleInstance force
^n::
GetKeyState, state, Lbutton
if state = D
{
Loop
{
send a
;SetMouseDelay 0.001
;Click
If(GetKeyState("LButton","P")=0)
Break
}
} else {
send b ;this codeline is only so that you can test it out in notepad. - you can remove this
}
Return
esc::exitapp
note : It is not perfect but the answer is close to your Question.

Autohotkey Return to regular function of Spacebar after mapping

i am trying to be able to toggle between different functions mapped to the spacebar
mapping spacebar isnt difficult, but how the hell do i get the "regular" function of spacebar back ?
Space::Space isnt working XD
BackSpace::tog()
tog()
{
static togstate = 0
if (togstate = 1)
{
ToolTip, Spacebar Normal
SetTimer, RemoveToolTip, 1500
Space::
togstate = 0
}
else
{
ToolTip, Spacebar Alternativ
SetTimer, RemoveToolTip, 1500
Space::
{
SetKeyDelay,5, 10
Send, 2345
}
return
togstate = 1
}
}
Ordinary hotkey bindings are more like declarations than commands or functions. So, they need to be on the global level.
You just need to use the Hotkey command, either inside or outside of functions.
Example:
Hotkey Space, funcA
Hotkey Space, funcB
Hotkey Space, Off
funcA() {
...
}
funcB() {
...
}

Autohotkey sends default button action if the cursor is over an inactive window

I wrote the following code in order to remap the 4-th and 5-th mouse buttons to scroll up & scroll down:
Code:
XButton2::
While GetKeyState("XButton2","P")
{
Send {WheelUp 1}
Sleep 120
}
return
XButton1::
While GetKeyState("XButton1","P")
{
Send {WheelDown 1}
Sleep 120
}
return
The problem:
If the cursor is over an active window, everything works.
But, when I click the XButton1 (4-th mouse button) or XButton2 (5-th mouse button) with the cursor over inactive window, sometimes it sends the default action into that window.
For example, if the cursor is over Chrome (inactive) and the active window is Task manager, it will send the default "page back" action instead of specified in the script "scroll down", but when I click on Chrome and make it active, everything works
Question:
Is there a way to just disable the default action? So that if you're not holding the button (While GetKeyState("XButton1","P")) it just does nothing?
Try this. It makes it so hotkeys work only if certain window is active and return nothing if they are not. I don't have extra mouse buttons so I tested it with regular keyboard keys and it worked.
loop
{
sleep,50
#if WinActive("ahk_exe chrome.exe") or WinActive("ahk_exe explorer.exe") ;;hotkeys work only if chrome.exe or explorer.exe is active
{
XButton2::
While GetKeyState("XButton2","P")
{
Send {WheelUp 1}
Sleep 120
}
XButton1::
While GetKeyState("XButton1","P")
{
Send {WheelDown 1}
Sleep 120
}
}
#if ! WinActive("ahk_exe chrome.exe") ;;if chrome isn't active XButton1 and XButton2 return nothing
{
XButton1::return
XButton2::return
}
}

Suspend AHK script

I'm writing a script for AHK which automate few things in my MMORPG game.
I want to perform 2 different automation when F2 or F3 is pressed.
Sometimes this script is with interaction with another action from keyboard/mouse so I need to suspend script when one of the following keys is pressed: 2,3,4,5,E,R
But when I press 1 the script need to be unsuspended.
I was trying many different options but I can't write it. I can't even google anything similiar.
My code looks like that:
key_a:="F2"
key_b:="F3"
loop {
sleep 1
if GetKeyState(key_a) {
a:=true
b:=false
}
if GetKeyState(key_b) {
a:=false
b:=true
}
if a {
MsgBox, Do something
}
if b {
MsgBox, Do somethiung else
}
}
This script language is bit strange for me.
Can you help me.
Thanks
Try:
key_a:="F2"
key_b:="F3"
SetTimer, keyStates, 100
KeyStates: ; SubRoutine
if GetKeyState(key_a, "P") {
a:=true
b:=false
}
if GetKeyState(key_b, "P") {
a:=false
b:=true
}
if a {
ToolTip, F2 is doing something
}
if b {
ToolTip, F3 is Doing Something!
}
return
2::
3::
4::
5::
E::
R::SetTimer, keyStates, Off
1::SetTimer, keyStates, on

How to have one key toggle between two functions using AHK

How would I go about creating a hotkey in AHK that runs one function the first time it is pressed and another one the second time it is pressed. I'm trying to emulate an on/off scenario.
Can it be done and if so how?
Something like this pseudo code is what I'm looking for:
#space::toggleit()
toggleit()
{
if (last time it was pressed, functionA was run)
run functionB
else
run functionA
}
I'm using AHK version 1.1.19.01
Try using AutoHotkey's hotkey-command:
hotkey, #space, functionA
return
functionA:
hotkey, #space, functionB
msgbox, A
return
functionB:
hotkey, #space, functionA
msgbox, B
return
If you'd want to toggle only between functionA and "do nothing", you can use the following:
hotkey, #space, functionA, toggle
return
functionA:
msgbox, A
return
I found a solution. Putting it here for other's benefit:
#space::tog()
tog()
{
static togstate = 0
if (togstate = 1)
{
msgbox ON
togstate = 0
}
else
{
msgbox OFF
togstate = 1
}
}
An easy way is to use a variable:
setting := false
toggleit()
{
if setting
run functionB
else
run functionA
setting := !setting
}