【问题标题】:How to assign hotkey of pressing Alt key twice (Alt+Release+Alt) in Autohotkey?如何在 Autohotkey 中分配按 Alt 键两次(Alt+Release+Alt)的热键?
【发布时间】:2018-10-23 15:16:25
【问题描述】:

如何在Autohotkey中分配快速按{Alt}键两次的热键?

换句话说:如何将Press Alt+Release Alt+Press Alt指定为热键?
换句话说:如何分配 key down {Alt} key then key up {Alt} key then 快速 key down {Alt}?.

我想设置释放{Alt} 键然后再次按下它(非常小)以激活热键之间的时间
• 示例: 如果用户按下{Alt} 键然后释放它然后等待5 秒然后再次按下{Alt} 键,热键根本不应该工作或激活。但是如果他在 1.5 秒之前再次按下{Alt} 键,那么热键将起作用。我希望能够设置这个时间。
• 类比: 这类似于双击鼠标按钮并设置第一次和第二次单击之间的时间,以便双击操作触发。但我正在寻找的是为键盘键设置双击。

【问题讨论】:

    标签: keyboard-shortcuts autohotkey


    【解决方案1】:

    Autohotkey 论坛上有一个脚本已经完成了这个任务:RapidHotkey

    页面上列出了两个必须包含在脚本中的函数,但它们使您能够设置热键、所需的点击次数以及两次点击之间允许的时间。这方面的一个样本可以是

    ~Alt::RapidHotkey("myLabel", 2, 0.3, 1)
    
    myLabel:
    Msgbox Alt was double clicked
    Return
    

    【讨论】:

    • 如果您在答案中包含 RapidHotkey() 函数会很好,因为您包含的链接将来可能无法使用。
    • 这太棒了,我将双 alt 重新映射为 win+b(显示自动隐藏的 Windows 任务栏而不显示开始菜单)
    【解决方案2】:

    如果你想在 Autohotkey 中,在按下热键两次(或点击次数)时执行 AHK 操作

    例如单击 (ALt 2x) - 执行操作。

    您可以使用这两个 AHK Scipt。 (第一个脚本很大,但你可以做很多想法。)

    1 - KeypressValueToREG.ahk - 这会将任何(键盘点击动作)和(鼠标点击动作)只放入一个(Windows 注册表项)

    2 - ShowKeypressValue.ahk - 这将直观地显示 KeypressValue 并且如果按两次 Alt 键,它将运行 Ahk 代码。

    注意:您需要同时运行两个脚本。

    KeypressValueToREG.ahk

    ;KeypressValueToREG.ahk comes from KeypressOSD.ahk that was Created by Author RaptorX
    ; Open this Script in Wordpad and For Changelog look to the Bottom of the script. 
    ;This code works with a getkeyname from a Dllcall (See Bottom Script- by Lexikos)
    ;you can press the esc key to exit.
    
    #SingleInstance force
    #NoEnv
    SetBatchLines, -1
    ListLines, Off
    
    ; Settings
        global TransN                := 200      ; 0~255
        global ShowSingleKey         := True
        global ShowMouseButton       := True
        global ShowSingleModifierKey := True
        global ShowModifierKeyCount  := true
        global ShowStickyModKeyCount := false
        global DisplayTime           := 2000     ; In milliseconds
        global GuiPosition           := "Bottom" ; Top or Bottom
        global FontSize              := 50
        global GuiHeight             := 115
    
    CreateGUI()
    CreateHotkey()
    return
    
    OnKeyPressed:
        try {
            key := GetKeyStr()
            ShowHotkey(key)
            SetTimer, HideGUI, % -1 * DisplayTime
        }
    return
    
    OnKeyUp:
    return
    
    _OnKeyUp:
        tickcount_start := A_TickCount
    return
    
    
    CreateGUI() {
        global
    
        Gui, +AlwaysOnTop -Caption +Owner +LastFound +E0x20
        Gui, Margin, 0, 0
        Gui, Color, Black
        Gui, Font, cWhite s%FontSize% bold, Arial
        Gui, Add, Text, vHotkeyText Center y20
    
        WinSet, Transparent, %TransN%
    }
    
    CreateHotkey() {
        Loop, 95
        {
            k := Chr(A_Index + 31)
            k := (k = " ") ? "Space" : k
    
            Hotkey, % "~*" k, OnKeyPressed
            Hotkey, % "~*" k " Up", _OnKeyUp
        }
    
        Loop, 24 ; F1-F24
        {
            Hotkey, % "~*F" A_Index, OnKeyPressed
            Hotkey, % "~*F" A_Index " Up", _OnKeyUp
        }
    
        Loop, 10 ; Numpad0 - Numpad9
        {
            Hotkey, % "~*Numpad" A_Index - 1, OnKeyPressed
            Hotkey, % "~*Numpad" A_Index - 1 " Up", _OnKeyUp
        }
    
        Otherkeys := "WheelDown|WheelUp|WheelLeft|WheelRight|XButton1|XButton2|Browser_Forward|Browser_Back|Browser_Refresh|Browser_Stop|Browser_Search|Browser_Favorites|Browser_Home|Volume_Mute|Volume_Down|Volume_Up|Media_Next|Media_Prev|Media_Stop|Media_Play_Pause|Launch_Mail|Launch_Media|Launch_App1|Launch_App2|Help|Sleep|PrintScreen|CtrlBreak|Break|AppsKey|NumpadDot|NumpadDiv|NumpadMult|NumpadAdd|NumpadSub|NumpadEnter|Tab|Enter|Esc|BackSpace"
                   . "|Del|Insert|Home|End|PgUp|PgDn|Up|Down|Left|Right|ScrollLock|CapsLock|NumLock|Pause|sc145|sc146|sc046|sc123"
        Loop, parse, Otherkeys, |
        {
            Hotkey, % "~*" A_LoopField, OnKeyPressed
            Hotkey, % "~*" A_LoopField " Up", _OnKeyUp
        }
    
        If ShowMouseButton {
            Loop, Parse, % "LButton|MButton|RButton", |
                Hotkey, % "~*" A_LoopField, OnKeyPressed
        }
    
        for i, mod in ["Ctrl", "Shift", "Alt"] {
            Hotkey, % "~*" mod, OnKeyPressed
            Hotkey, % "~*" mod " Up", OnKeyUp
        }
        for i, mod in ["LWin", "RWin"]
            Hotkey, % "~*" mod, OnKeyPressed
    }
    
    ShowHotkey(HotkeyStr) {
        WinGetPos, ActWin_X, ActWin_Y, ActWin_W, ActWin_H, A
        if !ActWin_W
            throw
    
        text_w := (ActWin_W > A_ScreenWidth) ? A_ScreenWidth : ActWin_W
    
        ;remove this gui codeline if you want only to Write the Value to Windows registry
        ;GuiControl,     , HotkeyText, %HotkeyStr%
        ;GuiControl,     , HotkeyText, %HotkeyStr%
    
        RegWrite, REG_SZ, HKEY_CURRENT_USER,software\GetKeypressValue,KeypressValue,%HotkeyStr%
    
        ;remove this gui codeline if you want only to Write the Value to Windows registry
        ;GuiControl, Move, HotkeyText, w%text_w% Center
        ;GuiControl, Move, HotkeyText, w%text_w% Center
    
        if (GuiPosition = "Top")
            gui_y := ActWin_Y
        else
            gui_y := (ActWin_Y+ActWin_H) - 115 - 50
    
        ;remove this gui codeline if you want only to Write the Value to Windows registry
        ;Gui, Show, NoActivate x%ActWin_X% y%gui_y% h%GuiHeight% w%text_w%
        ;Gui, Show, NoActivate x%ActWin_X% y%gui_y% h%GuiHeight% w%text_w%
    }
    
    GetKeyStr() {
        static modifiers := ["Ctrl", "Shift", "Alt", "LWin", "RWin"]
        static repeatCount := 1
    
        for i, mod in modifiers {
            if GetKeyState(mod)
                prefix .= mod " + "
        }
    
        if (!prefix && !ShowSingleKey)
            throw
    
        key := SubStr(A_ThisHotkey, 3)
    
        if (key ~= "i)^(Ctrl|Shift|Alt|LWin|RWin)$") {
            if !ShowSingleModifierKey {
                throw
            }
            key := ""
            prefix := RTrim(prefix, "+ ")
    
            if ShowModifierKeyCount {
                if !InStr(prefix, "+") && IsDoubleClickEx() {
                    if (A_ThisHotKey != A_PriorHotKey) || ShowStickyModKeyCount {
                        if (++repeatCount > 1) {
                            prefix .= " ( * " repeatCount " )"
                        }
                    } else {
                        repeatCount := 0
                    }
                } else {
                    repeatCount := 1
                }
            }
        } else {
            if ( StrLen(key) = 1 ) {
                key := GetKeyChar(key, "A")
            } else if ( SubStr(key, 1, 2) = "sc" ) {
                key := SpecialSC(key)
            } else if (key = "LButton") && IsDoubleClick() {
                key := "Double-Click"
            }
            _key := (key = "Double-Click") ? "LButton" : key
    
            static pre_prefix, pre_key, keyCount := 1
            global tickcount_start
            if (prefix && pre_prefix) && (A_TickCount-tickcount_start < 300) {
                if (prefix != pre_prefix) {
                    result := pre_prefix pre_key ", " prefix key
                } else {
                    keyCount := (key=pre_key) ? (keyCount+1) : 1
                    key := (keyCount>2) ? (key " (" keyCount ")") : (pre_key ", " key)
                }
            } else {
                keyCount := 1
            }
    
            pre_prefix := prefix
            pre_key := _key
    
            repeatCount := 1
        }
        return result ? result : prefix . key
    }
    
    SpecialSC(sc) {
        static k := {sc046: "ScrollLock", sc145: "NumLock", sc146: "Pause", sc123: "Genius LuxeMate Scroll"}
        return k[sc]
    }
    
    ; by Lexikos - https://autohotkey.com/board/topic/110808-getkeyname-for-other-languages/#entry682236
    GetKeyChar(Key, WinTitle:=0) {
        thread := WinTitle=0 ? 0
            : DllCall("GetWindowThreadProcessId", "ptr", WinExist(WinTitle), "ptr", 0)
        hkl := DllCall("GetKeyboardLayout", "uint", thread, "ptr")
        vk := GetKeyVK(Key), sc := GetKeySC(Key)
        VarSetCapacity(state, 256, 0)
        VarSetCapacity(char, 4, 0)
        n := DllCall("ToUnicodeEx", "uint", vk, "uint", sc
            , "ptr", &state, "ptr", &char, "int", 2, "uint", 0, "ptr", hkl)
        return StrGet(&char, n, "utf-16")
    }
    
    IsDoubleClick(MSec = 300) {
        Return (A_ThisHotKey = A_PriorHotKey) && (A_TimeSincePriorHotkey < MSec)
    }
    
    IsDoubleClickEx(MSec = 300) {
        preHotkey := RegExReplace(A_PriorHotkey, "i) Up$")
        Return (A_ThisHotKey = preHotkey) && (A_TimeSincePriorHotkey < MSec)
    }
    
    HideGUI() {
        Gui, Hide
    }
    
    ~esc::exitapp    
    ;---------------------------------------------
    ; ChangeLog : v2.22 (2017-02-25) - Now pressing the same combination keys continuously more than 2 times,
    ;                                  for example press Ctrl+V 3 times, will displayed as "Ctrl + v (3)"
    ;             v2.21 (2017-02-24) - Fixed LWin/RWin not poping up start menu
    ;             v2.20 (2017-02-24) - Added displaying continuous-pressed combination keys.
    ;                                  e.g.: With CTRL key held down, pressing K and U continuously will shown as "Ctrl + k, u"
    ;             v2.10 (2017-01-22) - Added ShowStickyModKeyCount option
    ;             v2.09 (2017-01-22) - Added ShowModifierKeyCount option
    ;             v2.08 (2017-01-19) - Fixed a bug
    ;             v2.07 (2017-01-19) - Added ShowSingleModifierKey option (default is True)
    ;             v2.06 (2016-11-23) - Added more keys. Thanks to SashaChernykh.
    ;             v2.05 (2016-10-01) - Fixed not detecting "Ctrl + ScrollLock/NumLock/Pause". Thanks to lexikos.
    ;             v2.04 (2016-10-01) - Added NumpadDot and AppsKey
    ;             v2.03 (2016-09-17) - Added displaying "Double-Click" of the left mouse button.
    ;             v2.02 (2016-09-16) - Added displaying mouse button, and 3 settings (ShowMouseButton, FontSize, GuiHeight)
    ;             v2.01 (2016-09-11) - Display non english keyboard layout characters when combine with modifer keys.
    ;             v2.00 (2016-09-01) - Removed the "Fade out" effect because of its buggy.
    ;                                - Added support for non english keyboard layout.
    ;                                - Added GuiPosition setting.
    ;             v1.00 (2013-10-11) - First release.
    ;--------------------------------------------
    

    ShowKeypressValue.ahk

    #SingleInstance force
    Gui, +AlwaysOnTop -MaximizeBox ; -Caption +Resize -MinimizeBox +Disabled -SysMenu -Owner +OwnDialogs
    Gui, Add, Text, center y10 h50 w300 vVar,  %KeypressValue%
    Gui, Color, White
    Gui, show
    size=20
    Gui, Font, s%size%
    GuiControl, Font, var
    
    ;run KeypressValueToREG.ahk - together with ShowKeypressValue.ahk
    ;The Features Are:
    ; - It will Show On your Screen, [All your Mouse Movements] and [All Keyboard Shortcuts Movement]
    ; - You can Make Scripts, that can do actions with MultiClicks on All Keyboard Shortcuts Clicks, How Nice Is that. 
    
    loop
    {
    RegRead, KeypressValue, HKEY_CURRENT_USER,software\GetKeypressValue,KeypressValue ; read KeypressValue
    sleep 50
    GuiControl,, var, %KeypressValue%
    
    
    
    if (KeypressValue="Alt ( * 2 )") ;use this for [1x=Alt][2x=Alt ( * 2 )][3x=Alt ( * 3 )] [and many more]
    {
    ;Here you can put any AHK CODE 
    msgbox you did click Alt 2x Times
    }
    
    if (KeypressValue="Alt ( * 3 )") ;use this for [1x=Alt][2x=Alt ( * 2 )][3x=Alt ( * 3 )] [and many more]
    {
    ;Here you can put any AHK CODE 
    msgbox you did click Alt 3x Times
    }
    
    
    } ;End Loop
    
    
    ~esc::exitapp
    

    如果您想使用精确的 MilliSeconds 控制值。

    你只能使用这个例子:

    example2.ahk

    注意:在 Line (SetTimer, CountClicks, 100) 中,您可以优化(更改)数字 100。(以便它在您的系统上正常工作。)(在后台运行的任何 Ahk 脚本的默认值 = 250ms )

    #NoEnv
    #SingleInstance force
    ;#NoTrayIcon
    
    a1 := -1
    b1 := 0
    
    esc::exitapp ;click (esc) key to stop the script. 
    
    ;~ key the default function Alt is also active. 
    ;and if you Click the Alt Button 3x times, it will Execute Ahk Code Part 3
    ~Alt::
    if(a1 = -1)
    {
    a1 := 4
    #Persistent
    SetTimer, CountClicks, 100
    }
    else
    {
    a1 := 3
    }
    return
    
    CountClicks:
    if(a1 = 3)
    {
    b1 := b1 + 1
    }
    if(a1 = 0)
    {
    msgbox you did Click <Alt> Key > %b1%x times
    if (b1=1) 
    {
    ;if Click 1x - Then Execute Ahk Code Part 1 
    ;Here you can put any code for Part 1 
    msgbox Executed Ahk Code Part 1      
    } 
    if (b1=2) 
    {
    ;if Click 2x - Then Execute Ahk Code Part 2  
    ;Here you can put any code for Part 2 
    msgbox Executed Ahk Code Part 2       
    } 
    if (b1=3) 
    {
    ;if Click 3x - Then Execute Ahk Code Part 3 
    ;Here you can put any code for Part 3
    msgbox Executed Ahk Code Part 3       
    } 
    if (b1=4) 
    {
    ;if Click 4x - Then Execute Ahk Code Part 4 
    ;Here you can put any code for Part 4
    msgbox Executed Ahk Code Part 4         
    } 
    b1 := 0
    SetTimer, CountClicks , off
    reload ; restart script
    }
    a1 := a1 - 1
    return
    

    【讨论】:

      【解决方案3】:

      使用这个简单的脚本:

      LAlt::
          If (A_PriorHotKey = "LAlt") AND (A_TimeSincePriorHotkey < 200){
              Send, {LAlt Down}
              KeyWait, LAlt
              Send, {LAlt Up}
          }
      return
      

      if 语句会检查LAlt 热键是否在最后的200 毫秒内被使用过,如果是,它将保持LAlt 键向下,当LAlt 物理上升时(你松开键),它将在操作系统中反映相同。 否则,它将什么也不做,例如,如果序列是LAlt,某个其他键然后再次LAlt,它不会激活,或者如果按下LAlt 之间的时间不够快(超过@ 987654329@ms) 它也不会激活。

      【讨论】:

      • 这实际上不起作用:ALT 键被此函数占用,例如 ALT+TAB 不再起作用。
      【解决方案4】:

      这可以检测两次快速 ALT 按下:

      ~LAlt::
          If (A_PriorHotKey = "~LAlt") AND (A_TimeSincePriorHotkey < 200){
              SoundBeep, 750, 500
          }
      return
      

      它基于Shayan's answer,但我添加了~ 以使其工作。没有它,ALT+TAB 和其他 ALT+xxx 不起作用。

      【讨论】:

        猜你喜欢
        • 2018-03-18
        • 1970-01-01
        • 1970-01-01
        • 2021-06-20
        • 2019-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-04
        相关资源
        最近更新 更多