Jump to content

Remap Mouse Button?


Go to solution Solved by Nine,

Recommended Posts

When I press the middle mouse button I want to perform some key combos, then hold a different mouse button until I release the one I am pressing, then do some more key combos without passing the original mouse button to the active window.  I can get it all to work except that the button being pressed is also sent to the active window.

Is this possible?

I've tried a number of solutions but they all either block mouse movement (required), mess with mouse movement even when the button isn't pressed, or cause the script to think I released the button.

#include <Misc.au3>

Opt("WinTitleMatchMode", 2) ;match partial names of windows

MsgBox($MB_SYSTEMMODAL, "Window", "Script started.")

Func OneNotePan()
    Send("{alt}dy")
    MouseDown($MOUSE_CLICK_LEFT)
    While _IsPressed("04") ;do nothing while the middle button is held
    WEnd
    MouseUp($MOUSE_CLICK_LEFT)
    Send("{Alt}dt{Alt}h{Esc}{Esc}")
EndFunc

Func AcrobatPan()
    ;MsgBox($MB_SYSTEMMODAL, "Window", "Acrobat is active.")
    Send("{Space down}")
    MouseDown($MOUSE_CLICK_LEFT)
    While _IsPressed("04") ;do nothing while the middle button is held
    WEnd
    MouseUp($MOUSE_CLICK_LEFT)
    Send("{Space up}")
EndFunc

Func VisioPan()
    Send("{CtrlDown}{ShiftDown}")
    MouseDown($MOUSE_CLICK_RIGHT)
    While _IsPressed("04") ;do nothing while the middle button is held
    WEnd
    MouseUp($MOUSE_CLICK_RIGHT)
    Send("{CtrlUp}{ShiftUp}")
EndFunc

Func IdentifyApp()
    ;MsgBox($MB_SYSTEMMODAL, "Window", "Middle button down.")
    if WinActive("OneNote") Then
        OneNotePan()
    Elseif WinActive("Acrobat") Then
        AcrobatPan()
        ;MsgBox($MB_SYSTEMMODAL, "Window", "Acrobat is active.")
    Elseif WinActive("Visio") Then
        VisioPan()
        ;MsgBox($MB_SYSTEMMODAL, "Window", "Visio is active.")
    EndIf
EndFunc

while 1
    if _IsPressed("04") Then IdentifyApp()
    Sleep (500) ;keep script running
WEnd

 

Edited by unL33T
Link to comment
Share on other sites

20 minutes ago, Andreik said:

What do you mean by passing the button and who is active window? :huh:

When I click the middle mouse button I don't want the middle mouse button to be sent to whatever program is open.  Just whatever I put in my script. The script performs different actions depending on which window is active.

Edited by unL33T
Link to comment
Share on other sites

  • Solution

Maybe something along this :

#include <WinAPI.au3>
#include <WindowsConstants.au3>

Opt("MustDeclareVars", True)
Opt("WinTitleMatchMode", 2) ;match partial names of windows

Global $hHook, $hFunc, $bMiddle
HotKeySet('{ESC}', _Exit)
OnAutoItExitRegister(Close)

Main()

Func Main()
  $hFunc = DllCallbackRegister(MouseProc, 'lresult', 'int;int;int')
  $hHook = _WinAPI_SetWindowsHookEx($WH_MOUSE_LL, DllCallbackGetPtr($hFunc), _WinAPI_GetModuleHandle(0))

  While Sleep(10)
    If $bMiddle Then
      Switch IdentifyApp()
        Case 1 ; repeat for all apps
          ; do something before
          While $bMiddle
            Sleep(10)
          WEnd
          ; do something after
        Case 4
          Send("notepad down" & @CR)
          While $bMiddle
            Sleep(10)
          WEnd
          Send("notepad up" & @CRLF)
      EndSwitch
    EndIf

  WEnd
EndFunc   ;==>Main

Func MouseProc($iCode, $iwParam, $ilParam)
  If $iCode < 0 Then Return _WinAPI_CallNextHookEx($hHook, $iCode, $iwParam, $ilParam)
  Switch $iwParam
    Case $WM_MBUTTONDOWN, $WM_MBUTTONUP
      If IdentifyApp() Then
        $bMiddle = $iwParam = $WM_MBUTTONDOWN
        Return 1
      EndIf
  EndSwitch
  Return _WinAPI_CallNextHookEx($hHook, $iCode, $iwParam, $ilParam)
EndFunc   ;==>MouseProc

Func _Exit()
  Exit
EndFunc   ;==>_Exit

Func Close()
  _WinAPI_UnhookWindowsHookEx($hHook)
  DllCallbackFree($hFunc)
EndFunc   ;==>Close

Func IdentifyApp()
  Local Static $aList[] = ["OneNote", "Acrobat", "Visio", "[CLASS:Notepad]"]
  For $i = 0 To UBound($aList) - 1
    If WinActive($aList[$i]) Then Return $i + 1
  Next
EndFunc   ;==>IdentifyApp

 

Edited by Nine
corrected a small bug
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...