Different function if key pressed again - autohotkey

So far my code looks like this:
capslock::
send FIRST FUNCTION
send {capslock up}
keywait, capslock,d
send SECOND FUNCTION
return
Doesn't work, always performs first function, then second.
What I'm trying to do is esentially a toggle.
Pressed - first function, Pressed- second function, Pressed- first function and so on.
Any suggestion? thanks

A very simple way to create a toggle, is to check the value of some variable and then change its value, like this:
CapsLock::
if (toggle) ;if true
{
ToolTip, action2
toggle := false
}
else ;else (if false)
{
ToolTip, action1
toggle := true
}
return

Related

AHK v2 mouse clicking loop

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.

How to double space as ctrl key?

I want the space key to work like Ctrl after 200 ms of being pressed. So, in theory it should work like this:
while space key is press down {
for first 200 ms {
if another key is pressed or space key released {
work like normal space
go no further
}
}
after first 200 ms {
if another key <Key> is pressed {
Send Ctrl+Key
}
}
}
Is it possible with AutoHotKey?
At least part of this is possible with AutoHotkey, the below fulfills everything except 'if another key is pressed'. It might be possible if you made every key a hotkey and then used A_PriorHotkey. Or perhaps there is another way I haven't thought of.
$Space::
KeyWait, Space, T2
if(ErrorLevel) {
Input, SingleKey, L1, {LControl}{RControl}{LAlt}{RAlt}{LShift}{RShift}{LWin}{RWin}{AppsKey}{F1}{F2}{F3}{F4}{F5}{F6}{F7}{F8}{F9}{F10}{F11}{F12}{Left}{Right}{Up}{Down}{Home}{End}{PgUp}{PgDn}{Del}{Ins}{BS}{CapsLock}{NumLock}{PrintScreen}{Pause}
Send {Ctrl down}%SingleKey%{Ctrl up}
} else {
Send {Space}
}
return
I took the input part from here.

how can i create an equivalent "$" effect on mouseclick events in AHK?

I'm trying to create an AHK script where the right mouse click is intercepted, similar to a $ call would work with a keyboard press. This is the code I've tried but the right click still is sent to the program.
$capslock::
send {u down}
var = 0
return
$capslock up::
send {u up}
var = 1
return
$rbutton::
if(var) {
;
} else
sendinput, {s}
return
Any help would be appreciated!

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.

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
}