Using GetKeyState for triple key combinations does not work - autohotkey

In the following script,when I press the CapsLock and Alt keys and then the J key,"is work" message will not pop up. Only by simultaneously pressing the CapsLock, Alt, and J keys and immediately releasing the keys will "it work" message pop up.
CapsLock & J::
if GetKeyState("Alt","p"){
MsgBox is work
}
return
Change the key J to K, as follows:
CapsLock & K::
if GetKeyState("Alt","p"){
MsgBox is work
}
return
This allows you to press the CapsLock and Alt keys first and then the K key to pop up the "it work" message. I tried other keys, such as A, S, D, F, H, and so on.But you can't change it to G. Only three keys can be pressed at the same time and immediately release the normal output "is work"
I don't understand why?
When custom key combinations use GetKeyState, what is the order in which the keys are executed?

As soon as you trigger your hotkey by pressing CapsLock + J or K it's looking to see if Alt is pressed. So if there is any delay between when the hotkey is triggered and Alt is pressed, then your MsgBox won't show.
To get it to work every time, start by holding the key that GetKeyState is looking for (Alt) and then hold your other keys in order from left to right as listed. So, hold Alt, then hold CapsLock, then press J or K.

Related

Remap keys in loops

Background: I'm trying to have a f-mode and d-mode which means if I press down the f key and press another key like i then nothing happens excepts a shortcut. let say it will send Up key instead of f and I.
Issue: how I can remap a pressed key (I in my example) to a shortcut (Up as example)?
Code:
d::
f::{
;...
loop{
if !GetKeyState("f","p") && !GetKeyState("d","p"){
break
}
if GetKeyState("i","p") {
OutputDebug "i"
send "{up}"
continue
}
; ...
}
}
Looks like you want to make a custom combination.
From the Docs:
You can define a custom combination of two keys (except joystick
buttons) by using " & " between them. In the below example, you would
hold down Numpad0 then press the second key to trigger the hotkey:
Numpad0 & Numpad1::MsgBox You pressed Numpad1 while holding down Numpad0.
Numpad0 & Numpad2::Run Notepad
But also note:
The prefix key loses its native function: In the above example,
Numpad0 becomes a prefix key; but this also causes Numpad0 to lose its
original/native function when it is pressed by itself. To avoid this,
a script may configure Numpad0 to perform a new action such as one of
the following:
Numpad0::WinMaximize A ; Maximize the active/foreground window.
Numpad0::Send {Numpad0} ; Make the release of Numpad0 produce a Numpad0 keystroke. See comment below.
This is to prevent holding down a key from spamming inputs while you wait to press the second part of a key combination. So essentially, your 'f' and 'd' keys will now perform their normal functions when you release them instead of initially pressing them down.
Anyways, the code would become:
f & i::
d & i::
Send {Up}
return
f::f
d::d

Use hotkeys with function keys, modifier keys, and a letter keys

