Sign in to follow this  
Followers 0

MouseOnEvent UDF!


159 posts in this topic

Posted (edited)

This UDF allows to set an events handler (a hook) for Mouse device.

The beginning...

I searched for a way to disable the Mouse Primary click, and be able to call some function when the click event is received...

Big thanks to amel27 for this one, i only organized the whole stuff to UDF style.

Example:

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

HotKeySet("{ESC}", "_Quit")

_Example_Intro()
_Example_Limit_Window()

Func _Example_Intro()
    MsgBox(64, "Attention!", "Let's set event function for mouse wheel *scrolling* up and down", 5)

    ;Set event function for mouse wheel *scrolling* up/down and primary button *down* action (call our function when the events recieved)
    _MouseSetOnEvent($MOUSE_WHEELSCROLLDOWN_EVENT, "_MouseWheel_Events")
    _MouseSetOnEvent($MOUSE_WHEELSCROLLUP_EVENT, "_MouseWheel_Events")
    _MouseSetOnEvent($MOUSE_PRIMARYDOWN_EVENT, "_MousePrimaryDown_Event")

    Sleep(3000)

    ;UnSet the events
    _MouseSetOnEvent($MOUSE_WHEELSCROLLDOWN_EVENT)
    _MouseSetOnEvent($MOUSE_WHEELSCROLLUP_EVENT)
    _MouseSetOnEvent($MOUSE_PRIMARYDOWN_EVENT)

    ToolTip("")

    MsgBox(64, "Attention!", "Now let's disable Secondary mouse button up action, and call our event function.", 5)

    _MouseSetOnEvent($MOUSE_SECONDARYUP_EVENT, "_MouseSecondaryUp_Event", 0, 1)
    Sleep(5000)
    _MouseSetOnEvent($MOUSE_SECONDARYUP_EVENT)

    ToolTip("")
EndFunc

