0
votes

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:

  • When I have caps lock on, ever few strokes the letter comes out lowercase.
  • When I use shift to type capital letters for an extended period of time, this will turn on caps lock

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

  
2
One thing is that every 1 second (based on SetTimer) you check mouse position and if not on an edge, send the Alt Up so that's why you get that so frequently, and that is probably interfering in other ways . . . - PGilm
Therefore, just flip the logic. Make some ordinary L M R click commands that first look to see where the mouse is, and if at edge of screen when pressed, sends those media keys, and else, send the unmodified L M R clicks. No SetTimer needed. In the meantime, you can also keep your Alt + L M R clicks as you had them for when you are actually pressing the Alt key and click a mouse button while not at an edge . . . - PGilm

2 Answers

1
votes

#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
}
1
votes

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