Verifying pressed keys in AutoHotKey - autohotkey

I've been searching for quite a while and took a look at alot of documentation on AHK, but haven't quite found out how to do this.
So basically, anytime CTRL + another key is pressed, I want to check what the other key is. When the other key is not one of the numpad numbers, I want to only send the other key. When the key is one of the numpad numbers, I want to send CTRL and the numpad key.
Thanks in advance!

I can only think of two ways. Firstly, assigning every hotkey manually.
loop, 255 {
char := chr(a_index - 1)
try {
hotkey, ^%char%, send_key, char
} catch e {
; many keys in the ansi chart cant be assigned to a hotkey, just ignore the error
}
}
return
send_key:
; A_ThisHotkey contains the pressed combination in a format like ^C. Remove the ^
key := subStr(a_thisHotkey, 2)
sendraw %key%
return
The loop iterates over all ANSI characters. See https://msdn.microsoft.com/en-us/library/aa245259(v=vs.60).aspx for a list. It is only a short form for
^a::send a
^b::send b
etc., 255 times. This approach doesn't affect space, numpad numbers (which you want), # and many more, and it also cannot differentiate between lowercase and uppercase letters. But maybe it'll do for you.
Secondly, using input. Read the doc page about it and if you still struggle, edit your question I guess. I don't know if or how this will work for key combinations

Related

AHK using three or more key presses to trigger hotkey/hotstring

I know this question has been discussed before (several years ago). I am hoping there is a new way to achieve this goal. I would simply like to use three or more keys to trigger an AHK script. For example, CTRL + SHIFT + Q to do a thing
In "fake" ahk, this would be
^+Q::
...
return
Of course, this type of command errors. The previous solution is quite verbose compared to the task and involves checking the state of some key etc. Is there a shorthand way of accomplishing this?
Please describe the error you get. The example you give does not produce an error for me.
But for one thing, a shift modifier on "Q" has no add'l effect (a Shift on "q" gives "Q").
But in the meantime, AHK is quite forgiving and any of these work:
^+Q::
msgbox %A_ThisHotkey%
return
^Q::
msgbox %A_ThisHotkey%
return
^+q::
msgbox %A_ThisHotkey%
return
And in general two or even three modifier keys (Ctrl+Alt+Shift) is quite normal in practice. if you want two or more non-modifier keys, you will have to track key state. I guess you know that from your reference to what was "discussed before (several years ago)."

Conditional Key Remapping AHK

I would like to remap the 'j' key so that it presses n when ergo is true, or y when it is false with AutoHotKey. When I remap normally using "j::n" for example, shift+j outputs a capital N, and so do other modifiers with the 'j' key. However, my code below only works when the letters are pressed without modifiers. Is there a way to get around this, and conditionally remap keys with AutoHotKey?
j::
if (ergo) ;inverted use of the ergo variable to make the code more efficient
Send {n}
else
Send {y}
return
You only want to wrap characters which have a special meaning in a send command in { }. Basically escaping, if you're familiar with that what is.
So you don't want to wrap n or y in { }. It can even result in undesired behavior.
There are quite a few approaches for this. Can't say which is best, since don't know what your full script is like.
I'll present two options that I'd guess to be most likely best approaches for you.
Firstly, the send command way like you tried. Just doing it right:
*j::
if (ergo)
SendInput, {Blind}n
else
SendInput, {Blind}y
return
So, usage of the *(docs) modifier so the hotkey works even if extra modifiers are being held down.
And then usage of the blind send mode so which ever modifier keys you may be holding when triggering the hotkey will not be released.
Also switched over to SendInput due to it being the recommended faster and more reliable send mode.
Second way would be creating a context sensitive hotkey with #If(docs).
#If, ergo
j::n
#If
j::y
This is a convenient and easy approach. But could possibly result in other problems.
Why? Well #If comes with some downsides which you can read more about here, but long story short:
You likely wont have any trouble unless you have a more complicated script.
When I remap normally using "j::n" for example, shift+j outputs a capital N, and so do other modifiers with the 'j' key. However, my code below only works when the letters are pressed without modifiers.
Looks like you are looking for the Wildcard * modifier.
From the docs:
Wildcard: Fire the hotkey even if extra modifiers are being held down. This is often used in conjunction with remapping keys or buttons.
So after this change, your code would look something like:
*j::
if (ergo) ;inverted use of the ergo variable to make the code more efficient
Send {n}
else
Send {y}
return

AutoHotKey - Key spamming does not work like I want it to

