Jump to content

Restricting Mouse Movement


romulous
 Share

Recommended Posts

It looks nothing like my script, so I guess I don't have to help. :)

edit: Just kidding. I gave it a try by the way, and he's right. If I try to unrestrict the mouse, it doesn't unrestrict until I minimize a window.

Maybe it's that mousetrap things fault. I'd just use something else.

Edited by Nevin
Link to comment
Share on other sites

Hi Nevin,

lol - I should have said "I have totally used all of Nevin's code without changing a thing!" ^_^:)

It's a really odd problem, but I'll give yours and Hatcheda's version a go and see if it behaves better than _MouseTrap. I mainly went with _MouseTrap to begin with because it was only one line of code...

Regards,

CM

Edited by romulous
Link to comment
Share on other sites

Ok, I now have two working versions - including the _MouseTrap one (yeah, I managed to fix that problem with a window action being required to deactivate the script) :)

First, Nevin/Hatcheda's version (modified slightly so I didn't have to hardcode the screen resolution into the script):

HotKeySet ( "{ESC}", "_Toggle" ); ESC key is the hotkey to disable the script

$Height = @DesktopHeight; Sets the resolution (height) to a variable - used in the main loop so you don't have to hardcode the desktop resolution
$Restrict = True; Sets the initial value of the variable to True, so that the main part of the script runs
$Width = @DesktopWidth; Sets the resolution (width) to a variable - used in the main loop so you don't have to hardcode the desktop resolution

While 1
    Sleep ( 10 )
    If $Restrict = True Then
        $mousepos = mousegetpos()
        If $mousepos[0] >= $Width Then MouseMove ( $Width-5, $mousepos[1], 0 ); $Width-5 is used because otherwise, the mouse cursor was kept just on the second monitor, not the first monitor
    EndIf
WEnd

Func _Toggle()
    If $Restrict = True Then
        Global $Restrict = False
    Else
        Global $Restrict = True
    EndIf
EndFunc

And now, the modified (with a single line) _MouseTrap version:

#include <Misc.au3>; Required for UDF _MouseTrap

HotKeySet ( "{ESC}", "Disable" ); ESC key is the hotkey to disable the script

$Height = @DesktopHeight; Sets the resolution (height) to a variable - used in the _MouseTrap loop so you don't have to hardcode the desktop resolution
$Restrict = True; Sets the initial value of the variable to True, so that the _MouseTrap part of the script runs
$Width = @DesktopWidth; Sets the resolution (width) to a variable - used in the _MouseTrap loop so you don't have to hardcode the desktop resolution

While 1
    Sleep ( 100 )
    If $Restrict = True Then
        _MouseTrap ( 0, 0, $Width, $Height ); Stops the mouse from moving outside the current resolution (i.e. the first monitor)
    EndIf
WEnd

Func Disable()
    If $Restrict = True Then
        Global $Restrict = False
        _MouseTrap()
    Else
        Global $Restrict = True
    EndIf
EndFunc

The trick with the _MouseTrap version was the addition of the _MouseTrap() line in the Disable function. Both tested, both work without problems as far as I can see (fingers crossed). Two different ways to achieve basically what I wanted to achieve ^_^

CM

Edited by romulous
Link to comment
Share on other sites

Interesting. Well thanks for telling us how it came out. I'll probably be using it to mess with people some time or another.

By the way, Moustrap may only be one line, but how large is the actual function its calling in Misc.au3? :)

;===============================================================================
;
; 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 at charter dot 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)
    Local $av_ret
    If @NumParams == 0 Then
        $av_ret = DllCall("user32.dll", "int", "ClipCursor", "int", 0)
    Else
        If @NumParams == 2 Then
            $i_right = $i_left + 1
            $i_bottom = $i_top + 1
        EndIf
        Local $Rect = DllStructCreate("int;int;int;int")
        If @error Then Return 0
        DllStructSetData($Rect, 1, $i_left)
        DllStructSetData($Rect, 2, $i_top)
        DllStructSetData($Rect, 3, $i_right)
        DllStructSetData($Rect, 4, $i_bottom)
        $av_ret = DllCall("user32.dll", "int", "ClipCursor", "ptr", DllStructGetPtr($Rect))
        ;       DllStructDelete($Rect)
    EndIf
    Return $av_ret[0]
EndFunc   ;==>_MouseTrap
Link to comment
Share on other sites

No problem Nevin. The _MouseTrap() command is the command used to de-activate the mousetrap itself I understand from the helpfile, it makes sense that it has to be in there somewhere. You do have a point though, _MouseTrap is a single line to code into a script if you want to use it, but it's a few lines inside the Misc.au3 file that you have to include in your script to use it in the first place (and if you included all those lines, the _MouseTrap version would actually be longer than yours and Hatcheda's). Still puzzled as to why the window minimise/un-minimise is required to deactivate the _MouseTrap if you don't use that de-activation line though.

Regards,

CM

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