Jump to content
Sign in to follow this  
Kiti

_MouseTrap

Recommended Posts

Kiti

Hello everyone!

Is there any way to make an "inverted" mouse trap? So basically I want a box-shaped area where the mouse cannot enter.

Another sollution would be a poligonal mousetrap with multiple coord parameters. In this case I'll make the mousetrap as the entire screen except that area where I don't want the mouse to go (this area is right near an edge of the screen).

Any ideeas? :)

Edited by Kiti

Share this post


Link to post
Share on other sites
Authenticity

Considering the shape would look like this:

|------------------------|--|
|------------------------|--|
|------------------------|--|
|------------------------|--|
|------------------------|--|
|------------------------|--|
|------------------------|--|
   Allowed                 Not allowed

It'll be easy to use the API function ClipCursor. Don't know other way.

Share this post


Link to post
Share on other sites
SpookMeister

Crude...

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

$x_min=200
$x_max=300
$y_min=200
$y_max=300

While 1
    Sleep(100) ; always a good idea so you dont max your cpu
    $a_pos=MouseGetPos()
    Select
        Case $a_pos[0] > $x_min And $a_pos[0] < $x_max And $a_pos[1] > $y_min And $a_pos[1] < $y_max
            MouseMove( $x_max - 10, $y_max - 10, 0)
    EndSelect
WEnd

Func Terminate()
    Exit 0
EndFunc ;==>

[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

Share this post


Link to post
Share on other sites
Kiti

Actually the shape should look like this:

|---------------------------|

|---------------------------|

|---------------------------|

|-------------------_____|

|-------------------|-------|

|-------------------|-------|

|-------------------|-------|

Allowed ----------Not allowed

SpookMeister

I've tried that, but if someone moves the mouse very fast, it can actually click inside that area before the script moves the mouse away. And the point is not to click there. :) You can try it yourself.

Share this post


Link to post
Share on other sites
Authenticity

lol forgive my simplicity but it'd be much easier to make the window take the while right side of the desktop and clip the mouse to exclude this part than making some polygonal mouse clip mechanism...

Share this post


Link to post
Share on other sites
Kiti

lol forgive my simplicity but it'd be much easier to make the window take the while right side of the desktop and clip the mouse to exclude this part than making some polygonal mouse clip mechanism...

But I do need the right side of my desktop :lmao: I just don't want to be able to click a certain bottom right area.:)

Share this post


Link to post
Share on other sites
SpookMeister

Hrm... perhaps if you made the check as part of an adlib every 20 ms or so and Disabled mouse clicks if they are in that area.


[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

Share this post


Link to post
Share on other sites
Kiti
torels

you could create a transparent gui with Popup and Topmost attributes and put it over the region you want to exclude from mouse actions... and then use SpookMeister's script (the one in his first post) to make it a bit fancier.

Anyway replacing sleep(100) with sleep(10) should avoid overloading your CPU and in the meantime speed up a bit the process :)


Some Projects:[list][*]ZIP UDF using no external files[*]iPod Music Transfer [*]iTunes UDF - fully integrate iTunes with au3[*]iTunes info (taskbar player hover)[*]Instant Run - run scripts without saving them before :)[*]Get Tube - YouTube Downloader[*]Lyric Finder 2 - Find Lyrics to any of your song[*]DeskBox - A Desktop Extension Tool[/list]indifference will ruin the world, but in the end... WHO CARES :P---------------http://torels.altervista.org

Share this post


Link to post
Share on other sites
MrCreatoR

Like this:

#include <MouseSetOnEvent_UDF.au3>

HotKeySet("^q", "_Quit")

Global $iLimit_Coord_Left       = 0
Global $iLimit_Coord_Top        = 40
Global $iLimit_Coord_Width      = 350
Global $iLimit_Coord_Height     = 25

_MouseSetOnEvent($MOUSE_PRIMARYDOWN_EVENT, "MousePrimaryDown_Event", "", "", 0, -1)

