Jump to content

MouseOnEvent UDF!


MrCreatoR
 Share

Recommended Posts

What is the best way to restrict long mouse press (left button) allowing only short click or double click?

I've tried such code:

 

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

...

   _MouseSetOnEvent($MOUSE_PRIMARYUP_EVENT, "__Dummy", 0, 1)
   _MouseSetOnEvent($MOUSE_PRIMARYDOWN_EVENT, "Mouse_LeftClick", 0, 1)
   _MouseSetOnEvent($MOUSE_SECONDARYUP_EVENT, "__Dummy", 0, 1)
   _MouseSetOnEvent($MOUSE_SECONDARYDOWN_EVENT, "__Dummy", 0, 1)
   _MouseSetOnEvent($MOUSE_WHEELDOWN_EVENT, "__Dummy", 0, 1)
   _MouseSetOnEvent($MOUSE_WHEELUP_EVENT, "__Dummy", 0, 1)

...

Func Mouse_LeftClick()
   MouseDown($MOUSE_CLICK_LEFT) ; Set the left mouse button state as down.
   Sleep(10)
   MouseUp($MOUSE_CLICK_LEFT) ; Set the left mouse button state as up.
EndFunc

It works fine until I made multiple left clicks, after that it totally stops to prevent long clicks of left button.

Link to comment
Share on other sites

  • 1 month later...
  • 2 years later...
#include "MouseOnEvent.au3"
#include <AutoItConstants.au3>

HotKeySet('{ESC}', '_Exit')

Global $iHold_Time = 200
Global $iTimer = 0

_MouseSetOnEvent($MOUSE_PRIMARYDOWN_EVENT, '_Primary_Event', 0, 0)
_MouseSetOnEvent($MOUSE_PRIMARYUP_EVENT, '_Primary_Event', 0, 0)

While 1
    Sleep(10)
WEnd

Func _Primary_Event($iEvent)
    Switch $iEvent
        Case $MOUSE_PRIMARYDOWN_EVENT, $MOUSE_PRIMARYDBLCLK_EVENT
            $iTimer = TimerInit()
            AdlibRegister('_Timer', 10)
        Case $MOUSE_PRIMARYUP_EVENT
            AdlibUnRegister('_Timer')
            $iTimer = 0
    EndSwitch
EndFunc


Func _Timer()
    If $iTimer And TimerDiff($iTimer) > $iHold_Time Then
        MouseUp($MOUSE_CLICK_PRIMARY)
    EndIf
EndFunc

Func _Exit()
    Exit
EndFunc

 

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

Update:

Quote

 v2.4 [15.03.2020]
* Fixed crash issue when running under x64 script. Thanks to LarsJ (Gary Frost?).
* Added _MouseSetOnEvent_SetDblClckSpeed function to set double click speed.

 

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

  • 1 year later...

When you call $sFuncName of _MouseSetOnEvent more than twice, with a sleep of approx. greater than half a second, it breaks all events. lower sleeps work fine.

The intent is to fire off one and only one instance of a function call when you scroll wheel. As the UDF captures each instance of a notch of the scroll wheel, you can get several at once. So, a sleep or a semaphore with a sleep, or unbind then rebinding the mouse event with a sleep -- these all break after the first or second use.

 

#include "MouseOnEvent.au3"

HotKeySet("^x",  "endProgram")

global $NotepadWHND = WinGetHandle ( "Notepad")

_MouseSetOnEvent($MOUSE_WHEELSCROLLDOWN_EVENT, "_MouseWheel_Events", $NotepadWHND,-1)

waitForInterrupt()

Func waitForInterrupt()
    While 1

    WEnd
EndFunc   ;==>waitForInterrupt

Func _MouseWheel_Events($iEvent)
    ConsoleWrite("_MouseWheel_Events call..." & @CRLF)
    Switch $iEvent
        Case $MOUSE_WHEELSCROLLDOWN_EVENT
            MouseWheel_down()
        Case $MOUSE_WHEELSCROLLUP_EVENT
            MouseWheel_up()
    EndSwitch
EndFunc

Func MouseWheel_down()
    ConsoleWrite("[1] npd active - mouse wheel down" & @CRLF)
    sleep(555)
EndFunc
Func MouseWheel_up()
    ConsoleWrite("[1] npd active - mouse wheel up" & @CRLF)
EndFunc


Func endProgram()
    ConsoleWrite("quitting..." & @CRLF)
    Exit
