brooklynsweb
brooklynsweb

Reputation: 817

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

I cooked up a script that lets me map my keyboard's media shortcuts to my mouse LRM buttons when they are pressed while the mouse coordinate is leftmost or rightmost of the screen. While it does work, I'm having having strange side effects:

Using the keyboard history, I see that my script is constantly sending the "Alt Up" key, I did this so that it release the "Alt Down" state, but something is off.

My goal is to send an modifier key when a mouse is over a certain coordinate, so that when I click with that mouse button, it launches another ahk-programmed shortcut. But can't figure out where the logic error is in my code or thinking process.

Here's the script:

; ------------------------
; 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

  

Upvotes: 0

Views: 821

Answers (2)

0x464e
0x464e

Reputation: 6489

#If(docs) is meant for this.
You could use it for example like this:

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
}

Upvotes: 1

PGilm
PGilm

Reputation: 2312

Per my comments, try it like this:

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

Note, the preceding ~ lets the original mouse click go through so ordinarily the context menu will come up on right click. I add a Sleep and Send Escape key to dismiss . . . Ymmv

Upvotes: 1

Related Questions