While 1
    Sleep(100)
WEnd

Func MousePrimaryDown_Event()
    Local $aMPos = MouseGetPos()
    
    If ($aMPos[0] >= $iLimit_Coord_Left And $aMPos[0] <= $iLimit_Coord_Left + $iLimit_Coord_Width) And _
        ($aMPos[1] >= $iLimit_Coord_Top And $aMPos[1] <= $iLimit_Coord_Top + $iLimit_Coord_Height) Then Return 1 ;Block mouse click
    
    Return 0
EndFunc

Func _Quit()
    Exit
EndFunc

Good example for partial restriction of mouse clicks.


 

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

Share this post


Link to post
Share on other sites
Kiti

Here's the final, working script, blocking you from clicking the clock (so you won't be able to change the current time):

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

$x_min=976
$x_max=@desktopwidth
$y_min=737
$y_max=@desktopheight

Global Const $WH_MOUSE_LL                     = 14

Global Const $MOUSE_MOVE_EVENT                = 512
Global Const $MOUSE_PRIMARYDOWN_EVENT        = 513
Global Const $MOUSE_PRIMARYUP_EVENT            = 514
Global Const $MOUSE_SECONDARYDOWN_EVENT        = 516
Global Const $MOUSE_SECONDARYUP_EVENT        = 517
Global Const $MOUSE_WHELLDOWN_EVENT            = 519
Global Const $MOUSE_WHELLUP_EVENT            = 520
Global Const $MOUSE_WHELLSCROLL_EVENT        = 522
Global Const $MOUSE_EXTRABUTTONDOWN_EVENT    = 523
Global Const $MOUSE_EXTRABUTTONUP_EVENT        = 524

Global $hKey_Proc                             = -1
Global $hM_Module                             = -1
Global $hM_Hook                             = -1
Global $aMouse_Events[1][1]



While 1
    Sleep(100) ; always a good idea so you dont max your cpu
    $a_pos=MouseGetPos()
    Select
        Case $a_pos[0] > $x_min And $a_pos[0] < $x_max And $a_pos[1] > $y_min And $a_pos[1] < $y_max
            _BlockMouseClicksInput(0)
            ToolTip('MouseClicks is disabled',0,0)
        Case $a_pos[0] < $x_min or $a_pos[0] > $x_max or $a_pos[1] < $y_min or $a_pos[1] > $y_max
            _BlockMouseClicksInput(1)
            ToolTip('MouseClicks is enabled',0,0)
    EndSelect
WEnd

Func Terminate()
    Exit 0
EndFunc ;==>



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



Func OnAutoItExit()
    If IsArray($hM_Hook) And $hM_Hook[0] > 0 Then
        DllCall("user32.dll", "int", "UnhookWindowsHookEx", "hwnd", $hM_Hook[0])
        $hM_Hook[0] = 0
    EndIf
    
    If IsPtr($hKey_Proc) Then
        DllCallbackFree($hKey_Proc)
        $hKey_Proc = 0
    EndIf
EndFunc




