Pages

2024/07/12

Send Key Sequences After a Key is Held Down using AutoHotkey Free on Windows 11

As far as I know, this is the only free app that lets me do what the title says.



Other free apps can set key combination or one key or remap mouse buttons or holding a mouse button to send actions, you can read my posts below.

Hold Mouse Button to Execute Key Sequences with X-Mouse Button Control FREE on Windows

Use PhraseExpress To Set Keyboard Shortcut For LibreOffice Writer's Menus – FREE

Clavier+ (I haven’t written about it but I will)

Thanks to AutoHotkey forum

I changed the script from this post to suit my need.



The test steps – AutoHotkey 1.1.34.04
OS: Windows 11 Home
Download: https://www.autohotkey.com/

 
1. Download it from a link above and install.



$a::
aDown:=A_TickCount
Keywait a
Duration:=(A_TickCount-aDown)
If (Duration>800)
Send ^c
Else
Send a
Return


Let me take time talking about coding.
The script above (orange font), If I hold key ‘a’ longer than 800 milliseconds (1 sec = 1,000 milliseconds), Ctrl+C  will be pressed (copy command).
If I hold key ‘a’ less than or equal to 800 milliseconds, key ‘a’ will be pressed (just like you type letter ‘a’)
If you want to change letter ‘a’ to others, in my case letter ‘x’, you have to make changes in the following lines to:

Line 1: $x::
Line 3: Keywait x
Line 8: Send x


If I hold key ‘x’ longer than 800 milliseconds, ), Ctrl+C  will be pressed (copy command). If I hold key ‘x’ less than or equal to 800 milliseconds, key ‘x’ will be pressed (just like you type letter ‘x’)
It is simple, isn’t it?
But if you want a special letter like myself, you might find it hard for coding.



If I press a key in the picture above, I get letter `. And I want that letter to send key combination to save a file (Ctrl+S).  I can not use the same way like I do in the previous script. I have to make changes in the following lines to:

Line 1: *`::
Line 3: Keywait ``
Line 6: Send ^s
Line 8: Send ``


One of the hard parts is ‘Send’ command. If a key has more than one letter, such as a key ‘Delete’. I have to use {Delete}.  The code below lets me delete files after I hold a key ‘x’. And if I press a key ‘x’ (not holding), I get letter ‘x’ just like I type a key ‘x’.

$x::
aDown:=A_TickCount
Keywait x
Duration:=(A_TickCount-aDown)
If (Duration>800)
Send {Delete}
Else
Send x
Return


That’s it for hard parts, let me talk about coding of modifiers. I have only tested ‘Ctrl’ key, but the manual is on its website.



The picture above I captured from its website. If I want it to press Ctrl+S, in the script, I have to write:

Send ^s

I think AutoHotkey can open a file or do other actions. If I tested it, I might write about it.


2. How to run a script. If you want to test, copy the first script then open a blank document of Notepad and past it.



Click File > Save. Name a file and add ‘.ahk’ at the end of name.



Then click ‘Save’.



Make sure that a script will be opened by AutoHotkey, I make a right click at a script to see menu ‘Open’. AutoHotkey’s icon is near text ‘Open’.  Open it.



Click an arrow at taskbar, you will see its icon that means a script is running/monitoring.
The flow is I select text or a file, hold key ‘a’ to copy it.



The picture above, there are menus.
Imagine I have more than 10 scripts running, I click an arrow at taskbar, I see a lot of its icons without a name if I don’t point a mouse pointer to it. It would be good, if I had a window to manage all scripts. It might exist somewhere.


The test steps – AutoHotkey 1.1.34.04
OS: Windows 11 Home
Download: https://www.autohotkey.com/

No comments: