Autohotkey: I'm unable to make a script for pressing 2 keys with a delay beteween them - autohotkey

Well i want to make a script with the objetive:
3{DOWN} key, and later hold or quickly press Z x3, and loop all that.
I have been trying to work with loop command but i just can't, im new with AutoHotkey and english it's not my native languague so it's been pretty hard.
Here is the code i tried but didn't work as i expect, since it press Z before the 3 {DOWN} keys.
#Persistent
SetTimer, Code, 150
Return
Code:
Send, Z{DOWN}
Return
If you know anyway to improve what i'm doing like, add a toggle like F8 to turn on/off, it would be aweosome.
Thanks for any help.
Helena.

Helena, What your script does right now is the following. As soon as the script starts it will start to send [Z] and [Arrow down] every 150 mili seconds. This is independent of what application is running at that time.
You write that you want to loop sending codes and that you want to toggle this ON/OFF.
Here is an example that comes closer to your goal.
#Persistent
F8:: ; This is your [F8] Toggle hotkey
If Toggle:=!Toggle ; Here you "test" the value of the variable "toggle" and after testing switch it to the opposite (true/false)
SetTimer, Trigger, -1 ; This is to create a separate thread for the loop. -1 means start in 1 ms but only do this one time, not every 1 ms's.
return
Trigger:
While (Toggle)
{
Send, +z{Down} ; + is the shift key, thus +z makes captial Z
Sleep, 500 ; Wait 500 ms (1/2 a second)
Send, +{z Down} ; Press Shift z Down. This will NOT start a repeat like ZZZZZZZZZZZZZZZZ
Sleep, 500 ; Wait 500 ms (1/2 a second)
Send, +{z Up} ; Lift Shift z Up
Sleep, 1 ; Required. Without it The F8 keypress to toggle off can not be read anymore
}
Return

Related

AutoHotKey, Repeat key press

All I want is a simple keypress of lowercase "v" 4 times with a 1 second delay between. I have tried so many iterations of this simple-sounding action to no avail and the most infuriating thing is that I can get it to work with a capital "V" – but that also emulates a shift+v key press. I just don't understand.
v::
Loop, 4
{
sleep 10
SendInput {V} ; capital V works perfectly fine but includes Shift+v, I don't want a shift
sleep 1000
}
return
Whereas ...
v::
Loop, 4
{
sleep 10
SendInput {v} ; Lowercase v, Does absolutely nothing...
sleep 1000
}
return
What am I missing?
Try
$v::
Loop, 4
{
sleep 10
SendInput v
sleep 1000
}
return
The $ prefix works by forcing the keyboard hook to be used and prevents the Send command from triggering the hotkey itself.
Alternative #UseHook can be specified somewhere above, so the keyboard hook is implemented for every hotkey after it
Another solution:
#IfWinActive ahk_class your_app_ahk_class
$v::
SetKeyDelay, 1000
Send {v 4}
Return
I don't know what's the purpose of this, but since the shortcut does not include any control keys, it might be better to limit this to a specific app. The first line in my snippet tries to achieve this.

Different actions at different release times

So i got this code which is working almost fine.. Its maybe a good starting point if u can help me.:
*LButton::
Send A
KeyWait, LButton, T1
If ErrorLevel
Send C
Else
Send B
KeyWait, LButton
Return
What the code does it defines whats gonna be the release action, after i let go the LMB (left mouse button) and when.
If i release the LMB under 1 sec its gonna send A B. If i release it after 1 sec it should send A C, which it does but not after i release the LMB. It sends C right after the 1 sec is gone. I want to send C only after i release the LMB.
And if the LMB was released after 1 sec than i want to block the LMB for x time. So i coudnt hit it again.
But if it was released before 1 sec it should work normally.
English was never my bright side.. :D, hope u can understand thx.
Assuming:
When you said "And if the LMB was released after 1 sec than i want to block the LMB for x time. So i coudnt hit it again", you meant that you wanted it so that the clicking the LMB wouldn't do any action during the block duration.
You wanted it so that only if the second action was triggered (the one that sends a 'C' after a certain amount of time has passed), the block would be activated. This would allow you to repeatedly click the mouse to send A B, but you would have to wait for a second before you can send anything after A C.
Then the solution I came up with involves a toggled variable that determines whether the hotkey will do anything if the LMB is pressed. The toggle variable, switch, is set to disable mode after the A C part of the script activates, but after one second (1000 milliseconds), the variable goes back to the "enabled" state. [For reference, when switch is 1, the hotkey is enabled, but if it is 0, it is disabled.]
Code:
switch := 0
*LButton::
if(!switch){
Send A
KeyWait, LButton, T1
If ErrorLevel{
Send C
gosub, tempBlock
}
Else
Send B
KeyWait, LButton
Return
}
else
return
tempBlock:
switch:=1
SetTimer, reset, -1000
return
reset:
switch:=0
return
Very easy to do if you don't try to mess around with KeyWait.
The example below is still missing your "blocking" thing. I don't really know what you meant by it.
If it's something you won't know how to add yourself, I can add it in after you explain it better.
*LButton::
ClickedAt := A_TickCount
SendInput, A
return
LButton Up::
if (A_TickCount - ClickedAt <= 1000)
SendInput, B
else
SendInput, C
return
So LButton:: is for the key being pressed down. and LButton Up:: is for the key being released.
On the down press we store the current system uptime from the built-in variable A_TickCount(docs) and on the key release we compare the current system uptime to the stored one to see how long has passed.
SendInput is also used instead of traditional Send, due to SendInput being the recommended faster and more reliable send mode.
Bonus, two liner (because why not):
*LButton::SendInput, % ("A", ClickedAt := A_TickCount)
LButton Up::SendInput, % (A_TickCount - ClickedAt <= 1000) ? "B" : "C"

