Autohotkey: replace key combination (but with timeout) - autohotkey

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

Related

Get First Character of A_LoopFileName?

I'm trying to parse a filename and get the first character from it as a string to compare it to a previously inputted variable. My code looks like:
FileSelectFolder, WhichFolder ; Ask the user to pick a folder.
; Ask what letter you want to start the loop from
InputBox, UserInput, Start At What Letter?, Please enter a letter to start at within the folder (CAPITALIZE IT!)., , 450, 150
if ErrorLevel {
MsgBox, CANCEL was pressed.
ExitApp
} else {
inputted_letter = %UserInput%
tooltip %inputted_letter% ; Show the inputted letter
sleep, 2000
tooltip
}
Loop, %WhichFolder%\*.*
{
current_filename_full = %A_LoopFileName%
files_first_letter := SubStr(current_filename_full, 1, 1)
tooltip %files_first_letter% ; Show the file's first letter
sleep, 2000
tooltip
if files_first_letter != inputted_letter
continue
...
Right now, it clearly shows in the tooltips the user-entered capital letter, and then the first letter of each file name from within the selected folder, but for some reason when the two look alike, it doesn't recognize them as a match. I'm thinking maybe because technically A_LoopFileName is not of a string type? Or maybe the inputted letter doesn't match the type of the first filename's letter?
I want it to continue if the inputted letter and the first letter of the filename don't match, but if they do, to carry on with the rest of the script. Any ideas on how I can get these two to successfully match? Thanks!
Firstly, AHK doesn't really have types. At least not how you've experienced types in other languages.
So your assumption about "not being correct type" will pretty much always be wrong.
So the actual cause is because in a legacy if statement, the syntax is
if <name of variable> <operator> <legacy way of representing a value>
So you'd do it like this:
if files_first_letter != %inputted_letter%
You we're comparing if the variable files_first_letter is equal to the literal text inputted_letter.
However, I highly recommend you stop using legacy syntax. It's really just that old.
It'll differ horribly much from any other programming language and you run into confusing behavior like this. Expression syntax is what you want to use in AHK nowadays.
Here's your code snippet converted over to expression syntax in case you're interested:
FileSelectFolder, WhichFolder
;Forcing an expression like this with % in every parameter
;is really not needed of course, and could be considered
;excessive, but I'm doing it for demonstrational
;purposes here. Putting everything in expression syntax.
;also, not gonna lie, I always do it myself haha
InputBox, UserInput, % "Start At What Letter?", % "Please enter a letter to start at within the folder (CAPITALIZE IT!).", , 450, 150
if (ErrorLevel)
;braces indicate an expression and the non-legacy if statement
;more about this, as an expression, ErrorLevel here holds the value
;1, which gets evaluated to true, so we're doing
;if (true), which is true
{
MsgBox, % "CANCEL was pressed."
ExitApp
}
else
inputted_letter := UserInput ; = is never used, always :=
Loop, Files, % WhichFolder "\*.*"
;non-legacy file loop
;note that here forcing the expression statement
;with % is actually very much needed
{
current_filename_full := A_LoopFileName
files_first_letter := SubStr(current_filename_full, 1, 1)
if (files_first_letter != inputted_letter)
continue
}
Also you don't have to be concerned about case with !=, it'll always compare case insensitively.

How to have "input" (or is it context?) dependent hotstrings/hotkeys ?

I want a hotkey or hotstring (whatever is easier), so I can easily convert e.g.
1:5 into [1,2,3,4,5] or
3:7 into [3,4,5,6,7] etc..
I want this to work for all integers...
So I want "multiple variants of the same hotstring" (or, if easier: a hotkey that works somewhat similar: e.g. pressing strg + h and typing 1:3 should produces [1,2,3] )
It should recognize that I typed a number followed by colon followed by another number, and then expand correspondingly..
I looked into the Input function, but it does not seem to be exactly what I want..
I don't need a working solution. Hints & links or keywords for further googling are already helpful..
After typing +h or pressing strg+h, type two numbers to produce the desired outcome:
:*:+h::
^h::
nr := "" ; empty variable's content
end_nr := ""
Input, var, L2 ; Length limit=2
; Input, var, L2 V ; V: Visible
If var is not integer
{
MsgBox, "%var%" is not integer
return
}
first_nr := SubStr(var, 1, 1)
second_nr := SubStr(var, 0)
if (first_nr >= second_nr)
{
MsgBox, "%first_nr%" is greater or equal "%second_nr%"
return
}
Loop
{
nr++ ; increase the number in the variable "nr" by 1 in each iteration
if (nr < first_nr)
continue
If (nr = second_nr)
break
end_nr .= nr . "," ; concatenate the outputs by adding a comma to each one
}
If (first_nr = 0)
MsgBox, "0,%end_nr%%second_nr%"
else
MsgBox, "%end_nr%%second_nr%"
return

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 KeyWait statements

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"
}

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.