1

I want to use AutoHotKey to disable Alt+F4 when they are pressed within 0.05 seconds of each other. Otherwise, I'd like it to work as normal.

Explanation:
My Lenovo Yoga 2 Pro has alternate functions for the function keys.
For example: "F3" is mapped to volume+, "F4" is mapped to "close active window"

There are two modes:

  • Old-school mode: F3 just acts as F3, and you must hold Fn+F3 key to activate volume+
  • New-school mode: Pressing F3 activates volume+, and Fn+F3 will do the normal F3.

In either mode, I run the risk of closing my active window when I go to use volume+ because they are too close, which is very problematic. Note that AutoHotKey cannot detect the Fn key, thus I cannot use that to solve my issue.

The image below shows the AutoHotKey Key History tool. In New-school mode, I typed "asdf" and then pressed "F4" which is "close active window". You can see this actually simulates ALT+F4, and there is a very short duration between ALT and F4...

AutoHotKey Key history

I'm thinking that I could disable this "close active window" function by having AutoHotKey interrupt an ALT+F4 combo when there is less than 0.05 seconds between the two keys. Can this be done?

Edit: In response to Blauhirn's code, here is the original, edited for a shorter wait duration, (from 50 to 10). It works most of the time, though 1/10 times the window is still cosed:

~alt::
hotkey, alt, off
hotkey, !F4, doNothing, on
sleep, 10
hotkey, !F4, doNothing, off
while(getKeyState("alt"))
    sleep, 1
hotkey, alt, on
return

doNothing:
return

Here is a change I thought would fix my focus issue by sending a 2nd Alt when the "close active window" was detected:

doNothing:
send {LAlt}
return

However, the 2nd Alt is not sent. It IS sent when the delay is above 40ish, however I find that is way too long, and in turn it interferes with my manual use of Alt+F4.

Bort
  • 493
  • 1
  • 8
  • 26

1 Answers1

2

Have you tried using simply

F4::return

? Maybe this will override the Lenovo action for F4

Other than that, here are the two approaches I can think of:

  1. Disabling the ALT+F4 standard win hotkey by default. Adding a custom hotkey for a delayed F4

    !F4::   ; by default:
    doNothing:  ; this is a label (see GoSub)
    return  ; == do nothing
    
    ~alt::  ; alt was pressed
        sleep, 50   ; wait 50 milliseconds
        if(!getKeyState("alt")) ; if alt is NOT pressed down anymore, exit
            return
        else    ; (else is actually unnecessary here)
            hotkey, !F4, close  ; Add new AltF4-hotkey
    return
    
    close:
        winclose, A ; close the Active window
    return
    
    ~alt up::   ; alt is being released
        hotkey, !F4, doNothing  ; remove the new AltF4 hotkey and go back to custom standard behaviour: do nothing.
    return
    

    it still triggers Alt, which usually leaves me in the menu of the active window (File, Edit, View, etc), or if typing within a textarea (such is this), it will remove typing focus.

    well yes. If you decide to keep the lenovo keys, I don't think there is a way to prevent it. As you suggested, sending ALT again should solve the problem

  2. using Input, after ALT has been pressed. Input blocks user input for a configurable time, as long as the V option is used.

(3. disabling your Lenovo Yoga 2 Pro special keys. If you need the F3 function, you can do that in AutoHotkey e.g. using send {volume_up}

phil294
  • 10,038
  • 8
  • 65
  • 98
  • Your first approach works! The only issue is that it still triggers Alt, which usually leaves me in the menu of the active window (File, Edit, View, etc), or if typing within a textarea (such is this), it will remove typing focus. Can you explain the logic of what is happening in that snippet? If I add an additional Alt press when it detects the Fn+Alt, it should undo the change in focus. Plus I may wish to further edit it, but I'm not sure what is happening. – Bort Jul 25 '15 at 15:22
  • I adjusted the code (&post), working a bit different now – phil294 Jul 25 '15 at 15:59
  • Thanks but I may have explained myself poorly. Your original code worked, but the new one doesn't. I simply wanted to edit your original code to press Alt again if it detected the "Close active window". I have edited my question to show what I tried. Keep in mind that I don't want to disable F4 or Alt+F4 when I do it manually. – Bort Jul 25 '15 at 21:58
  • I did understand you. I replaced the old code with a new one, for it is better understandable. it sould work similarly. why doesnt it? – phil294 Jul 26 '15 at 09:50
  • for that other problem, try adding `keywait, alt` before the alt send – phil294 Jul 26 '15 at 09:54