Autohotkey script to send space when tapped and shift when held. It is affecting other mappings

I wrote this script to make my space-bar button act as a space-bar when tapped and a shift button when held. However, it affects mappings such as
::btw::by the way
Scenario:
when I tapped space-bar after typing "btw" to convert "btw" into "by the way ", the conversion did not take place.
Is there anyway that I can change my script below to ensure that conversion in the above scenario happens?
$Space::
now := A_TickCount
while GetKeyState("Space", "P") ; to find out whether space-bar is held
if (A_TickCount-now > 180) ; this time is tested on asker's computer
{
SendInput {Shift Down}
KeyWait, Space
SendInput {Shift Up}
return
}
SendInput {Space} ; if key detected to be tapped, send space as per normal
return
Thanks :)
Hotstrings ignore events generate by Autothotkey if their send level is equal or lower than the send level of the event.
This means that the send level of Space, must be set to a higher level than the level of hostrings you wish to trigger with it:
#InputLevel, 10 ;set send level for the following code to 10
$Space::
#InputLevel ;set it back to default value of 0 for any remaining code
now := A_TickCount
while GetKeyState("Space", "P") ; to find out whether space-bar is held
if (A_TickCount-now > 180) ; this time is tested on asker's computer
{
SendInput {Shift Down}
KeyWait, Space
SendInput {Shift Up}
return
}
SendInput {Space} ; if key detected to be tapped, send space as per normal
return
you have to change the auto-replace code for btw to
:*:btw::by the way
instead of
::btw::by the way
this will auto-replace btw to by the way even if you have the mentioned hotkey for space
#Neo Ding Yue Is it working? Initially I did it like you, but it had many flaws. Finally I found a solution which I am using since two years: 1. registry remap space to behave like LShift and use this code:
~*LShift:: ;tilde so it gets triggered already on down, in combination with any key, hence can be used as modifier key
Keywait,LShift, L ; just to deactivate autofire
return
~LShift up::
IF(A_TimeSincePriorHotkey < 150 && A_PriorKey = "LShift") {
SendInput {Space}
}
return
If you have question, just ask (I have other versions).

AHK Prevent Multiple Hotkey Triggers

It's a quite simple code.
I just want my Mousewheeldown to Send P only once.
Even if I scroll it like 3 times, I only want it to send P only once every 100ms or sth.
Here is my really small bit of code so far:
SetKeyDelay , -1, 50
#NoTrayIcon
#NoEnv
#persistent
#MaxMem 2
WheelDown::
Send {p}
return
Sleeping a bit after sending the keystroke would solve this problem.
WheelDown:
Send, p
Sleep, 100
return
Also, you do not need to put the p between {}-s (curly braces), as it is not a special key.
The sleep command takes it's parameter as milliseconds, so if for example you would want to allow it only one 'p' in a second, you would write Sleep, 1000.

Holding control, shift and v, using only one key

I am using lightroom for this hotkey. I dont know why is this doesn't work for me.
::{control}{sleep 5}{shift}{sleep 5}v
And Alt+F+↓(5x) then Enter.
And Alt+F+↓(5x) then Enter.
I don't know what you mean by that, because I can't see anything like that in your script. So I will just ignore it and do what the thread title and your script says/implies.
The following script will simulate pressing Ctrl+Shift+V when you press the A key
SetKeyDelay, 5 ;wait 5 milliseconds between every keystroke
a::^+v
About your comment, I guess I know what you are on to, try this:
SetKeyDelay, 5 ;wait 5 milliseconds between every keystroke
2::
SendInput, !f
Sleep, 50
SendInput, {Down}{Down}{Down}{Down}{Down}{Enter}
Return