EndFunc   ;==>endProgram

 

Edited by drwhopoe1
Link to comment
Share on other sites

  • 11 months later...

I have found a bug.

If you register a mouse wheel up event, trigger the event by scrolling up, then unregister it and register it again and trigger it -- it triggers twice! If you unregister and register the event once more, it triggers trice, etc. The expectation is that it will trigger exactly once, since you have registered it N+1 times and unregistered N times.

This happens:

  • only with the non-RI functions
  • only if $iBlockDefProc function argument is set to $MOE_RUNDEFPROC
  • only after you trigger the event (if you don't trigger it, it works as expected)

To reproduce:

#include "MouseOnEvent.au3"

_MouseSetOnEvent($MOUSE_WHEELSCROLLUP_EVENT, "onMouseWheelUp", 0, $MOE_RUNDEFPROC) ; register
MouseWheel($MOUSE_WHEEL_UP) ; trigger
Sleep(100) ; some delay to allow the event to be processed before we unregister it
_MouseSetOnEvent($MOUSE_WHEELSCROLLUP_EVENT) ; unregister
_MouseSetOnEvent($MOUSE_WHEELSCROLLUP_EVENT, "onMouseWheelUp", 0, $MOE_RUNDEFPROC) ; register
_MouseSetOnEvent($MOUSE_WHEELSCROLLUP_EVENT) ; unregister
_MouseSetOnEvent($MOUSE_WHEELSCROLLUP_EVENT, "onMouseWheelUp", 0, $MOE_RUNDEFPROC) ; register
ConsoleWrite("---")
MouseWheel($MOUSE_WHEEL_UP) ; trigger
Sleep(100) ; some delay to allow the event to be processed before we exit

Func onMouseWheelUp()
    ConsoleWrite("up" & @CRLF)
EndFunc

After it prints "---", you would expect it to print "up" once, but it printa it 3 times!

After some debugging, it looks like the UDF author forgot to set $i__MSOE_EventReturn to 1 somewhere. With $iBlockDefProc function argument being set to $MOE_RUNDEFPROC, once __MouseSetOnEvent_MainHandler() is called at least once, which is done by the first "MouseWheel($MOUSE_WHEEL_UP)" in the reproducible code, $i__MSOE_EventReturn gets set from 1 to 0 and is never set back to 1. With $i__MSOE_EventReturn now always being 0, _WinAPI_UnhookWindowsHookEx() never gets called when we remove the last event, so the hook never gets removed from the chain. When we later add an event, UDF sees that it's a first event and creates another, 2nd, hook with _WinAPI_SetWindowsHookEx(). Basically, hooks are always created but never removed when they should be, which causes these extra event triggers.

Tbh I don't understand why $i__MSOE_EventReturn is needed in the first place. If I understand the code correctly, it is used to delay the cleanup in case the event is being processed via __MouseSetOnEvent_MainHandler() -> _WinAPI_PostMessage() -> __MouseSetOnEvent_WM_CALL(),  but I don't see why the delay is necessary. Not very familiar with Win32 programming and haven't read too much into UDF's code, so I can easily be missing something. For now I have set $i__MSOE_EventReturn to always be 1 in my copy of the UDF, which fixes the reproducible code issue.

tl;dr: Calling _MouseSetOnEvent to unregister the last event, it unregisters it from the UDF but it doesn't remove the created WinAPI hook, it remains in the Windows's hook chain.

Edited by BakedCakes
Link to comment
Share on other sites

  • 2 weeks later...

Found another bug: RI doesn't callback on mouse wheel events when the mouse is moving, it only calls back when the mouse is stationary.

#include "MouseOnEvent.au3"

_MouseSetOnEvent_RI($MOUSE_WHEELSCROLLDOWN_EVENT, "onMouseWheelDown", 0, $MOE_RUNDEFPROC)
$counter = 0
While 1
    Sleep(10)
WEnd

Func onMouseWheelDown()
    ConsoleWrite($counter & @CRLF)
    $counter += 1
EndFunc

When running the above, if you move the mouse cursor around, for example to the left and right, while also scrolling down, onMouseWheelDown() will not be called. onMouseWheelDown() is called only when you scroll down while the mouse cursor is not changing the position.

_MouseSetOnEvent() (the non-RI one) doesn't have this bug and works even when the mouse is moving around.

Edited by BakedCakes
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...