AHK Hold Button Toggle - autohotkey

I'm looking to make the right mouse button send a single keystroke on DOWN, and another separate, single keystroke on UP.
"Pseudo-code" example, where pressing down right mouse button toggles an eyedropper tool by sending its hotkey (i), and releasing right mouse button sends the hotkey for brush (b):
RButton down::
Send i ; Send eyedropper hotkey
RButton up::
Send b ; Send brush hotkey
I understand from other questions asked that a while loop using GetKeyState could get part way there, but the while loop causes the key to fire rapidly, which is not desired.
How do I make the right mouse button send its corresponding up/down hotkey only once on up/down?

; RIGHT BUTTON HOTKEY
RButton::
GetKeyState, state, RButton, P ;GET RIGHT MOUSE BUTTON STATE
if state = D ;IF BUTTON STATE = DOWN
{
SetTimer, CheckRButtonState, 10 ;SET TIMER TO MONITOR WHEN IT WAS RELEASED
Goto, RButtonDown ;JUMP TO SUBROUTINE FOR BUTTON STATE = DOWN
}
return
; TIMER
CheckRButtonState: ;TRIGGERED BY TIMER IN EVERY 10 MILLISECONDS
GetKeyState, state, RButton, P ;GET RIGHT MOUSE BUTTON STATE
if state = U ;IF BUTTON STATE = UP
{
SetTimer, CheckRButtonState, Off ;STOP TIMER
Goto, RButtonUp ;JUMP TO SUBROUTINE FOR BUTTON STATE = UP
}
return
; SUBROUTINE FOR RIGHT BUTTON PRESSED
RButtonDown:
TrayTip,Down, %state% ;PUT HERE THE TASKS YOU WANT IT TO PERFORM WHEN BUTTON IS DOWN
return
; SUBROUTINE FOR RIGHT BUTTON RELEASED
RButtonUp:
TrayTip,Up, %state% ;PUT HERE THE TASKS YOU WANT IT TO PERFORM WHEN BUTTON IS UP
return

Related

AHK script to change RButton Behavior

I want to make a script that would allow me to click once every time I press the mouse, however, if instead of letting go immediately, I hold the RMB for more than 0.25s it would click again on release.
Essentially allowing me to use RMB normally as long as I don't hold it for too long but allowing me to do a double click if held.
This is a work around since my mouse's button gets stuck if I click too fast and I'm not able to get a new one atm.
The purpose of this is to be able to use my mouse on PS and to play the one game i play some times: Black Ops 2 while i save enough for a new mouse. In the context of the game, i want to be able to use Toggle ADS as a base and be able to use the toggle by default by just clicking but be able also do a Hold to ADS with the same button on the fly without changing the game's configuration.
I am not very proficient at AHK and this is all I got so far, holding works fine, however if I don't hold, it does a double click which is annoying.
RButton Down::
Send {Click, Right}
keywait RButton, t.25
if errorlevel
keywait RButton,
Send {RButton Up}
return
You could maybe do some trickery with multiple KeyWaits, but I wouldn't recommend time consuming processing inside hotkey labels in any case due to AHKs single threaded nature.
Here's something very simple I'd recommend instead:
~*RButton::RClickTime := A_TickCount
~*RButton Up::
if (A_TickCount - RClickTime >= 250)
Click, Right
return
So first when we press down RButton (note that there is no "down" state for hotkey names, the key name itself means it being pressed down) we store the current system time with A_TickCount.
And we use the ~ modifier for the hotkey so the keypress itself isn't consumed.
And the * modifier is used so holding down e.g. Ctrl or Shift, etc, wouldn't make the hotkey not work.
Then on RButton release (RButton Up::) we compare the current system time with the stored system time to see if over 250ms passed. If so, we send another right click with Click, Right (don't use a send command here, it isn't really intended for this).
It looks like you're missing some curly braces around your if block; but I think you can implement the right double-click functionality with something as simple as,
RButton::
KeyWait RButton, T.25
numberOfClicks := errorLevel + 1
Send {Click Right %numberOfClicks%}
return
~RButton:: ;*When Right Mouse Button is Down, do the following.*
keywait RButton, T.25 ;*Wait (250 milliseconds) for it to be released.*
if errorlevel { ;*When it exceeds the said time*
keywait RButton ;*Wait for it to be released*
Send, {RButton Up} ;*Send Right Mouse Button up*
}
return