Func _Example_Limit_Window()
    Local $hGUI = GUICreate("MouseOnEvent UDF Example - Restrict events on specific window")

    GUICtrlCreateLabel("Try to click on that specific GUI window", 40, 40, 300, 30)
    GUICtrlSetFont(-1, 12, 800)
    GUICtrlCreateLabel("Press <CTRL + Q> to exit", 10, 10)
    GUISetState()

    _MouseSetOnEvent($MOUSE_PRIMARYDOWN_EVENT, "_MousePrimaryDown_Event", $hGUI)
    ;A little(?) bugie when you mix different events :(
    ;_MouseSetOnEvent($MOUSE_SECONDARYUP_EVENT, "_MouseSecondaryUp_Event", $hGUI)

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $GUI_EVENT_PRIMARYDOWN
                MsgBox(0, "", "Should be shown ;)")
        EndSwitch
    WEnd

    _MouseSetOnEvent($MOUSE_PRIMARYDOWN_EVENT)
    ;_MouseSetOnEvent($MOUSE_SECONDARYUP_EVENT)
EndFunc

Func _MouseWheel_Events($iEvent)
    Switch $iEvent
        Case $MOUSE_WHEELSCROLLDOWN_EVENT
            ToolTip("Wheel Mouse Button (scrolling) DOWN Blocked")
        Case $MOUSE_WHEELSCROLLUP_EVENT
            ToolTip("Wheel Mouse Button (scrolling) UP Blocked")
    EndSwitch

    Return 1 ;Block
EndFunc

Func _MousePrimaryDown_Event()
    ToolTip("Primary Mouse Button Down Blocked")
    Return 1 ;Block
EndFunc

Func _MouseSecondaryUp_Event()
    ToolTip("Secondary Mouse Button Up Blocked")
EndFunc

Func _Quit()
    Exit
EndFunc

Available Events Constants:

$MOUSE_MOVE_EVENT - Mouse moving.

$MOUSE_PRIMARYDOWN_EVENT - Primary mouse button down.

$MOUSE_PRIMARYUP_EVENT - Primary mouse button up.

$MOUSE_PRIMARYDBLCLK_EVENT - Primary mouse button double click.

$MOUSE_SECONDARYDOWN_EVENT - Secondary mouse button down.

$MOUSE_SECONDARYUP_EVENT - Secondary mouse button up.

$MOUSE_SECONDARYDBLCLK_EVENT - Secondary mouse button double click.

$MOUSE_WHEELDOWN_EVENT - Wheel mouse button pressed down.

$MOUSE_WHEELUP_EVENT - Wheel mouse button up.

$MOUSE_WHEELDBLCLK_EVENT - Wheel mouse button double click.

$MOUSE_WHEELSCROLL_EVENT - Wheel mouse scroll.

$MOUSE_WHEELSCROLLDOWN_EVENT - Wheel mouse scroll Down.

$MOUSE_WHEELSCROLLUP_EVENT - Wheel mouse scroll Up.

$MOUSE_XBUTTONDOWN_EVENT - Side mouse button down (usually navigating next/back buttons).

$MOUSE_XBUTTONUP_EVENT - Side mouse button up.

$MOUSE_XBUTTONDBLCLK_EVENT - [Not implemented yet] Side mouse button double click.

------------------------------------------

CHANGELOG:

v1.9 [22.07.2012]

* Script breaking version!

* Dropped AutoIt 3.3.0.0 support.

* Instead of $sParam1 and $sParam2, now $vParam used as last parameter.

* Event function ($sFuncName) now called with $iEvent as first parameter, and $vParam as second (both optional).

* Now $iBlockDefProc is set to -1 by default (event function can define whether to block event process or not, simply by returning 1 or 0).

* Fixed not working $MOUSE_PRIMARYDBLCLK_EVENT and $MOUSE_SECONDARYDBLCLK_EVENT,

now handled manually because windows does not always receive these events (depending on CS_DBLCLKS style).

(not tested properly, so these events will have "experimental" label for now).

* Fixed error related to "Subscript used with non-Array variable", caused when window with handle of $hTargetWnd parameter is not found (window closed).

* Examples updated.

v1.8 [02.06.2010]

* Fixed an issue with wrong handling when $MOUSE_XBUTTONUP/DOWN_EVENT and few other events are set.

* Fixed an issue when user attempts to set other function for the event that already have been set.

Now the function and other parameters are replaced for the current event.

* UDF file renamed (removed "Set" in the middle and "_UDF" at the end of the name).

* Cosmetic changes in the UDF code.

* Docs updated.

v1.7 [14.10.2009]

* Stability fixes. Thanks again to wraithdu.

v1.6 [13.10.2009]

* Fixed an issue with releasing the resources of mouse hook (Thanks to wraithdu).

v1.5 [09.10.2009]

+ Added wheel button up/down *scrolling* event recognition. Thanks to JRowe (http://www.autoitscript.com/forum/index.php?showtopic=103362).

* Fixed an issue with returning value from __MouseSetOnEvent_MainHandler - should call _WinAPI_CallNextHookEx before return (thanks to Yashied).

* Constants starting with MOUSE_EXTRABUTTON* renamed to MOUSE_XBUTTON*, as it should be in the first place.

* Few examples updated.

v1.4 [30.09.2009]

+ Added UDF header to the function.

+ Now the original events-messages (such as $WM_MOUSEMOVE) can be used as well.

+ Added missing events (althought i am not sure if they are still supported)

$MOUSE_PRIMARYDBLCLK_EVENT - Primary mouse button double click.
$MOUSE_SECONDARYDBLCLK_EVENT - Secondary mouse button double click.
$MOUSE_WHEELDBLCLK_EVENT - Wheel mouse button double click.
$MOUSE_EXTRABUTTONDBLCLK_EVENT - Side mouse button double click.

* Changed global vars and internal functions names to be more unique.

* Fixed variables declaration and misspelling.

v1.3 [27.10.2008]

* Added optional parameter $iBlockDefProc - Define wether the Mouse events handler will block the default processing or not (Default is 1, block). If this is -1, then user can Return from the event function to set processing operation (see the attached example «AutoDrag window.au3»).

v1.2 [05.04.2008]

* Added: [Optional] parameter $hTargetWnd, if set, the OnEvent function will be called only on $hTargetWnd window, otherwise will be standard Event processing.

Note: Can be a little(?) buggy when you mix different events :)

v1.1 [22.03.2008]

* Fixed: Incorrect ReDim when remove event from the array, it was causing UDF to crash script with error.

* Spell/Grammar corrections :)