How can I use a hotkey that responds to pressing F24 & Shift D, for example? I want to press a function key (F#, not the key that is labeled "fn"), a modifier key, and a letter.
I am using a mouse with multiple side buttons; I'm using G Hub to send F24 when one of the mouse buttons is pressed. So I'm trying to have that mouse button act as another modifier key.
I'm able to do assign a hotkey with only F24 and a letter key. But I no matter what I try, I cannot do this with an added modifier key. When I try to run the script, I get the error "invalid key." I have tried this with the Shift key and with the Alt key, and neither will work.
This works:
F24 & D::MsgBox, hello
I've tried every alternat syntax shown in the documentation, but none of the following work.
F24 & {Alt} & D::MsgBox, hello
!{F42}D::
!{F42} & D::
F24 & ! & D::
{Alt} {F24} D::
F24 & +D::
Combinations of 3 or more hotkeys are not supported as described in the documentation you can use #if and GetKeyState
I don't know how are you generating F24 the following will work if you first press shift and then F12 and d if your keyboard generates F24 just replace F12 with F24
#if GetKeyState("Shift", "P")
F12 & d::MsgBox works
#If

AutoHotKey redefining Shift key effective for first key press only

Here is a short AHK script which does two things:
Creates an extra Shift key on right alt (AltGr)
Redefines CapsLock as a function key, to be used to type extra characters.
#InputLevel 1
RAlt::RShift ; define shift on right-alt
SC03a::F20 ; define special function key on capslock
#InputLevel 0
F20 & SC002:: ; the '1' key
GetKeyState, sh, Shift
if sh = D
Send ¡ ; upside-down exclamation
else
Send ¹ ; superscript 1
return
It nearly works:
pressing AltGr plus 1 produces '!', as you would expect for a shifted key.
pressing CapsLock plus 1 produces '¹', as you would expect given the definition above.
The problem arises when you hold down both AltGr and CapsLock and press '1'. This produces '¡' as expected for the first character, but subsequent presses produce '¹'. It appears the Shift state gets magically cancelled after the first press.
Note, this doesn't happen with a "real" Shift key - Caps+Shift+1 produces '¡' every time, so it appears to be a problem with redefining another key to be Shift, where its shift state is cancelled after the first instance.
Am I missing something?
A while loop might be what you're looking for.
Example:
LShift::
while (GetKeyState("LShift", "P"))
{
SendInput, {¡}
Sleep, 100
}
return

Avoiding loop when Double pressing same shortcut key without changing original 1 press shortcut key function (Autohotkey)

I want to produce something like this :
1. When i press Esc 2 times (double) i want to produce my "made-up custom action"
2. When i press Esc 1 time i want to produce the original Escape
ok, here is the explanation of my question :
This code should apply as global, means when i start the script it will work on all windows
%ALAT1% is variable the Active Windows title , abbreviation "Anime Land Akui Transform" some kind of silly anime game
If i press Esc one time . it will "pause" the game, but when i press Esc 2 times, it will "exit" the game"
I want to do a custom action when i press Esc 2 times, but i also don't want the original Esc key function got override when i press 1 time.
Escape::
if (A_PriorHotkey = A_ThisHotKey and A_TimeSincePriorHotkey < 400)
WinActivate, %ALAT1%
IfWinActive, ahk_class TscShellContainerClass
{
Send {F5} ; F5 contains another action
}.
else
{
*Do original function of Escape i.e Send {Escape} ; Now this is the problem
}
return
after i try the code above : suppose i press "Escape" button it will loop the Escape as if i press Escape 2 times. So my real question is how to make "original 1 times press Esc key function" won't loop to "our custom 2 times press Esc key function" ?
Change the first line of your code to;
$Escape::
This will set it so that the hotkey is only triggered by real keystrokes not simulated ones.

Mapping Capslock to Esc and Capslock & C to a function in Autohotkey

I want to configure autohotkey in the following way:
Capslock::Esc
Capslock & C::
Run, www.stackoverflow.com
return
So if I just press Capslock it treated like if I would have pressed Esc. If I on the other hand press both Capslock and c, it call the function that opens the browser with www.stackoverflow.com.
At the moment the remapped seems to break when I have the other function in the script. When I press capslock now it toggles it for a short time, so the key alone does effectively nothing. I don't get my Esc.
Pressing capslocks + A on the other hand activates capslock and produces a real A.
Is there an easy way to fix this?
Check out this code:
inProcess = 0
Capslock::
Gui, 93:+Owner ; prevent display of taskbar button
Gui, 93:Show, y-99999 NA, Enable nav-hotkeys
inProcess = 1
KeyWait, Capslock ; wait until the Capslock button is released
Gui, 93:Cancel
if (inProcess == 1){
Send, {Esc}
}
Return
#IfWinExist, Enable nav-hotkeys
*c::
Run, www.stackoverflow.com
inProcess = 0
return
#IfWinExist, ; end context-sensitive block
I've modified an answer available here: http://www.autohotkey.com/board/topic/56428-problem-rebinding-ctrl-to-capslock-using/