Jump to content

Restrict cursor to area on the screen


GaryFrost
 Share

Recommended Posts

Requires beta version

#include <GuiConstants.au3>

Opt("MustDeclareVars",1)

Dim $GUI, $coords[4], $msg

$GUI = GUICreate("Mouse Trap Example", 392, 323)

GUISetState()

While 1
    $coords = WinGetPos($GUI)
    _MouseTrap($coords[0], $coords[1], $coords[0] + $coords[2], $coords[1] + $coords[3])
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case Else
    ;;;
    EndSelect
WEnd
_MouseTrap()
Exit

UDF:

;===============================================================================
;
; Description:        _MouseTrap
; Parameter(s):    $i_left - Left coord
;                        $i_top - Top coord
;                        $i_right - Right coord
;                        $i_bottom - Bottom coord
; User CallTip:    _MouseTrap([$i_left = 0[, $i_top = 0[, $i_right = 0[, $i_bottom = 0]]]]) Confine the Mouse Cursor to specified coords. (required: <Misc.au3>)
; Author(s):        Gary Frost (custompcs@charter.net)
; Note(s):            Use _MouseTrap() with no params to release the Mouse Cursor
;
;===============================================================================
Func _MouseTrap($i_left = 0, $i_top = 0, $i_right = 0, $i_bottom = 0)
    If @NumParams == 0 Then
        DllCall("user32.dll", "int", "ClipCursor", "int", 0)
    Else
        If @NumParams == 2 And ($i_left > 0 Or $i_top > 0) Then
            $i_right = $i_left + 1
            $i_bottom = $i_top + 1
        EndIf
        Local $Rect = "int;int;int;int", $left = 1, $top = 2, $right = 3, $bottom = 4, $r
        $r = DllStructCreate($Rect)
        If @error Then Return -1
        DllStructSetData($r, $left, $i_left)
        DllStructSetData($r, $top, $i_top)
        DllStructSetData($r, $right, $i_right)
        DllStructSetData($r, $bottom, $i_bottom)
        DllCall("user32.dll", "int", "ClipCursor", "ptr", DllStructGetPtr($r))
        DllStructDelete($r)
    EndIf
EndFunc  ;==>_MouseTrap
Edited by gafrost

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

Cool! Just Tried it with params of 0,0,0,0 and It locked the cursor in the corner of the screen. Useful for locking the cursor for security reasons.

HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.
Link to comment
Share on other sites

How about having the right and bottom fields default to the left and top ones? Then you could call _CursorClip(0, 0) or _CursorClip(200, 200) and 'stick' the cursor to that location.

Link to comment
Share on other sites

You might want to mention that it's necessary to _CursorClip() before setting a different region.

Also, on my system this works better (no movement):

If @NumParams = 2 Then
    $i_right = $i_left + 1
    $i_bottom = $i_top + 1
EndIf

On my system _CursorClip(0, 0) hides the cursor. That's a handy feature that should probably be documented!

Edited by LxP
Link to comment
Share on other sites

You might want to mention that it's necessary to _CursorClip() before setting a different region.

Also, on my system this works better (no movement):

If @NumParams = 2 Then
    $i_right = $i_left + 1
    $i_bottom = $i_top + 2
EndIf

On my system _CursorClip(0, 0) hides the cursor. That's a handy feature that should probably be documented!

<{POST_SNAPBACK}>

Ok, changed the if statement added 1 to each only if left or top are not zero

no need to call _ClipCursor() before setting diff region at leas not on my system here's an example

#include <GuiConstants.au3>

HotKeySet("n", "_NewCoords")

Opt("MustDeclareVars",1)

Dim $GUI, $coords[4], $msg

$GUI = GUICreate("Clip Cursor Example", 392, 323)

GUISetState()
_CursorClip(0,0)
While 1
;~   $coords = WinGetPos($GUI)
;~   _CursorClip($coords[0], $coords[1], $coords[0] + $coords[2], $coords[1] + $coords[3])
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case Else
   ;;;
    EndSelect
WEnd
_CursorClip()
Exit


Func _NewCoords()
_CursorClip(50,50, 100, 100)
EndFunc

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

Oops, must have been calling it improperly -- it's working as expected now (still hides the cursor at (0, 0) though).

Your UDF's blurb mentions that it needs <Misc.au3> -- why is this?

Link to comment
Share on other sites

Oops, must have been calling it improperly -- it's working as expected now (still hides the cursor at (0, 0) though).

Your UDF's blurb mentions that it needs <Misc.au3> -- why is this?

<{POST_SNAPBACK}>

For when it gets added to AutoIt as a UDF :whistle:

EDIT: Gafrost beat me to it.. :dance: lol

Edited by layer
FootbaG
Link to comment
Share on other sites

great, well renaming the function _MouseTrap, lil more descriptive of what it does, not sure if it's the same thing that Larry's does in the dll, even if it is the all the functions in dll should be able to be done directly thru the beta version now.

Gary

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

I like _MouseTrap hahah... I think most of Larrys Dll funcs are now native AutoIt commands with Dll*** functions except for _MouseTrap... Sooo, I don't see why this couldn't be added.

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