Func _MouseSetOnEvent($iEvent, $sFuncName="", $sParam1="", $sParam2="", $hTargetWnd=0, $iBlockDefProc=1)
    If $sFuncName = "" Then ;Unset Event
        If $aMouse_Events[0][0] < 1 Then Return 0
        Local $aTmp_Mouse_Events[1][1]
        
        For $i = 1 To $aMouse_Events[0][0]
            If $aMouse_Events[$i][0] <> $iEvent Then
                $aTmp_Mouse_Events[0][0] += 1
                ReDim $aTmp_Mouse_Events[$aTmp_Mouse_Events[0][0]+1][6]
                $aTmp_Mouse_Events[$aTmp_Mouse_Events[0][0]][0] = $aMouse_Events[$i][0]
                $aTmp_Mouse_Events[$aTmp_Mouse_Events[0][0]][1] = $aMouse_Events[$i][1]
                $aTmp_Mouse_Events[$aTmp_Mouse_Events[0][0]][2] = $aMouse_Events[$i][2]
                $aTmp_Mouse_Events[$aTmp_Mouse_Events[0][0]][3] = $aMouse_Events[$i][3]
                $aTmp_Mouse_Events[$aTmp_Mouse_Events[0][0]][4] = $aMouse_Events[$i][4]
                $aTmp_Mouse_Events[$aTmp_Mouse_Events[0][0]][5] = $aMouse_Events[$i][5]
            EndIf
        Next
        
        $aMouse_Events = $aTmp_Mouse_Events
        
        If $aMouse_Events[0][0] < 1 Then OnAutoItExit()
        
        Return 1
    EndIf
    
    If $aMouse_Events[0][0] < 1 Then
        $hKey_Proc     = DllCallbackRegister("_Mouse_Events_Handler", "int", "int;ptr;ptr")
        $hM_Module     = DllCall("kernel32.dll", "hwnd", "GetModuleHandle", "ptr", 0)
        $hM_Hook     = DllCall("user32.dll", "hwnd", "SetWindowsHookEx", "int", $WH_MOUSE_LL, _
            "ptr", DllCallbackGetPtr($hKey_Proc), "hwnd", $hM_Module[0], "dword", 0)
    EndIf
    
    $aMouse_Events[0][0] += 1
    ReDim $aMouse_Events[$aMouse_Events[0][0]+1][6]
    $aMouse_Events[$aMouse_Events[0][0]][0] = $iEvent
    $aMouse_Events[$aMouse_Events[0][0]][1] = $sFuncName
    $aMouse_Events[$aMouse_Events[0][0]][2] = $sParam1
    $aMouse_Events[$aMouse_Events[0][0]][3] = $sParam2
    $aMouse_Events[$aMouse_Events[0][0]][4] = $hTargetWnd
    $aMouse_Events[$aMouse_Events[0][0]][5] = $iBlockDefProc
EndFunc


Func _Mouse_Events_Handler($nCode, $wParam, $lParam)
    Local $iEvent = BitAND($wParam, 0xFFFF)
    
    If $aMouse_Events[0][0] < 1 Then
        OnAutoItExit()
        Return 0
    EndIf
    
    For $i = 1 To $aMouse_Events[0][0]
        If $aMouse_Events[$i][0] = $iEvent Then
            If $aMouse_Events[$i][4] <> 0 And Not _IsHoveredWnd($aMouse_Events[$i][4]) Then Return 0 ;Allow default processing
            Local $sExec = "Call($aMouse_Events[$i][1]"
            
            For $j = 2 To 3
                If $aMouse_Events[$i][$j] <> "" Then $sExec &= ", " & $aMouse_Events[$i][$j]
            Next
            
            $sExec &= ")"
            
            $iRet = Execute($sExec)
            
            If $aMouse_Events[$i][5] = -1 Then Return $iRet
            Return $aMouse_Events[$i][5] ;Block default processing (or not :))
        EndIf
    Next
EndFunc

Thank you all! :lmao:

Edit: Still, it was even nicer if there was a much easier way to do it :)

Edited by Kiti

Share this post


Link to post
Share on other sites
MrCreatoR

Here's the final, working script, blocking you from clicking the clock (so you won't be able to change the current time):

Code removed....

Thank you all! :think:

Edit: Still, it was even nicer if there was a much easier way to do it :)

I suggest to use it as include, not all mixed together (if you want to keep it simpler :lmao: ).

And you don't have to use the main loop...

And btw, it will not work for all of us, because of different monitor resolutions. Try this:

#include <MouseSetOnEvent_UDF.au3>

HotKeySet("^q", "_Quit")

$aClock_Pos = ControlGetPos("[CLASS:Shell_TrayWnd]", "", "TrayClockWClass1")

Global $iLimit_Coord_Left              = $aClock_Pos[0]
Global $iLimit_Coord_Top              = $aClock_Pos[1]
Global $iLimit_Coord_Width           = $aClock_Pos[2]
Global $iLimit_Coord_Height          = $aClock_Pos[3]

