如何仅当鼠标位于屏幕的特定坐标而不干扰我的键盘时才激活脚本?

How do I activate a script only if the mouse is at a certain coordinate of the screen without interfering with my keyboard?

我编写了一个脚本,当鼠标坐标位于屏幕的最左边或最右边时,当按下它们时,我可以将键盘的媒体快捷方式映射到鼠标 LRM 按钮。虽然它确实有效,但我有奇怪的副作用:

使用键盘历史记录,我看到我的脚本不断发送“Alt Up”键,我这样做是为了释放“Alt Down”状态,但有些东西关闭了。

我的目标是当鼠标在某个坐标上时发送修改键,这样当我单击该鼠标按钮时,它会启动另一个 ahk 编程的快捷方式。但是想不通我的代码或者思路哪里有逻辑错误。

这是脚本:

; ------------------------
; Global Initializers
#InstallKeybdHook
#MaxThreadsPerHotkey 1

; ---------------------
; Control Spotify; position your mouse top-most edge and use L/M/R-mouse keys.

SetTimer, WatchCursorx, 1000
return

WatchCursorx:
CoordMode, Mouse, Screen
MouseGetPos, xpos, ypos

;Based on location of the mouse simulate shortcut activation

If (xpos == 2559 || xpos == 0)
{
    Send {Alt Down}
}
Else
{
    Send {Alt Up}
}
return

;Define shortcuts mentioned above

!RButton::
Send {Media_Next}
return

!LButton::
Send {Media_Prev}
return

!MButton::
send {Media_Play_Pause}
return

  

根据我的评论,试试这样:

CoordMode, Mouse, Screen

~RButton::
    MouseGetPos, xpos, ypos
    If (xpos == 2559 || xpos == 0)
    {
        Send {Media_Next}
        sleep, 500
        Send {esc} ' this gets rid of right context menu
    }
return

~LButton::
    MouseGetPos, xpos, ypos
    If (xpos == 2559 || xpos == 0)
        Send {Media_Prev}
return

~MButton::
    MouseGetPos, xpos, ypos
    If (xpos == 2559 || xpos == 0)
        Send {Media_Play_Pause}
return

请注意,前面的 ~ 允许原始鼠标单击通过,因此通常右键单击会出现上下文菜单。我添加了一个 SleepSend Escape 键来 dismiss 。 . . Ymmv

#If(docs) 就是为了这个。
您可以像这样使用它:

CoordMode, Mouse, Screen

#If, MouseOnTheRight()
LButton::SendInput, {Media_Prev}
RButton::SendInput, {Media_Next}
MButton::SendInput, {Media_Play_Pause}
#If

MouseOnTheRight()
{
    MouseGetPos, x
    return x == A_ScreenWidth - 1
}