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

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

Related

How to send a key repeatedly in Autohotkey

I want to write an AutoHotkey script which loop a key X number of times.
For example, here's is a script which overwrites the function of ENTER key with function of F2 key in File Explorer.
#IfWinActive ahk_class CabinetWClass
Enter::
Send, {F2}
#IfWinActive ahk_class CabinetWClass
Enter::
Send, {ENTER}
#IfWinActive
The goal is to press ENTER to rename a select file, and then press ENTER to confirm the rename. Pressing ENTER on the same file that have just been renamed should send F2 key again (In case there is typo error).
Currently the second block doesn't work as I'm sending the same key, how to fix this?
The KeyWait command is your friend in this case.
There is still room to improve on how you handle the second Enter
#IfWinActive ahk_class CabinetWClass
$Enter::
sleep,100 ; giving time to detect the first Enter
Send, {F2}
Keywait, Enter, T5 D ; wait 5 seconds for the Enter to be pressed down
If (ErrorLevel == 0)
{
Send, {Enter}
sleep 200
Send, {F2}
}
else
{
traytip, , timeout ; Enter was not pressed down in 5 seconds
}
return
Basically, it appears you're trying to assign different tasks to the same hotkey and due to this being done seperately ahk is selecting one of the tasks and running with that task and only that task. If loops can be used within hotkeys, so I would suggest using this to rotate between the two expected outcomes. Please see example below:
temp:= 1
enter::
if(temp==1)
{
Send, {ENTER}
temp:=2
}
else if(temp==2)
{
Send, {F2}
temp:=1
}
return
1::
Temp:=1
return
2::
temp:=2
return
^x::ExitApp
I also added in hotkeys for 1/2 to allow you to manually decide the outcome rather than it being specifically assigned in the case of any issues.
Oh, and ctrl+x to close the macro.
You're trying to rebind the enter key twice.
Rebinding a key is like saying "When I press this key, do this:" - in this case it's under an #IfWinActive so it's more like "When this window is open and I press this key..."
When you break that down you have "When I press enter - press F2" as well as "When I press enter, press enter"
What you're wanting to achieve is make the rebind conditional - i.e. it only sends F2 under certain conditions.
It's hard to know how to help without more context. Is there any reason you can't use a different key combination? Like Ctrl + Shift + Enter?
Something like:
+^Enter::send, {F2}

Trigger double press when key held

I am new to stackoverflow and apologize in advance if what I am trying to explain is unclear.
I have tried multiple ways to make this work but have had no success so far.
I am trying to achieve the following:
When F3 is held and left arrow is pressed, left arrow will be pressed twice with no pause (0sec).
When F3 is held and right arrow is pressed, right arrow will be pressed twice with no pause (0sec).
Here's an alternative to Blauhirn's
F3::
While (GetKeyState("F3", "P")) {
If (GetKeyState("Left", "P"))
SendInput, {Left}
If (GetKeyState("Right", "P"))
SendInput, {Right}
}
Return
Alternatively you don't need to Loop to send multiples of of the same key.
You can simply use SendInput, {Left 4} the number represents the number of times that key will be sent.
Edit:
Oops, I didn't address the issue of delay between key presses. So I changed Send to SendInput as that has no delay between key presses.
~F3 & ~left::
send {left}
return
does this work?
This means, that as soon as f3 and left is pressed together, left will be sent a second time. If you want to repeat the send {left}command, use it like
loop, 4 ; 4 times
{
send {left}
}

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}

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

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

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}