* Added: An example of _BlockMouseClicksInput().

Download:

From Downloads: MouseOnEvent.zip

Attached: MouseOnEvent.zip

Old version:

MouseSetOnEvent_UDF_1.8.zip

MouseSetOnEvent_UDF_1.7.zip

MouseSetOnEvent_UDF_1.6.zip

MouseSetOnEvent_UDF_1.5.zip

MouseSetOnEvent_UDF_1.4.zip

MouseSetOnEvent_UDF_1.3.zip

Previous downloads: 146 + 200 + 804

Edited by MrCreatoR
madzero, IgImAx and jaberwacky like this

Share this post


Link to post
Share on other sites



Posted

One mounth, 51 downloads (views?), and no comments? :)

:)

Share this post


Link to post
Share on other sites

Posted

Hi,

seems to work quite fine! But I do not use OnEvent mode.

Mega

Share this post


Link to post
Share on other sites

Posted

Maybe the problem is that the title says "_MoseSetOnEvent"

Share this post


Link to post
Share on other sites

Posted

seems to work quite fine!

Thanks.

But I do not use OnEvent mode.

The UDF has nothing to do with OnEvent mode :) - you can use it with any mode :)

Share this post


Link to post
Share on other sites

Posted

Thanks.

The UDF has nothing to do with OnEvent mode :) - you can use it with any mode :)

:party: did just one short run and assumed the rest.

It is cool. This way, it is possible to add an option to BlockInput to just disable the mouse.

I tried this but it gets an error.

#include <MouseSetOnEvent_UDF.au3>
_blockInput()
ToolTip('MouseClicks is disabled')
Sleep(5000)
_blockInput(1)
ToolTip('MouseClicks is enabled')
Sleep(5000)

Func _blockInput($opt = 0)
    If $opt = 0 Then
        _MouseSetOnEvent($MOUSE_PRIMARYDOWN_EVENT, "_do")
        _MouseSetOnEvent($MOUSE_SECONDARYDOWN_EVENT, "_do")
    Else
        _MouseSetOnEvent($MOUSE_PRIMARYDOWN_EVENT)
        _MouseSetOnEvent($MOUSE_SECONDARYDOWN_EVENT)
    EndIf
EndFunc   ;==>_blockInput

Func _do()
EndFunc

Mega

Share this post


Link to post
Share on other sites

Posted

This is really useful, will certainly add this to my autoit directory on my desktop :)

Share this post


Link to post
Share on other sites

Posted

:) did just one short run and assumed the rest.

It is cool. This way, it is possible to add an option to BlockInput to just disable the mouse.

I tried this but it gets an error.

Oops :) , the error is because i did a mistake when the array is Redimmed... just find in the UDF this line:

ReDim $aTmp_Mouse_Events[$aTmp_Mouse_Events[0][0]+1][0]

and replace last [0] by [4]

And nice example btw, i will include it into the archive, but i changed it a little(?)...

#include <MouseSetOnEvent_UDF.au3>

_BlockMouseClicksInput(0)

ToolTip('MouseClicks is disabled')
Sleep(3000)

_BlockMouseClicksInput(1)

ToolTip('MouseClicks is enabled')
Sleep(3000)

