0

I have a fairly simple idea that I think AutoHotKey could help me with. Essentially, when I use ctrl-v to paste, I want it to transmit a second ctrl-v so that I paste twice. I've got several keybinds in my development environment set up to be double-strokes of various ctrl keypresses, so I'd also like to be able to extend the script to various other keys aside from ctrl-v, too. This is my first AutoHotKey script though and I'm having a hard time getting off the ground since there's a lot here to digest. Thanks!

  • Is tapping v twice so hard you need to script it? Or should I say request a script for it ;) – jiggunjer Aug 27 '17 at 9:59
  • Like I said, I have a number of other hotkeys that are set up as doubled keystrokes as well. This was partly to speed up those keystrokes, while also serving as a contextually-relevant way to get me started with AHK, I guess. – bwerks Aug 28 '17 at 18:28
1

In AutoHotkey, double-sending a key is pretty straightforward.

#IfWinActive
~^v::SendInput, ^v

A few additional items:

1) You can tailor which programs will receive a double-send or not by using #IfWinActive and designating a window name, exe name, class, etc. for WinTitle

2) The tilde in the hotkey definition means that the script doesn't stop the original keystroke from going through. If you want full control over whether the original goes through or not, you could remove the tilde (which will block the original keystroke) and then send it twice yourself (or not at all, or whatever you might want to do without the original keystroke getting registered).

3) You can add a delay if needed by extending the function to more than just a single-line hotkey definition and using a Return statement

4) You can use the same code for multiple hotkeys using A_ThisHotkey.

5) You can also allow keystrokes with multiple modifiers using a * modifier if needed. This may not work exactly right with the example code below but just something to be aware of.

Example of above items 1-4:

; this will execute the code to send duplicate keystrokes 
; for any of the hotkeys defined, in this case, a,b,c, or Ctrl+V
#IfWinActive ahk_exe notepad.exe
~a::
~b::
~c::
~^v::
    Sleep 200   ; Possibly use or adjust for reliability if needed
    SendInput % SubStr(A_ThisHotkey, 2) ; Remove tilde and send duplicate keystrokes for whichever hotkey executed
Return
  • I haven't worked with ahk for some time, but doesn't this trigger an infinite loop of hotkeys? – jiggunjer Aug 27 '17 at 10:05
  • No. AutoHotkey prevents that by default. You can use SendLevel settings to retrigger hotkey definitions if needed. – JJohnston2 Aug 27 '17 at 16:22
  • I like the easy extensibility that this script exposes, rather than having nearly-identical blocks per-key that differ only in the key being duplicated. Very cool! – bwerks Aug 28 '17 at 18:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.