AHK Script to send two button presses on one button press+release

Hi im trying to make an AHK Script that sends a button press twice, once when i hold the button down and the second time when i release the button on my keyboard. So far i've only figured out the opposite of making any button a toggle, however that doesn't really help me D: any help appreciated!
The $-prefix prevents the hotkey from triggering itself, because it forces the keyboard hook to be used.
$a::
SendInput, a
KeyWait, a ; wait for the key to be released
SendInput, a
return
For more than one keys, you can use:
#NoEnv
#SingleInstance Force
SendMode Input
#InstallkeybdHook
#UseHook ; prevents the keys from triggering itself
keys := ["a","b","c","d","1","2","3","4"] ; ....
for each, key in keys
hotkey, %key%, send_key_twice, on
return
send_key_twice:
Send, %A_ThisHotkey%
KeyWait, %A_ThisHotkey% ; wait for the key to be released
Send, %A_ThisHotkey%
return

Press both mouse buttons when holding left click

When I hold down left mouse button I would like for the right mouse button be held down also.
The right mouse would then release when the left mouse is no longer being held. So both buttons can be controlled simultaneously with the left click.
LButton::RButton means Left Button acts like your Right Button. The ~ modifier means "Fire the hotkey as well as whatever it's remapped to".
~LButton::RButton
Quick edit: If you need it to activate after Left Button has been held for a certain amount of time, use this:
; Time for LButton to be held down before RButton is sent (in milliseconds)
waitTime := 500
return
~*LButton::
while GetKeyState("LButton", "P")
if (A_TimeSinceThisHotkey > waitTime){
Send, {RButton Down}
KeyWait, LButton
Send, {RButton Up}
}
return

Conditionally intercept a mouse click in Autohotkey?

I want to have a script which will intercept a mouse click and send a key press instead, but only when the capslock key is toggled on. I want the mouse click to be sent normally if the capslock key is toggled off.
Currently I have made this:
$LButton::
if GetKeyState("CapsLock", "T") = 1
send, {a}
else
send, {LButton}
return
The problem with this is that when the capslock key is off, the left button can click perfectly normally but it cannot drag.
If I change $ to ~, it is able to drag but it also performs a click when the capslock key is toggled on.
Is there any way to make the script ignore the click completely if the capslock key is toggled off?
AHK_L's #If will give you what you want:
#If GetKeyState("CapsLock", "T")
LButton::Send, a
With this code, you won't have to bother what happens when capslock is off. AHK will intercept the click on a lower level and let it trickle through.
How to use the symbol UP.
SetBatchLines, -1 ; you pretty much have to include this to speed up the execution
LButton::
if( GetKeyState("CapsLock", "T") )
tooltip, ignore left click
else
send, {LButton Down}
return
LButton UP::
send, {LButton Up}
return

script to use Left Click as Left Shift does not work correctly

I've search AHK forums but couldn't solution.
The script I found on AHK to use LShift as LButton does not work correctly..when press & hold left shift and move the mouse pointer, it can't copy text exactly like as if click and hold left mouse. What I mean by exactly is.. press & hold left shift and move the mouse pointer copies the whole paragraph or section of text..not just the few words I wanted.
My first time here & newbie w/ AHK. Your time is appreciated.
~Lshift::
Send {LButton}
return
F10::exitapp
you could try:
~Lshift::
Send {LButton Down} ; Press the LButton key Down
KeyWait, LShift ; Wait for the LShift key to be lifted
Send {LButton Up} ; Release the LButton key
return
F10::exitapp