Jump to content

I need to block/hook the middle mouse wheel click


Synaps3
 Share

Recommended Posts

Hi,

I have a problem with my mouse that I can't fix manually, so instead of purchasing a new mouse, I am trying to use autoit to fix it.

I have almost fixed it here with the following code, BUT the UDF is a POS. It just randomly stops working after 1-5 clicks. It is the UDF thing here: https://www.autoitscript.com/forum/topic/64738-mouseonevent-udf/

My code is kludgy and creates a GUI just to block the middle mouse click. I just need to block the middle mouse click for a few seconds after it is clicked so it doesn't keep registering multiple clicks. As bad as it looks, it does work. It just stops working after a while.

Does any have some better mouse hook code?

Here is my code:

#include <GUIConstantsEx.au3>
#include <Misc.au3>
#include "MouseOnEvent.au3"

_MouseSetOnEvent($MOUSE_WHEELDOWN_EVENT, "ExtraButtonDown_Event")

Func _BlockMouseInput($iFlag)
    Switch $iFlag
        Case 0  ;off
            GUIDelete(WinGetHandle("BlockMouseMove"))
            _MouseTrap()
        Case 1  ;on
            $iPos=MouseGetPos()
            GUICreate("BlockMouseMove", 1, 1, $iPos[0],$iPos[1], $WS_POPUP, $WS_EX_TOPMOST)
            GUISetState()
            _MouseTrap($iPos[0],$iPos[1])
    EndSwitch
EndFunc

Func ExtraButtonDown_Event()
    ;MsgBox($MB_SYSTEMMODAL, "Title", "This message box will timeout after 10 seconds or select the OK button.")
    Sleep(500)
    _BlockMouseInput(1)
    Sleep(1000)
    _BlockMouseInput(0)
EndFunc

While 1
   GUIGetMsg()
WEnd

 

Link to comment
Share on other sites

This hook allows the first click, then disables it for a given delay, then re-enables it
The gui is for visualization only

#include <WinApi.au3>
#include <WindowsConstants.au3>
HotKeySet('{ESC}', '_Close')

Global Const $MSLLHOOKSTRUCT = $tagPOINT & ";dword mouseData;dword flags;dword time;ulong_ptr dwExtraInfo"
Global $hHook, $n, $delay = 2000
Local $hFunc, $pFunc, $hMod

$hFunc = DllCallbackRegister('_MouseProc', 'long', 'int;wparam;lparam')
$pFunc = DllCallbackGetPtr($hFunc)
$hMod = _WinAPI_GetModuleHandle(0)
$hHook = _WinAPI_SetWindowsHookEx($WH_MOUSE_LL, $pFunc, $hMod)

$gui = GuiCreate("test", 100, 80, -1, 20)
$label = GuiCtrlCreateLabel("", 10, 20, 80, 50)
GuiSetState()

While 1
    If GuiGetMsg() = -3 Then _Close()
    Sleep(10)
WEnd


Func _MouseProc($iCode, $iwParam, $ilParam)
    Static $t = $delay
    If $iCode < 0 Then Return _WinAPI_CallNextHookEx($hHook, $iCode, $iwParam, $ilParam)
    Local $info = DllStructCreate($MSLLHOOKSTRUCT, $ilParam) 
    Switch $iwParam
        Case $WM_MBUTTONDOWN
        If TimerDiff($t) < $delay Then Return 1  ;<<< disable
        $t = TimerInit()
        $n += 1
            GuiCtrlSetData($label, "Mbutton Down " & $n)
    EndSwitch
    Return _WinAPI_CallNextHookEx($hHook, $iCode, $iwParam, $ilParam)
EndFunc

Func _Close()
    _WinAPI_UnhookWindowsHookEx($hHook)
    DllCallbackFree($hHook)
    Exit
EndFunc

 

Link to comment
Share on other sites

4 hours ago, mikell said:

This hook allows the first click, then disables it for a given delay, then re-enables it
The gui is for visualization only

That is absolutely perfect. Thanks. Now I don't need a new mouse :)

 

4 hours ago, Bert said:

why do you need such a thing? I've never heard of a need to have this functionality. It's very interesting what you are trying to do, just not getting why you need it.  

My mouse registers many clicks when I click the mouse wheel once. There is something defective with the mouse. I tried to clean it, but it seems to be something wrong electronically.

This just makes it so that it only registers the first click when the wheel is pressed.

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