AutoHotKey KeyWait statements - autohotkey

I'm using AutoHotKey and I want to achieve something particular.
I have an hotkey that should perform a certain action, inside this hotkey, I would like to code something to detect if I only press the "C" key, or if I press "C" then "L" keys.
If there is only the "C" key pressed, then it should perform an action, otherwise , if "C" then "L" keys are pressed it should do another action.
But I can't do this as I don't really understand KeyWait, I mean how can I do something like that :
if(KeyWait, C){
firstAction
else {
if(KeyWait, C){
if(KeyWait, L){
anotherAction
}
}

Solved using the input function.
; Get one character and store it in the Character variable
Input, Character, L1
If Character = C
{
; Give up to half of one second to type L
Input, Character, L1 T0.5
If Character = L
MsgBox % "We have C and L"
else
MsgBox % "Just C here, give an L next time"
}

Related

Autohotkey: replace key combination (but with timeout)

When I type, I want my script to replace:
"ab" to "x"
"ac" to "y"
"a[more than 300ms]b" - don't replace, just leave "ab"
So, it should replace only if time between "a" and "b"/"c" is less than 300ms
tried doing this:
a & b :: Send x
a & c :: Send y
But it obviously doesn't 'forgive' me any delay
Would be great to get any hints, thanks!
A hotstring is usually good for replacements. To add that delay, here's a simple thing I could come up with:
~*a::APressTime := A_TickCount
:*B0:ab::
if(A_TickCount - APressTime <= 300)
SendInput, % "{BS 2}x"
return
First a simple hotkey for A that just saves the current system time with A_TickCount.
Then a hotstring to which I set some options you're likely going to want to use:
* so you don't need to type an ending character.
B0 so it doesn't backspace the letters ab automatically.
You might also want to use the ? option so the hotstring also works while "inside of a word". Add it in if you want.
Then we compare the current system time to the time when a was last pressed. If it's 300ms or less, send backspace twice followed up by x.
({BS 2} is same as {Backspace 2} or {Backspace}{Backspace})
To do the same with ac, just copy paste the hotstring again and switch b with c, and x with y.
Alternatively, here's something a bit more fancy to put them in the same hotstring label with a ternary:
:*B0:ab::
:*B0:ac::
if(A_TickCount - APressTime <= 300)
SendInput, % "{BS 2}" (SubStr(A_ThisHotkey, 0) = "b" ? "x" : "y")
return

Autohotkey: Set input length to 'any'

I am scanning 4 barcodes that sends as N123P123Q12V123456 with different length on the numbers. When I scan it splits the string correct at the characters, and outputs it with the enter as it should. But I can't press any key after the scan, let's say that I want to write something manually - it does not work with this code.
I would also like to add F1, delay, F4 at the end of the string (after V-string). Is it just Send {F1} ?
Loop
{
Input, OutputVar, L26 M
if (RegExMatch(OutputVar, "iO)([A-Z]\d+)([A-Z]\d+)([A-Z]\d+)([A-Z]\d+)", Output))
for k,v in % StrSplit(RegExReplace(OutputVar,"(?<=\d)[A-Z]\d+","|$0"),"|") {
send, % v "{Enter}"
Sleep, 500
}
}

Can I list several keys to perform the same action? [AHK]

AHK allows to bind keys, that is us a::z t fire 'z' whenever 'a' is pressed.
What if I want to fire 'z' whenever 'a', 'b', or 'c' is pressed?
I can obviously repeat my code:
a::z
b::z
c::z
I can probably use a Gosub like
a::Gosub, abc
b::Gosub, abc
c::Gosub, abc
abc:
send z
return
Is there a better way to say "if a,b, or c are pressed - fire z"?
You can just use
a::
b::
c::z
i am not sure what is the exact synthax, but this works.
We're at codegolf.stackexchange.com, right?
JFF, here's assigning A-Y to Z with just 61 characters, using the Hotkey command:
loop,25
hotkey,% chr(a_index+64),z
return
z(){
send z
}
Another solution using Hotkey to define hotkeys on the fly, and parse so that the user can directly specify a list of keys:
; Thanks engunneer: autohotkey.com/board/topic/45636-script-to-prevent-double-typing/?p=284048
; Thanks throwaway_ye: https://www.reddit.com/r/AutoHotkey/comments/54g40q/how_can_i_bind_several_keys_to_the_same_command/d81j0we
; The following part must be located in the auto-execute section,
; i.e. the top part of the AHK script.
keylist = 1234567890qwertzuiopasdfghjklyxcvbnm
Loop, parse, keylist
{
Hotkey, $%A_LoopField%, SendGivenKey
}
Return
; This can be located anywhere in the AHK file
SendGivenKey:
StringReplace, key, A_ThisHotkey, $, , All
send %key%
Return

AutoHotkey repeated key sequences, detecting KEYUP and KEYDOWN for modifiers

Similar to what this question covers, I'm trying to bind two sequences of keys.
Ideally I'd like to bind Alt DOWN,-,-,-,Alt UP to an em-dash (—) and Alt DOWN,-,-,Alt UP to an en-dash (–).
What I have almost works for em-dashes but not quite:
; Em-dash
!-::
Input Key, L1
if Key=-
Input Key, L1
if Key=-
Send {ASC 0151}
return
; En-dash
;!-::
;Input Key, L1
;if Key=-
;Send {ASC 0150}
;return
The em-dash sequence works like Alt+-,-,-, instead of what I'm trying to match. I'm not sure how to only test for Alt DOWN and Alt UP. The en-dash sequence fails altogether to bind because !- has already been bound.
Have a look at this one:
dashCount := 0
!-::
dashCount++
if(dashCount = 1) {
SetTimer, WaitForAlt, -1
}
return
WaitForAlt:
KeyWait, Alt
if(dashCount = 2) {
Send {ASC 0150}
} else if(dashCount = 3) {
Send {ASC 0151}
}
dashCount := 0
return
It seems to do the job well. The code works by counting each time Alt + - gets pressed. Concurrently, a pseudo-thread is spawned that waits for Alt to be released and then sends the appropriate dash, depending on the counter.

Konami Code on autohotkey

up up down down left right left right b a enter :: Msgbox, konami code.
is there a way to do this?
yes its actually pretty simple...
comb := "up|down|down|left|right|left|right|b|a|enter"
~up::
Loop, parse, comb, |
{
input, var,T.900,{%a_loopfield%}
if inStr(ErrorLevel, "Timeout")
return
}
msgbox Konami Code!!!
return
The first "up" is the one that will trigger the sequence hence only one "up" in the combination variable.
you can change the combination to whatever you want, but then you would have to change the hotkey to the first "key" that you want to press.