Jump to content

A Variable Controlled Mouse Hook


OldCoder
 Share

Recommended Posts

I had created a hook to get the values of the mouse wheel and button a while back, but have recently wanted more from this small function. I wanted to be able to disable or reroute mouse events by simply placing values into variables, without the need for calling functions. The following is the result.

#cs
    MOUSE HOOK

    This function hooks, (captures), all mouse events and can programmatically disable or redirect them to other functions. Call MWHL_INIT() to initialize.

    The following variables either offer information or control the flow of mouse events:

        $MWHL   = Wheel turned up or down, (1=up,
              -1=down, 0=idle)
        $MBUT   = Middle Button, (1=click, 0=not)

        $BM, (Block Mouse), variables disable or control flow. By making them 1, they will disable an event, (ie. $BMMM=1 disables all Mouse Movement). By placing a function name in them, you can redirect the event to a function, (ie. $BMRD="CapRite" captures the right mouse button and sends it to the CapRite() function):

            $BMMM   = Mouse Move
            $BMLD   = Left Down
            $BMLU   = Left Up
            $BMRD   = Right Down
            $BMRD   = Right Up
            $BMMD   = Middle Down
            $BMMU   = Middle Up
            $BMMS   = Middle Scroll

    Be sure to set $BM variables to zero to re-enable normal functioning.

    Finally, to disable the hook, call MWHL(-1,-1,-1).
            
#ce

Func MWHL_INIT()
Global $MWHL,$BMMS=0,$BMLD=0,$BMLU=0, $BMRD=0, $BMRU=0, $BMMD=0,$BMMU=0,$BMMM=0,$MBUT,$mwhl_call=DllCall("kernel32.dll","hwnd","GetModuleHandle","ptr",0),$mwhl_back=DllCall("user32.dll","hwnd","SetWindowsHookEx","int",14,"ptr",DllCallbackGetPtr(DllCallbackRegister("MWHL","int","hwnd;uint;long")),"hwnd",$mwhl_call[0],"dword",0),$MVAR[12]=["","$BMMM","$BMLD","$BMLU","","$BMRD","$BMRU","","$BMMD","$BMMU","","$BMMS"]
EndFunc

Func MWHL($h,$m,$l)
If $h=-1 AND $m=-1 AND $l=-1 Then Return DllCall("user32.dll", "int", "UnhookWindowsHookEx", "hwnd", $mwhl_back[0])
Global $MWHL=BitShift(DllStructGetData(DllStructCreate("int X;int Y;dword mouseData",$l),3),16)/120, $MBUT=($m=0x208),$MEVENT=BitAND($m, 0xFFFF)
If $MEVENT<512 OR $MEVENT>522 Then Return
Local $MV=Execute($MVAR[$MEVENT-511])
If IsString($MV) Then
    Call($MV)
    Return 1
EndIf
Return $MV
EndFunc

I have tested this a fair amount and then searched the forum for anything just like this, and yes, I know there are other mouse hooks out there, but one of them doesn't seem to get the direction of the mouse wheel, and I just like the simplicity of this one. It's format may seem a bit tight, but I love things that have a lot of power and occupy a little space. Hope someone finds it useful.

Cheers, ;)

EDIT: BTW, yes, I know I didn't change the names of the calls from my old, original post, but I felt a bit nostalgic about the original functions and decided to leave their names as they are. If you like, change them to whatever you want. :)

*Formatting care of "BrewManNH" Limited. For all your code formatting needs, call BrewManNH's.

Edited by OldCoder
"Intelligence is the ability to adapt to change."                                      - Stephen Hawking                                        "...not the ability to exploit others."                                                  - OldCoder
Link to comment
Share on other sites

Is there a way to make the scripts you post keep their original formating, like the tabs? Also, is there a word wrap feature I am missing, (can't seem to find one)? Thanks. :)

Edited by OldCoder
"Intelligence is the ability to adapt to change."                                      - Stephen Hawking                                        "...not the ability to exploit others."                                                  - OldCoder
Link to comment
Share on other sites

Before pasting the code into the [ autoit] tag area, click the button on the editor screen in the top left corner, the tooltip for it says Toggle Editing Mode. This will keep the formatting of your code when you paste it, plus it's also handy for things like URLs or anything else that the forum software butchers when pasted into a message.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Ahhhhhhhhh! Very nice!

The word wrap thing would be a nice feature for them to add too, but I'm happy with this.

Thanks, man.

"Intelligence is the ability to adapt to change."                                      - Stephen Hawking                                        "...not the ability to exploit others."                                                  - OldCoder
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

×
×
  • Create New...