Sign in to follow this  
Followers 0
somdcomputerguy

MouseTrap.au3

2 posts in this topic

#1 ·  Posted (edited)

This script uses the _MouseTrap UDF to keep the mouse within a pre-defined box area. I originally wrote it to keep my girlfriends grandkid from clicking 'outside the box' in whatever browser game he was playing. Then she found it useful, and I thought others might too, so here it is. There is one occasional bug/minor nuisance that I have found, that is when it's 'remembering' it's position and size, it sometimes gets the size wrong, and uses the apps size instead of the GUI size. I'm sure there are more 'bugs' in the code, but for the most part it usually works correctly.. Using it is fairly easy, position and/or resize the GUI (arrow keys, CTRL+SHFT arrow keys), start it (F10), ESC quits. I hope others find this useful.

#include <WindowsConstants.au3>
#include <GuiConstantsEx.au3>
#include <Misc.au3>

If _Singleton("Mouse Trap", 1) = 0 Then
    MsgBox(0, "Warning", "Mouse Trap is already running", 2)
    Exit
EndIf

Opt("MustDeclareVars", 1)
Opt("GUICoordMode", 2)

Global $GUI, $Coords, $Title
Local $Button1, $Button2, $Button3, $Button4

If FileExists(@ScriptDir & "/MouseTrap.ini") Then
    Local $aValues = IniReadSection(@ScriptDir & "/MouseTrap.ini", "Value")
    Local $var = WinList()
    For $i = 1 to $var[0][0]
        If $var[$i][0] <> "" AND IsVisible($var[$i][1]) Then
            For $n = 1 To $aValues[0][0]
                Local $aValuesI = StringSplit($aValues[$n][1], ",")
                If $var[$i][0] = $aValues[$n][0] And Not BitAnd(WinGetState($aValues[$n][0], ""), 16) Then
                    $GUI = GUICreate("Mouse Trap", $aValuesI[1], $aValuesI[2], $aValuesI[3], $aValuesI[4], $WS_SIZEBOX + $WS_SYSMENU)
                    ExitLoop(2)
                EndIf
            Next
        EndIf
        $GUI = GUICreate("Mouse Trap", 550, 420, -1, -1, $WS_SIZEBOX + $WS_SYSMENU)
    Next
Else
    $GUI = GUICreate("Mouse Trap", 550, 420, -1, -1, $WS_SIZEBOX + $WS_SYSMENU)
EndIf

GUISetIcon("mouse.ico")
TraySetIcon("mouse.ico")

$Button1 = GUICtrlCreateButton(" Remember ", 10, 10, 75)
 GUICtrlSetResizing(-1, $GUI_DOCKALL)
$Button2 = GUICtrlCreateButton(" Forget ", -1, 5)
 GUICtrlSetResizing(-1, $GUI_DOCKALL)
$Button3 = GUICtrlCreateButton(" Start ", -1, 5)
 GUICtrlSetResizing(-1, $GUI_DOCKALL)
$Button4 = GUICtrlCreateButton(" Quit ", -1, 5)
 GUICtrlSetResizing(-1, $GUI_DOCKALL)

GUISetState()

Main()

Func Main()
    If FileExists(@ScriptDir & "/MouseTrap.ini") Then
        GUICtrlSetState($Button2, $GUI_ENABLE)
    Else
        GUICtrlSetState($Button2, $GUI_DISABLE)
    EndIf
    WinSetTrans("Mouse Trap", "", 195)
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                Exit
            Case $Button1
                Remember()
            Case $Button2
                Forget()
            Case $Button3
                Start()
            Case $Button4
                Exit
        EndSwitch
    Wend
EndFunc

Func Start()
    WinSetTrans("Mouse Trap", "", 0)
    $Coords = WinGetPos($GUI)
    While 1
        _MouseTrap($Coords[0], $Coords[1], $Coords[0] + $Coords[2], $Coords[1] + $Coords[3])
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
        EndSwitch
    WEnd
    _MouseTrap()
    Main()
EndFunc

Func Remember()
    While 1
        ToolTip("Click on the App now")
        Sleep(100)
        If _IsPressed("01") Then ExitLoop
    WEnd
    ToolTip("")
    $Coords = WinGetPos($GUI)
    $Title = WinGetTitle("[active]")
    IniWrite(@ScriptDir & "/MouseTrap.ini", "Value", $Title, $Coords[2] & "," & $Coords[3] & "," &  $Coords[0] & "," & $Coords[1])
    ControlSetText("Mouse Trap", "", " Remember ", "Remembered")
    Sleep(100)
    WinActivate("Mouse Trap", "")
    Main()
EndFunc

Func Forget()
    If FileDelete(@ScriptDir & "/MouseTrap.ini") Then
        MsgBox(0, "Mouse Trap", "I won't remember where to go now..", 3)
        Sleep(100)
        GUICtrlSetState($Button2, $GUI_DISABLE)
        WinActivate("Mouse Trap", "")
    EndIf
EndFunc

Func IsVisible($handle)
  If BitAnd(WinGetState($handle), 2) Then
    Return 1
  Else
    Return 0
  EndIf
EndFunc

It should be obvious by looking at the code, but I'll say it anyway, if one chooses to run this script, it does write to disk when 'remembering'.

mouse.ico

Edited by snowmaker

- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Share this post


Link to post
Share on other sites



I've updated the code. I don't know if anybody is actually using this, but it does run better (As in the 'minor bug/annoyance' was another 'goof by me'). I've gotten rid of all the hotkeys, and I've also found that reading the help file enables an AutoIt created GUI to be resized with a mouse, and controls to keep a position in that GUI when being resized..


- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

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