1

I have an AutoHotKey script that should hit F3 when I hold down Ctrl, and hit F2 when I release Ctrl.

My script currently:

$ctrl::
Suspend, On
Send, {F3}
While (GetKeyState("Ctrl",P))
{
}
Send, {F2}
Suspend, Off
Return

But when I hold Ctrl down, it does nothing. When I release Ctrl, it hits both F3 and F2. Can someone tell me how to fix this?

1

via: http://www.autohotkey.com/docs/commands/Send.htm

To hold down or release a key: Enclose in braces the name of the key followed by the word Down or Up. For example:

Send {b down}{b up}
Send {TAB down}{TAB up}
Send {Up down} ; Press down the up-arrow key.
Sleep 1000 ; Keep it down for one second.
Send> {Up up} ; Release the up-arrow key.

When a key is held down via the method above, it does not begin auto-repeating like it would if you were physically holding it down (this is because auto-repeat is a driver/hardware feature). However, a Loop can be used to simulate auto-repeat. The following example sends 20 tab keystrokes:

Loop 20 {
Send {Tab down} ; Auto-repeat consists of consecutive down-events (with no up-events).
Sleep 30 ; The number of milliseconds between keystrokes (or use SetKeyDelay).
}
Send {Tab up} ; Release the key.

The word DownTemp may also be used. Its effect is the same as Down except for the modifer keys (Control/Shift/Alt/Win). In those cases, DownTemp tells subsequent sends that the key is not permanently down, and may be released whenever a keystroke calls for it. For example, Send {Control DownTemp} followed later by Send a would produce a normal "a" keystroke, not a control-A keystroke.

  • The issue isn't the sending of the keys, but the hotkey triggers. – Nelson Sep 29 '16 at 3:52
1

This code provides your desired functionality:

$~ctrl::
    send {f3}
    keyWait, ctrl
    send {f2}
return
  • The ~ modifier causes the hotkey to not block the hotkey, so you are effectively sending Ctrl+F3 and Ctrl+F2. – Nelson Sep 29 '16 at 3:53
  • Nope it's only sending F3. Try my code. Change one of the F-keys to S and executive it in a Word document. It will only send S, not Ctrl-S to Save. The tilde is required to prevent the KB hook from waiting for the input, otherwise it won't work. (This is why your new Answer with the omitted tilde does exactly the same thing as this one does, but it took double the lines.) – adaaaam Nov 28 '16 at 4:18
0

The keyup option for the hotkeys seems to be what you need.

Here is tested working code:

Ctrl::
Send {F3}
keywait, Ctrl, L ; prevent F3 from firing while Ctrl is being held down.
return

Ctrl Up::Send {F2}
  • I don't know what happens if you try to run this script and also try to use CTRL based shortcut keys like CTRL+C. I think this script will block it. – Nelson Sep 29 '16 at 4:05
  • The Question was already answered with a working script here: superuser.com/a/815704/239384 – adaaaam Nov 28 '16 at 4:19
  • Also the KeyUp is unnecessary, you're creating multiple hotkeys which adds complexity – adaaaam Nov 28 '16 at 4:27

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.