Func _BlockMouseClicksInput($iOpt=0)
	If $iOpt = 0 Then
		_MouseSetOnEvent($MOUSE_PRIMARYUP_EVENT, "__Dummy")
		_MouseSetOnEvent($MOUSE_PRIMARYDOWN_EVENT, "__Dummy")
		_MouseSetOnEvent($MOUSE_SECONDARYUP_EVENT, "__Dummy")
		_MouseSetOnEvent($MOUSE_SECONDARYDOWN_EVENT, "__Dummy")
	Else
		_MouseSetOnEvent($MOUSE_PRIMARYUP_EVENT)
		_MouseSetOnEvent($MOUSE_PRIMARYDOWN_EVENT)
		_MouseSetOnEvent($MOUSE_SECONDARYUP_EVENT)
		_MouseSetOnEvent($MOUSE_SECONDARYDOWN_EVENT)
	EndIf
EndFunc   ;==>_BlockMouseClicksInput

Share this post


Link to post
Share on other sites

Posted

Updated first post:

CHANGELOG:

v1.1 [22.03.2008]

* Fixed: Incorrect ReDim when remove event from the array, it was causing UDF to crash script with error.

* Spell/Grammar corrections :)

* Added: An example of _BlockMouseClicksInput().

Share this post


Link to post
Share on other sites

Posted

Hi,

thanks for credit.

That is what I meant. (example)

Blocking the mouse is really helpful sometimes!

Great!

Mega

Share this post


Link to post
Share on other sites

Posted

Maybe the problem is that the title says "_MoseSetOnEvent"

The title of the topic and and the first post have a misspelling.

Share this post


Link to post
Share on other sites

Posted

Great work :)

I'm gonna start playing with !!

Share this post


Link to post
Share on other sites

Posted

This is useful... Thanks!

Share this post


Link to post
Share on other sites

Posted

Hi,

I tried to play around with the following script.

The intention is to demonstrate right mouse click in a specific application, say UltraEdit.

It is very simple, if you right mouse click (down/up) when UltraEdit is active, then a tooltip will be displayed.

If you right mouse click in another Window, it will perform normal right mouse click.

If middle mouse button is clicked, the application will exit.

I have two questions

1. what is the best way to make this program stay in memory to track mouse clicks until it is terminated?

sleep(2147483647) is a bad way I suppose, but for testing purpose, it is ok

another way is

while 1

wend

2. this is really annoying me

After this program exits, left mouse click is not able to activate other Window

Only after I press Alt+Tab to switch to other Window, left mouse click starts to work normally

Any inputs will be appreciated :)

#include <MouseSetOnEvent_UDF.au3>

_MouseSetOnEvent($MOUSE_SECONDARYUP_EVENT, "RightMouseUp")
_MouseSetOnEvent($MOUSE_SECONDARYDOWN_EVENT, "RightMouseDown")
_MouseSetOnEvent($MOUSE_WHELLUP_EVENT , "MiddleMouseUp")

sleep(2147483647)

Func RightMouseUp()
  If WinActive("UltraEdit") Then
	ToolTip("Right Mouse up")
  Else
	MouseUp("Right")
  EndIf
EndFunc

Func RightMouseDown()
  If WinActive("UltraEdit") Then
	ToolTip("Right Mouse down")
  Else
	MouseDown("Right")
  EndIf
EndFunc

Func MiddleMouseUp()
  _MouseSetOnEvent($MOUSE_SECONDARYUP_EVENT);Enable mouse button back.
  _MouseSetOnEvent($MOUSE_SECONDARYDOWN_EVENT);Enable mouse button back.
  _MouseSetOnEvent($MOUSE_WHELLUP_EVENT);Enable mouse button back.
  exit
EndFunc

Share this post


Link to post
Share on other sites

Posted

First post updated:

v1.2 [05.04.2008]

* Added: [Optional] parameter $hTargetWnd, if set, the OnEvent function will be called only on $hTargetWnd window, otherwise will be standard Event processing.

Note: Can be a little(?) buggy when you mix different events :)

Share this post


Link to post
Share on other sites

Posted

what is the best way to make this program stay in memory to track mouse clicks until it is terminated?

sleep(2147483647) is a bad way I suppose

