AutoHotKey: Send a button when another is held down, and after released - mouse

in AutoHotKey
I want to write a script that will press a button once when the right mouse button is held
and press another once its released.
I tried writing something (I used numpad0 instead of mousebutton)
Numpad0::
Send {d}
Numpad0 Up::
Send {u}
but, it keeps sending du all the time, instead of just d and a final u.
why is that?

If you're putting your hotkey command on a different line to where the hotkey is declared you need to use a return statement to end it:
Numpad0::
Send {d}
return
Numpad0 Up::
Send {u}
return
You can also just declare each hotkey on one line without a return if you're not trying to do too much:
Numpad0:: Send {d}
Numpad0 Up:: Send {u}

Related

AutoHotkey - Script stops working after a while

I'm using a simple script, all working fine as I want, but after switch a couple windows or after a while it just stops working.
#InstallKeybdHook
#IfWinActive ahk_exe Figma.exe
$!WheelUp::
Send {Control down}
$^WheelUp::
Send {WheelUp}
SetTimer,ControlUp,-300
Return
$!WheelDown::
Send {Control down}
$^WheelDown::
Send {WheelDown}
SetTimer,ControlUp,-300
Return
ControlUp:
Send {Control up}
Return
#IfWinActive
Is there anything I am missing?
I'm trying to swap keys only when I'm using Figma. I want when I press ALT+mouseUp sent to the software CTLR+MouseUp, and ALT+mouseDown send CTRL+mouseDown.
I want when I press ALT+mouseUp sent to the software CTLR+MouseUp, and ALT+mouseDown send CTRL+mouseDown.
If this's all you need, then wouldn't a simpler code do the job? Do you have any other unspoken needs?
!WheelUp::SendInput, ^{WheelUp}
!WheelDown::SendInput, ^{WheelDown}
The timer can be interrupted by other threads, i.e. another timer subroutine or a hotkey subroutine. When we send keyboard and mouse commands fast, subroutines ControlUp that are constantly being created are constantly being interrupted by newly created timer subroutines and new hotkey subroutine. In the end, these backlogged subroutines, which are postponed running, may not send {Control up} at the timing we expect, which makes script not working as we expect.

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

If mouse is clicked, do one thing, if mouse is held down, do the normal thing

I want my code to be able to register if the mouse is clicked, and do something when that happens, but also to be able to register if the mouse is held down and not interrupt the mouse being held down if that is the case. For example,
If AutoCAD is open
If mbutton is clicked
click the escape key
If mbutton is held down
be able to use the mbutton held down as usual
End
I've tried a couple of different ways to do this but I don't have the knowledge to do this exactly. I've got the "If AutoCAD is open", and the "click the escape key" parts down, just not the "use the mbutton as normal if held down part"
Thanks for any help you can provide!
This was a bit of a tricky one. Change the #IfWinExist line accordingly. You can adjust the duration to be what you would consider "holding" MButton
SetTitleMatchMode, 2
#IfWinExist AutoCAD
~MButton::
duration := 100
start := A_TickCount
While(GetKeyState("MButton"))
{
if ((A_TickCount - start) > duration)
{
KeyWait, MButton
Send {MButton Up}
Return
}
}
Send, {Escape}
Return
#IfWinExist AutoCAD
If I understand you right, you are searching for the UP keyword:
The word UP may follow the name of a hotkey to cause the hotkey to
fire upon release of the key rather than when the key is pressed down.
(documentation)
#if autocad()
MButton Up::
send {escape}
return
#if
autocad() {
return true
}

Sending and releasing multiple keys at once using AutoHotkey

I'm attempting to send three keys (Alt, Shift, Q) at same time using this script:
:*:pk:: ;
Send, {AltDown}{ShiftDown}{qDown}
return
When I run this is it does not release the keys, it seems like the Alt button remains pressed. After the above keys are pressed I then want to press the "q" character again (separately, not at same time).
How can I amend my script above to achieve this?
When using Down, you must also send an Up to the same key or else it will remain pressed. This can be achieved like this:
:*:pk::
Send, {Alt Down}{Shift Down}{q Down}{Alt Up}{Shift Up}{q Up}
Send, {q}