Avoiding loop when Double pressing same shortcut key without changing original 1 press shortcut key function (Autohotkey) - 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.

Related

How to use two keys as trigger AutoHotKey?

I would like to use ctrl + w + x as a hotkey, but of course ^wx:: is an invalid hotkey.
here the ressources I've found, I tried them but they didn't worked for me, (though english isn't my native language so I may have misreaded??)
the AutoHotKey documentation for the list of keys
https://www.autohotkey.com/docs/KeyList.htm
An AutoHotKey topic named "Press 2 buttons at the same time?" from 2018
https://www.autohotkey.com/boards/viewtopic.php?t=45869
Another Autohotkey topic named "Trying to activate a key press using 2 keys" from 2014
https://autohotkey.com/board/topic/109093-trying-to-activate-a-key-press-using-2-keys-tried-a-bc-error-on-run-help-please-s/
thanks in advance for helping me :)
Here is one solution based on the first example from the Input command in the Documentation:
delay := 3.0 ;number of seconds to wait for additional input
$^w::
Input, SingleKey, L1T%delay%, {LControl}{RControl}{LAlt}{RAlt}{LShift}{RShift}{LWin}{RWin}{AppsKey}{F1}{F2}{F3}{F4}{F5}{F6}{F7}{F8}{F9}{F10}{F11}{F12}{Left}{Right}{Up}{Down}{Home}{End}{PgUp}{PgDn}{Del}{Ins}{BS}{CapsLock}{NumLock}{PrintScreen}{Pause}
if (SingleKey = "x"){
;;;;;;;
MsgBox Put Hotkey here
Send ctrl + w + x was pressed
;;;;;;;
}
else{
Send {Ctrl Down}{w}{Ctrl Up}
Send %SingleKey%
}
return
Explanation of Code:
Setup:
Declare a variable to hold the amount of time you want the hotkey to wait for an 'x' input after Ctrl+w is pressed, called delay
Hotkey Declaration: When Ctrl+w is pressed
Wait delay number of seconds for the user to press another key
If that key was x, then send your custom hotkey
Otherwise, send Ctrl+w, followed by whatever key was pressed by the user.
Other Notes:
Using the $ hotkey modifier to prevent an infinite recursive loop
If it doesn't work properly, feel free to let me know and I can try to help you

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

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

Exchange LWin and LAlt

This is my code.
#InputLevel 1
LAlt::LWin
LWin::LAlt
#InputLevel 2
!a::
#a::
tooltip You pressed %A_ThisHotkey%.
return
From my understanding, InputLevel 2 is executed first, then InputLevel 1, which means if I press LAlt key and a key, screen will show I pressed !a, if I press LWin key and a key, screen will show I pressed #a.
However actually my screen shows nothing. My key is not captured.
Do I miss anything? I want to capture hotkey first then do remapping.
When you press alt + a the LAlt::LWin hotkey is being fired first and the !a:: hotkey is being ignored. If you want to fire the !a:: regardless of the first hotkey fired you must add ~ on the LAlt::LWin hotkey so its native function (necessary to perform alt+a) isn't going to be blocked.
It may be easier to understand with this example:
#InputLevel 1
~LAlt:: tooltip First pressed %A_ThisHotkey%.
~LWin:: tooltip First pressed %A_ThisHotkey%.
#InputLevel 2
!a::
#a::
tooltip Then pressed %A_ThisHotkey%.
return
ps: if you also want to ignore extra modifiers that are (maybe) being held down you can add the wildcard * on the !a:: and #a:: hotkeys. So, in the end, the hotkeys would look like this: ~LAlt::LWin ~LWin::LAlt *!a:: and *#a::

Remove auto modifier from hotkeys

Is there a way to make a hotkey with a modifier that doesn't automatically put the modifier on the output key? ex: ^a::b -- except, when I hit ctrl+a it just gives me ctrl+b, when I just want ctrl+a to = b.
Playing a game which for some reason won't let me use hotkey ctrl + keys so I'm circumventing the issue by setting the hotkeys as something out of the way then setting the actual hotkey I want to those keys in autohotkey.
Try:
^a::
Send, b
Send, c
Send, d
Return
or
^a::Send, bcd
Could you show the exact text of line 4 that causes the error?