_MouseSetOnEvent($MOUSE_PRIMARYDOWN_EVENT, "_Mouse_Event", "", "", 0, -1)
;_MouseSetOnEvent($MOUSE_PRIMARYUP_EVENT, "_Mouse_Event", "", "", 0, -1) ;Not really needed
_MouseSetOnEvent($MOUSE_SECONDARYDOWN_EVENT, "_Mouse_Event", "", "", 0, -1)
_MouseSetOnEvent($MOUSE_SECONDARYUP_EVENT, "_Mouse_Event", "", "", 0, -1)

While 1
    Sleep(100)
WEnd

Func _Mouse_Event()
    Opt("MouseCoordMode", 0)
    
    Local $aMPos = MouseGetPos()
    
    If ($aMPos[0] >= $iLimit_Coord_Left And $aMPos[0] <= $iLimit_Coord_Left + $iLimit_Coord_Width) And _
        ($aMPos[1] >= $iLimit_Coord_Top And $aMPos[1] <= $iLimit_Coord_Top + $iLimit_Coord_Height) Then
        
        ToolTip('MouseClicks are disabled', 0, 0)
        
        Return 1 ;Block mouse click
    EndIf
    
    ToolTip('MouseClicks are enabled', 0, 0)
    
    Return 0
EndFunc

Func _Quit()
    Exit
EndFuncoÝ÷ Øêâ*.Á©íÛ"^¯*.q©Üç$Â+aZ)Ý£k¢cè{mç%¡É0whÁÊ'¶º%jëh×6#include <MouseSetOnEvent_UDF.au3>

HotKeySet("^q", "_Quit")

$hClock_Wnd = ControlGetHandle("[CLASS:Shell_TrayWnd]", "", "TrayClockWClass1")

_MouseSetOnEvent($MOUSE_PRIMARYDOWN_EVENT, "_Mouse_Event", "", "", 0, -1)
;_MouseSetOnEvent($MOUSE_PRIMARYUP_EVENT, "_Mouse_Event", "", "", 0, -1) ;Not really needed
_MouseSetOnEvent($MOUSE_SECONDARYDOWN_EVENT, "_Mouse_Event", "", "", 0, -1)
_MouseSetOnEvent($MOUSE_SECONDARYUP_EVENT, "_Mouse_Event", "", "", 0, -1)

While 1
    Sleep(100)
WEnd

Func _Mouse_Event()
    Local $aMouse_Pos = MouseGetPos()
    
    Local $aRet = DllCall("User32.dll", "int", "WindowFromPoint", _
        "long", $aMouse_Pos[0], _
        "long", $aMouse_Pos[1])
    
    If $aRet[0] = $hClock_Wnd Then Return 1 ;Block mouse click
EndFunc

Func _Quit()
    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

Share this post


Link to post
Share on other sites
GregoryAlan

This code only lets you block mouse clicks for an area

To release this, just add these functions to your script:

_MouseSetOnEvent($MOUSE_PRIMARYDOWN_EVENT)

;_MouseSetOnEvent($MOUSE_PRIMARYUP_EVENT) ;Not really needed

_MouseSetOnEvent($MOUSE_SECONDARYDOWN_EVENT)

_MouseSetOnEvent($MOUSE_SECONDARYUP_EVENT)

This "un-blocks" mouse events

(you still need to have #include <MouseSetOnEvent_UDF.au3> which is made by mrCreator, I believe Kiti has a link to it)

Share this post


Link to post
Share on other sites
bambamfox

hi,

sorry - don't want to start a new topic for my short question:

my _mousetrap function shall be globally active - the mouse is beeing unblocked, when changing the active window (alt+tab) (the script still running in the background)

#include <Misc.au3>

_MouseTrap(0,0,0,0)
sleep(5000)

does anyone have an idea?

thx ;)

Edited by bambamfox

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  

×