首页 > 解决方案 > 将光标移动到水平方向的代码问题

问题描述

我使用的代码在激活时将光标的移动限制在水平方向,但我遇到了以下问题:切换脚本时,光标从其原始位置向下移动,而不是保持在相同的高度。

我正在使用的代码是这个:

!s:: ; Hotkey will toggle status
Confine := !Confine
MouseGetPos ,, SetY
Confine ? ClipCursor( 0 , SetY , A_ScreenWidth , SetY+1 ) : DllCall( "ClipCursor" )
Return

ClipCursor( x1=0 , y1=0 , x2=1 , y2=1 ) {
    VarSetCapacity( R , 16 , 0 )
    NumPut( x1 , &R + 0 )
    NumPut( y1 , &R +4 )
    NumPut( x2 , &R +8 )
    NumPut( y2 , &R +12 )
Return DllCall( "ClipCursor" , UInt , &R )
}

我需要光标不要跳跃。如何解决这种行为?


这个问题与我之前发布的另一个问题有关:

如何使用按键打开/关闭 AHK 脚本?

标签: autohotkey

解决方案


坐标 SetY 必须相对于桌面(整个屏幕)。

!s:: ; Hotkey will toggle status
    Confine := !Confine
    CoordMode, Mouse, Screen ; If this command is not used the coordinates are relative to the active window.
    MouseGetPos ,, SetY
    If (Confine)
        ClipCursor( Confine, 0, SetY, A_ScreenWidth+1, SetY+1 )
    else
        DllCall( "ClipCursor" )
Return

ClipCursor( Confine=True, x1=0 , y1=0 , x2=1 , y2=1 ) {
    VarSetCapacity( R , 16 , 0 )
    NumPut( x1 , &R + 0 )
    NumPut( y1 , &R +4 )
    NumPut( x2 , &R +8 )
    NumPut( y2 , &R +12 )
    Return DllCall( "ClipCursor" , UInt , &R )
}

https://www.autohotkey.com/docs/commands/CoordMode.htm


推荐阅读