Key Spam does not work so good.
Here is the code:
Loop
{
If GetKeyState("z", "P")
send w
If GetKeyState("g", "P")
send a
If GetKeyState("j", "P")
send d
If GetKeyState("h", "P")
send s
}
but when I hold z or zg together, then the original keys z and zg come all time once or twice between the w or wa output.
What can I write that the keys z+g+h+j never will be seen in the text file only w+a+s+d if I run this script?
Karoline, you are going about it the wrong way. Autohotkey was designed to do just what you really want to do.
As MCL says in his comment, you need only to map your keys to a send function
Put this in your script:
z::send w
g::send a
j::send d
h::send s
Now, when you push z it will type w instead.
Keep in mind that this is global over your entire computer unless you limit it to notepad using IfWinActive. The script will not exit on its own.
but this doenst work like i want
i want that i can press g+z same time or z+j same time or g+h same time or h+j same time
and that i get then awawawawaw or asasasasasasas or wdwdwdwdwdwdwd
that when i press 2 buttons same time it switch output ever 1 char
and when i use your answer and holz z it spams wwwwwwwwwwwwwwwwwwwwwwwwwwwww
and if if press then z+g it only continou spaming wwwwwwwwwwwwww not wawawawawawawawaw
thans
Karoline
According to your "answer", you are targeting a keyboard behaviour that's not standard. That is, using remapped keys with AHK or not, when pressing two letter keys simultaneously, solely the key pressed at last will continue firing.
Here's an example on how you can achieve an alternating behaviour instead:
SendMode, Input
g & z::
z & g::
Send, aw
return
g::Send, a
+g::Send, A
z::Send, w
+z::Send, W
You will have to explicitely define every custom combination with an & modifier, which makes them prefix keys. This comes with an upside and with a small downside: The upside is, you can now keep pressing your combination and it will continue firing two keys in an alternating fashion; no loop or the like required. The downside: You need to reconstruct prefix hotkeys' original behaviour. In our example, that's sending a lowercase or uppercase letter.

Remapping alphabet or number key pairs to perform alt,ctrl,shift functions

I work with a hospital data entry system where no customization is allowed. I have found AutoHotKey to provide effective ways to work around the this system's "slow clunkiness".
What I need are more function keys on the keyboard. The best way I have found to approximate that is by remapping key pairs 1 Q:: Alt n, 2 w::ctrl r....ect. The best I could come up with was from AHK site's example and explanation of how to remap letter keys to other letter key destination. I tweaked and adjusted and came up with the following:
1 & q::
SetKeyDelay -1, 40 ;
Send {ctrl Down} ;
Send {Blind}{f Downtemp}
return
1 & q up::
SetKeyDelay -1 ;
Send {Blind}{ctrl up}
Send {Blind}{f Up}
return
This code will do the job but it has a downside. The first key in the sequence looses it's regular function. In this case, I've lost the use of my top row of numbers to become function keys. Is it possible to have the first key revert back to it's native function automatically after it was used in the key pair? I and the other pharmacy personnel will be most grateful for any help you can provide. Thank You.
Key combinations with & produce prefix keys. To restore a prefix key's original function, you need to define it explicitely:
1 & q::Msgbox, 1
1::Send, 1
+1::Send, {!} ; SHIFT + 1 may depend on your keyboard layout
However, it makes sense to arrange your key combinations in a way that produces as few prefix keys as possible. In your example, if you want to define hotkeys like 1 + q, 2 + q, 3 + q, and so on, it may be smarter to use q as the first key, leaving you with the necessity of redefining only one key:
SendMode, Input
q & 1::Msgbox, 1
q & 2::Msgbox, 2
q & 3::Msgbox, 3
q::Send, q
+q::Send, Q
; AltGr+q and CTRL+ALT+q also do something in Germany ;)
<^>!q::Send, #
^!q::Send, #
As you can see in the example, you always have to look out for keys that have a (usually third) functionality that's triggered by AltGr or CTRL+ALT. But this strongly depends on your keyboard layout.

Eval / RegExp listen with Autohotkey

I think this must be possible but I can't find it in the documentation anywhere on autohotkey
I want to do something like:
[a-z0-9]::
SoundPlay, %A_WinDir%\Media\[\1].wav
SoundPlay *-1
i.e. listen for all keys a to z and 0-9 find the relevant wav file (a.wav etc..) and play it. If it can't be found Play a default sound.
Is this way out of AHK's league? Should I stick to doing this in python??!
Many thanks in advance
You can use the Hotkey Command to assign all possible hotkeys to the label.
The first loop here uses a trick with converting values to their ASCII codes, doing math, and converting them back to characters. The logic works like, "What is the 5th character after 'a'?" -- to which it replies 'f'.
#Persistent
Loop, 26
Hotkey, % Chr(Asc("a") + A_Index - 1), MyLabel
Loop, 10
Hotkey, % A_Index - 1, MyLabel
return
MyLabel:
MsgBox You pressed %A_ThisHotkey%
return
I'm not aware of a way to use a regex to specify the hotkey, but this is how I've done it in one of my scripts:
#::
a::
b::
c::
; (all other alpha keys...)
; Pass key on, if not in subscription list
If(in_edit_screen())
{
; Handle caps lock
If(GetKeyState("Capslock", "T"))
{
; Send uppercase letter
StringUpper key, A_ThisHotkey
Send %key%
}
Else
{
Send %A_ThisHotkey%
}
Return
}
; etc.
Return
This is part of a routine that's called whenever a certain window is focused, but the window in question has a few different modes; in one of those modes (indicated by the in_edit_screen flag), I want to just pass the keys through, while in another mode, I want the keys to do something special. A_ThisHotkey is the variable that tells the routine which key/key combination the user actually pressed.
Why not launch a script where you use:
Input, CI_KeyVar, I L1
and then read-out which key was pressed (variable = CI_KeyVar), do your thing (check if the ascii code falls between 0 and Z because this input will act on any input) and re-launch the script to wait for the next key press....
The way out would be to trigger on e.g. the esc key, so the script stops.