首页 > 解决方案 > 仅当鼠标位于屏幕的某个坐标而不干扰我的键盘时,如何才能激活脚本?

问题描述

我编写了一个脚本,当鼠标坐标位于屏幕的最左侧或最右侧时,我可以将键盘的媒体快捷方式映射到鼠标 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

  

标签: autohotkey

解决方案


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

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

请注意,前面~是让原始鼠标单击通过,因此通常右键单击时会出现上下文菜单。我添加了一个SleepandSend Escape键来关闭。. . ymmv


推荐阅读