为什么 运行 此脚本时空格键事件被抑制?

Why are spacebar events suppressed when running this script?

我制作了一个脚本,用我的左手在 windows 10 中平铺 windows,使用 dvorak 键盘布局。它按预期工作,除了它阻止 spacebar 事件到达脚本本身以外的任何程序。这是 运行 脚本时的键盘日志:

20  039 s   d   0.09    Space           
20  039 s   u   0.08    Space           
20  039 s   d   0.08    Space           
20  039 s   u   0.08    Space           
20  039 s   d   0.08    Space           
20  039 s   u   0.09    Space           
20  039 s   d   0.08    Space           
20  039 s   u   0.09    Space           
20  039 s   d   31.14   Space           
20  039 s   u   0.11    Space           
20  039 s   d   0.09    Space           
20  039 s   u   0.11    Space

两个 space 事件都被正确检测到,但仍被抑制。手动终止脚本解决问题。

  #MaxThreads 255 ; for the purpose of troubleshooting

    #e::return      
    #.::return 
    #o::return          
    #u::return       

    Space & u::  
    Space & .::
    Space & o::
    Space & e::
    send {LWin Down}
    spcup()       
    exit

    spcup()                                                                          
    {
    while GetKeyState("Space", "P") {
    rite := GetKeyState("u", "P")
    left := GetKeyState("o", "P")
    up := GetKeyState(".", "P") 
    down := GetKeyState("e", "P")           
    if (rite=1) {
        Sleep 75
        Send {Right}
        Sleep 75
        }
        else if (left=1) {
            Sleep 75
            Send {Left}
            Sleep 75 
            }   
        else if (up=1) {
            Sleep 75
            Send {Up}
            Sleep 75  
            }
        else if (down=1) {
            Sleep 75
            Send {Down}
            Sleep 75
            }
        else if !GetKeyState("Space", "P") { 
            sleep 75
            Send {LWinUp}
            exit
            }
    }
    }

PS,我用space是因为我的左windows键物理损坏了。 将 Space 绑定到自身并添加 "Space::Space" 行,部分工作,只有 up 事件被注册为热键,因此它仅在 spacebar 释放时触发,产生这个:

20  039 s   d   0.25    Space           
20  039 h   u   0.16    Space           
20  039 i   d   0.00    Space           
20  039 i   u   0.00    Space   

s=suppressed
h=hotkey
i=ignored(sent by autohotkey itself)

"Solved",有点像。如果没有 ~,前缀键无法保持其原始功能,因此将其发送到活动 window,这是不需要的。

isWindowFullScreen()
{
    ;checks if the specified window is full screen
    ;use WinExist of another means to get the Unique ID (HWND) of the desired window

    if  WinExist("A") {
    WinGet, style, Style, A
    WinGetPos ,,,winW,winH, A
    ; 0x800000 is WS_BORDER.
    ; 0x20000000 is WS_MINIMIZE.
    ; no border and not minimized
    retVal := ((style & 0x20800000) or winH < A_ScreenHeight or winW < A_ScreenWidth) ? 0 : 1
    Return, retVal
}
else {
return
}
}

#if isWindowFullScreen() = 0
#e::return       
#.::return 
#o::return          
#u::return
#Space::return 
Space::Space             
Space & u::                                                        
Space & .::
Space & o::
Space & e::
send {LWin Down}
spcup()       
exit



spcup()                                                                                   
{
while GetKeyState("Space", "P") {
loop {
rite := GetKeyState("u", "P")
left := GetKeyState("o", "P")
up := GetKeyState(".", "P") 
down := GetKeyState("e", "P")           
if (left=1) {
    Sleep 75
    Send {Left}
    Sleep 75 
    }   
    else if (up=1) {
        Sleep 75
        Send {Up}
        Sleep 75  
        }
    else if (down=1) {
        Sleep 75
        Send {Down}
        Sleep 75
        }
    else if (rite=1) {
        Sleep 75
        Send {Right}
        Sleep 75
        }
} until !GetKeyState("Space", "P") 
Sleep 75
Send {LWinUp}
exit
}
}

来自help documentation

Numpad0 & Numpad1::MsgBox You pressed Numpad1 while holding down Numpad0.
Numpad0 & Numpad2::Run Notepad

The prefix key loses its native function: In the above example, Numpad0 becomes a prefix key; but this also causes Numpad0 to lose its original/native function when it is pressed by itself. To avoid this, a script may configure Numpad0 to perform a new action such as one of the following:

Numpad0::WinMaximize A   ; Maximize the active/foreground window.
Numpad0::Send {Numpad0}  ; Make the release of Numpad0 produce a
Numpad0 keystroke. See comment below.

Fire on release: The presence of one of the above custom combination hotkeys causes the release of Numpad0 to perform the indicated action, but only if you did not press any other keys while Numpad0 was being held down. [v1.1.14+]: This behaviour can be avoided by applying the tilde prefix to either hotkey.

为您的每个 Space & 热键添加一个波浪号前缀 ~ 应该会使其按您预期的方式运行。或者,您可以添加 ~Space::Return.

请注意,这将始终发送 space,这可能会在不同的应用程序中产生意想不到的后果。例如,在浏览器中它会向下滚动类似于按下 向下翻页键。

这是我用于测试的代码的精简版:

~Space & u::
~Space & .::
~Space & o::
~Space & e::
aReplace := [[ "u" , "Right" ] , [ "o" , "Left" ] , [ "." , "Up" ] , [ "e" , "Down" ]]
Loop , % aReplace.Length()
    sReplace := ( SubStr( A_ThisHotkey , 0 , 1 ) = aReplace[ A_Index , 1 ] ) ? aReplace[ A_Index , 2 ] : sReplace
Send , #{%sReplace%}
Return