AutoHotKey code to receive ctrl+x while pressing ctrl+c twice - autohotkey

AutoHotKey code to receive CTRL+X while pressing CTRL+V twice
Can anyone help with this?
Many thanks!

Assuming we are talking about Ctrl+C, not V, and assuming you want to keep the original Ctrl+C function but also use it for Ctrl+X when pressing twice in a short time:
#persistent
Transform, cC, Chr, 3 ; store the value of ctrlC into cC.
hotkey, $^c, ctrlC, ON ; this is basically the same as if to say $^c::..., but only deactivable
return
ctrlC:
hotkey, $^c, ctrlC, OFF
input, key, M L1 T0.2
hotkey, $^c, ctrlC, ON
if errorlevel = timeout
send ^c
else if key = %cC%
send ^x
else
send %key%
return
should do..
also see Input for further information. I used this little hotkey-command-trick in order to temporarily disable the Ctrl+C-Hotkey, because otherwise input would not recognize the second c
In this example, I set timeout to 0.2 seconds. Change it to your convenience.
About your capslock idea - sounds like a good idea to me, but anyways, we're not a code-provider network. The command getKeyState should help you started.

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

How to not send hotkey text to open editor

I use this hotkey to close the current Window :
:*:xx::
Send, {Alt Down}{Sleep 100}{f4 Down}{Alt Up}{f4 Up}
return ;
How can the script be amended so that the characters xx are not sent to display but are still registered by autohotkeys ? In other words if focus is within open editor do not display the xx characters but still fire the commands associated with the xx keys.
You can't do it with a Hotstring but you would have have to use a Hotkey and check for a double key press. A regular key like x might not be the most useful as it will most likely always get in the way of your regular typing as you want to block the x being sent. An example with Ctrl:
~Ctrl::
KeyWait, Ctrl ; wait for Ctrl to be released
KeyWait, Ctrl, D T0.2 ; and pressed again within 0.2 seconds
if ErrorLevel ; timed-out (only a single press)
MsgBox single
else
MsgBox double
return
The above code comes from here http://www.autohotkey.com/board/topic/23224-resolved-catch-a-double-press-click/#entry150299 and you will also find an example (no 4) on the KeyWait doc http://ahkscript.org/docs/commands/KeyWait.htm

Dual use of keys in autohotkey?

I would like to assign the number 1 so that when I press it the result is to send to the computer qx and then enter.
1:: Send, qx{ENTER}
q:: Send, jx{Enter}
But then I also want to assign the letter q to send something else to the computer. Is it possible to do this? I'm concerned that if I assigned 1 to something that involves q, then whenever I press 1 q will also be called, and we'll end up in a loop.
Is there some way to fix it so AHK 'knows' when qx gets sent that I don't want to call the q function (and hence jx)?
Good thinking. The AutoHotKey developers have thought about that and introduced the $ to prevent this "loop"
example:
$1::Send, q
$q::Send, 1
Pressing 1 will generate a q, but due to the $ it will NOT trigger the second hotkey and vise versa.
So by pacing a $ in front of the q, you will prevent this loop in your situation.

How to remap key in certain case and not remap in other case with autohotkey?

I want to remap alt+e when caps is on in autocad.
And when capslock is not on, alt+e should open menu edit.
I use script like this
<!e::
if(GetKeyState( "CAPSLOCK", "T" ))
{
SendInput erase{space}wp{space}
}
else
{
Send !e
}
When I turn on capslock, remap key is OK.
When I turn off capslockand alt+e, menu edit opened, but closed immediately.
Thanks.
You will want a $ at the beginning of your hotkey to prevent the endless loop that the !e in your else block will trigger. You will also want to add a Return at the end of the hotkey to prevent the script from continuing into what is below this hotkey.
$!e::
if GetKeyState( "CapsLock", "T" )
Sendinput, erase{space}wp{space}
else
Sendinput, !e
Return
(Brackets are only required when if/else blocks are more than one line.)
Beyond that, the likely issue is that it's an alt hotkey that is also set to send alt.
I say this is an issue because if you press and hold alt, it activates menus,
and then the script sends alt, which will be in conflict with that.
As Ricardo said, the ideal way to script this is with the #IF command (only included with AHK_L).
#If GetKeyState("CapsLock", "T") and WinActive("AutoCAD")
!e:: SendInput, erase{space}wp{space}
#If
Notice that you can add the WinActive() function to the #If command's expression.
Try it without that first, and also realize that the application's title needs to be exactly "AutoCAD" at all times for that to work. I would recommend finding AutoCad's ahk_class,
with AHK's window spy, instead of using the title.
If it still does not work, it is likely that AHK is sending faster than AutoCAD would like to receive.
Info on how to deal with that can be found here.
Try to change your else block to this:
Send, {ALTDOWN}e{ALTUP}
I do not rely on these symbols to send keystrokes in AutoHotKey.

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.