Modifying working mouse volume control code

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
june
Posts: 5
Joined: 23 Aug 2018, 04:25

Modifying working mouse volume control code

23 Aug 2018, 06:51

Hello.

I have spend the last few days tumbling around with ahk basics trying to make working mouse volume control.
As google have come to help I found numerous threads in the forum with many options making my hands itchy to try many other things.
I even made my joystick do things as well volume control but I still wanted to do that particular thing with my mouse. So I continued searching and I found awesome solution in the post from Lexikos in this thread https://autohotkey.com/board/topic/8011 ... h-rbutton/ .
It works wonderful but soon I had cramps because I have big fingers and I have to cram my fingers while holding the right button and scrolling in the same time.
So I tried to modify it with replacing rbutton with xbutton2 but I failed. Well, actually worked but having similar problem the way rbutton had in the simpler codes in the beginning of the thread. I can`t write in that thread because it`s archived.

So my question is how to modify Lexikos code which is the one bellow replacing RButton with xbutton2 and still maintain the functions.

Code: Select all

; Concept: Implement RButton as a modifier for other hotkeys 

 ;          by monitoring the mouse position and button state. 



 ; Requires:

;   - AutoHotkey (any version should be fine)

;   - A mouse with a wheel

;   - Two or more fingers (optional but recommended) 



 ; Start with wheel/MButton hotkeys disabled: 

gosub DisableRButtonHotkeys



RButton::

     ; Enable hotkeys while RButton is down. 

    Hotkey WheelUp,   On

    Hotkey WheelDown, On

    Hotkey MButton,   On

     ; Determine initial mouse position. 

    CoordMode Mouse, Screen

    MouseGetPos x1, y1

    Loop

    {

         ; If another hotkey has been triggered... 

        if (A_ThisHotkey != "RButton")

        {

             ; Stop monitoring the other conditions (below).  Just wait 

             ; for the button to be released, then disable the hotkeys. 

            KeyWait RButton

            break

        }

         ; If button has been released... 

        if !GetKeyState("RButton", "P")

        {

             ; Press and release the button. 

            MouseClick R

             ; Break out of the loop to disable the hotkeys. 

            break

        }

        MouseGetPos x2, y2

         ; If mouse has moved... 

        if (x1 != x2 || y1 != y2)

        {

             ; Set speed to "instant" for best results. 

            SetDefaultMouseSpeed 0

             ; Press the button down (at the initial mouse position). 

            Click Right Down %x1%, %y1%

             ; Move back to where the user had moved the mouse. 

            MouseMove x2, y2

             ; Wait for the button to be released. 

            KeyWait RButton

             ; Release the button. 

            Click Right Up

             ; Break out of the loop to disable the hotkeys. 

            break

        }

         ; Yield CPU time to other applications. 

        Sleep 1

    }

     ; Disable the hotkeys. 

    DisableRButtonHotkeys:

    Hotkey WheelUp,   Off

    Hotkey WheelDown, Off

    Hotkey MButton,   Off

return



WheelUp::   Send {Volume_Up}

WheelDown:: Send {Volume_Down}

MButton::   Send {Volume_Mute}
English is not my native language so forgive me if for any mistakes and sound strange. :oops:
User avatar
Scr1pter
Posts: 1275
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: Modifying working mouse volume control code

23 Aug 2018, 09:59

Hi,

I've never worked with XButton and will never do,
so I would do it this way:

Remap your desired mouse button to a key by using your mouse software.
For example: Map the F8 key to mouse button 5.

Then you can change the AHK script and replace all rbuttons to F8, like this:

Code: Select all

; Concept: Implement F8 as a modifier for other hotkeys 
 ; by monitoring the mouse position and button state. 
 ; Requires:
;   - AutoHotkey (any version should be fine)
;   - A mouse with a wheel
;   - Two or more fingers (optional but recommended) 
 ; Start with wheel/MButton hotkeys disabled: 

gosub DisableF8Hotkeys

F8::
; Enable hotkeys while F8 is down. 
Hotkey WheelUp,   On
Hotkey WheelDown, On
Hotkey MButton,   On

; Determine initial mouse position. 
CoordMode Mouse, Screen
MouseGetPos x1, y1
Loop
{
  ; If another hotkey has been triggered... 
  if (A_ThisHotkey != "F8")
  {
    ; Stop monitoring the other conditions (below).  Just wait 
    ; for the button to be released, then disable the hotkeys. 
    KeyWait F8
    break
  }
  ; If button has been released... 
  if !GetKeyState("F8", "P")
  {

    ; Break out of the loop to disable the hotkeys. 
    break
  }

  MouseGetPos x2, y2

   ; If mouse has moved... 

  if (x1 != x2 || y1 != y2)
  {
    ; Set speed to "instant" for best results. 
    SetDefaultMouseSpeed 0
    ; Press the button down (at the initial mouse position). 
    ; Click Right Down %x1%, %y1%
    ; Move back to where the user had moved the mouse. 
    MouseMove x2, y2
    ; Wait for the button to be released. 
    KeyWait F8
    ; Release the button. 
    ; Break out of the loop to disable the hotkeys. 
    break
  }
  ; Yield CPU time to other applications. 
  Sleep 1
}

; Disable the hotkeys. 

DisableF8Hotkeys:
Hotkey WheelUp,   Off
Hotkey WheelDown, Off
Hotkey MButton,   Off
return

; Map the hotkeys
WheelUp::   Send {Volume_Up}
WheelDown:: Send {Volume_Down}
MButton::   Send {Volume_Mute}
You could try to replace all F8 by XButton2, but I can't guarantee it will work.
I tested this script and it made a good impression.
To be honest, I don't know if the mouse position functionality is needed (in this script).

Advice:
Copy the whole code, paste it in Notepad (or any editor) and use Search and Replace (mostly Ctrl+H).
Then replace F8 by ANY other key.

P.S. I deleted some code lines of the original script which contained CLICK RBUTTON.

Regards
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
User avatar
jackdunning
Posts: 126
Joined: 01 Aug 2016, 18:17
Contact:

Re: Modifying working mouse volume control code

23 Aug 2018, 15:07

This may not be what you want, but it's incredibly simple and I use it all the time. Interestingly, I found it in the AutoHotkey documentation for the #If directive.

I merely hover my mouse over the Windows Taskbar at the bottom of the Desktop and scroll the mouse wheel. The speaker volume goes up and down. No extra keys or mouse buttons needed.

I discuss it in detail as an example for using AutoHotkey directives in "Windows Volume Control Using Your Mouse Wheel and the AutoHotkey #If Directive."

Simple, but cool!

I also placed the code at the Free Scripts page (ChangeVolume.ahk)—includes both AutoHotkey Versions 1.1 and 2.0 examples.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Aqualest, jdfnnl and 198 guests