You can just set a loop:

While 1
   Sleep(10)
WEnd

:)

Share this post


Link to post
Share on other sites

Posted (edited)

I know it's an older topic, but this is one of the few UDF's posted lately that I actually needed to use in a project of mine.

Awesome UDF :)

EDIT: I may have found a possible bug (Unless this is intended behavior)

Try this code in SciTe:

#include <MouseSetOnEvent_UDF.au3>

_MouseSetOnEvent(516, "BlowUp")
Sleep(5000)
_MouseSetOnEvent(516)
Func BlowUp()
	MsgBox(0, "Kaboom", "You all die")
EndFunc

Now press the right mouse button. You see the MsgBox. Now when you exit the MsgBox, the context menu appears as if you pressed the button twice. Is this intended?

Edited by KentonBomb

Share this post


Link to post
Share on other sites

Posted

Great udf!

Share this post


Link to post
Share on other sites

Posted

damn, i really want to use your udf !!!

but am i the only one who has problems ? i have got the "MouseSetOnEvent_UDF.au3", i tried it with v 3.2.8.1 and 3.2.10.0 and the latest beta, but i do not have the following functions:

- DllCallbackRegister

- DllCallbackGetPtr

- DllCallbackFree

and so on. so please where can i find these ? i've been searching the forum for hours, but there seems to be a problem with lots of functions with similar names.

thx

j.

Share this post


Link to post
Share on other sites

Posted

damn, i really want to use your udf !!!

but am i the only one who has problems ? i have got the "MouseSetOnEvent_UDF.au3", i tried it with v 3.2.8.1 and 3.2.10.0 and the latest beta, but i do not have the following functions:

- DllCallbackRegister

- DllCallbackGetPtr

- DllCallbackFree

and so on. so please where can i find these ? i've been searching the forum for hours, but there seems to be a problem with lots of functions with similar names.

thx

j.

It requires Beta.

Share this post


Link to post
Share on other sites

Posted

It requires Beta.

Well, actualy those introduced in 3.2.10.0 :).

Share this post


Link to post
Share on other sites

Posted

Hi first of all i want to say that u`ve made a good think doing this udf...and second i wanned to ask if there`s a way to integrate this udf with the hover udf to create the 3rd state of a button (pressed) thta would be very userfull

Share this post


Link to post
Share on other sites

Posted

N!ce.

Any way of getting it to detect the scrollwheel, scrolled up or down?

__________

Test12

Share this post


Link to post
Share on other sites

Posted

N!ce.

Any way of getting it to detect the scrollwheel, scrolled up or down?

__________

Test12

Yes:

#include <MouseSetOnEvent_UDF.au3>

_MouseSetOnEvent($MOUSE_WHELLSCROLL_EVENT, "Scroll_Event")

Sleep(3000)

_MouseSetOnEvent($MOUSE_WHELLSCROLL_EVENT) ;Unset


Func Scroll_Event()
	ConsoleWrite("Scroll Event Fired" & @LF)
EndFunc

Share this post


Link to post
Share on other sites

Posted (edited)

No sorry, like in :

#include <MouseSetOnEvent_UDF.au3>
$hGUI = WinGetHandle("Blaaaa")

_MouseSetOnEvent($MOUSE_WHELLSCROLL_EVENT_UP, "MOUSE_WHELLSCROLL_UP", "", "", $hGUI)
_MouseSetOnEvent($MOUSE_WHELLSCROLL_EVENT_DOWN, "MOUSE_WHELLSCROLL_DOWN", "", "", $hGUI)

While 1
	Sleep(100)
WEnd

Exit

Func MOUSE_WHELLSCROLL_UP()
	ConsoleWrite("UP" & @CR)
EndFunc

Func MOUSE_WHELLSCROLL_DOWN()
	ConsoleWrite("DOWN" & @CR)
EndFunc

_____________________

Test12

Edited by test12

Share this post


Link to post
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
Sign in to follow this  
Followers 0