Jump to content
Sign in to follow this  
E1M1

Mouse lock - Locks mouse in selected window

Recommended Posts

E1M1

I just made easy to use mouse locker, which locks your mouse in selected window.

My inspiration to write this program: I was in middle of fight when I clicked out of game window, as a result of this game window went behind other window and when I switched back to game window i was almost dead.

Usages

* Lock mouse in selected window and keep it active.

- Can be used with windowed games to avoid accidents that happen if you click out of window and as a result window goes behind other window.

Features

*Lock/unlock mouse in selected window

*Prevets selected window from going background

*Dynamic key combination binding

*Tutorials for beginners

Bugs/ToDo

*Post please

#include <Misc.au3>
#include <GuiComboBox.au3>
#include <Array.au3>

Global $ini = @ScriptDir & "\mouselock.ini"
Global $KeyList = "Left mouse button|Right mouse button|Middle mouse button (three-button mouse)|Windows 2000/XP: X1 mouse button|Windows 2000/XP: X2 mouse button|BACKSPACE |TAB |CLEAR |ENTER |SHIFT |CTRL |ALT |PAUSE |CAPS LOCK |ESC |SPACEBAR|PAGE UP |PAGE DOWN |END |HOME |LEFT ARROW |UP ARROW |RIGHT ARROW |DOWN ARROW |SELECT |PRINT |EXECUTE |PRINT SCREEN |INS |DEL |0 |1 |2 |3 |4 |5 |6 |7 |8 |9 |A |B |C |D |E |F |G |H |I |J |K |L |M |N |O |P |Q |R |S |T |U |V |W |X |Y |Z |Left Windows |Right Windows |Numeric pad 0 |Numeric pad 1 |Numeric pad 2 |Numeric pad 3 |Numeric pad 4 |Numeric pad 5 |Numeric pad 6 |Numeric pad 7 |Numeric pad 8 |Numeric pad 9 |Multiply |Add |Separator |Subtract |Decimal |Divide |F1 |F2 |F3 |F4 |F5 |F6 |F7 |F8 |F9 |F10 |F11 |F12 |F13 |F14|F15|F16|F17 |F18|F19|F20|F21|F22|F23|F24|NUM LOCK |SCROLL LOCK |Left SHIFT |Right SHIFT |Left CONTROL |Right CONTROL |Left MENU |Right MENU |;|=|,|-|.|/|`|["
Global $KeylistSplitted = StringSplit($KeyList, "|")
Global $KeyListSplitedMatchList = StringSplit("01|02|04|05|06|08|09|0C|0D|10|11|12|13|14|1B|20|21|22|23|24|25|26|27|28|29|2A|2B|2C|2D|2E|30|31|32|33|34|35|36|37|38|39|41|42|43|44|45|46|47|48|49|4A|4B|4C|4D|4E|4F|50|51|52|53|54|55|56|57|58|59|5A|5B|5C|60|61|62|63|64|65|66|67|68|69|6A|6B|6C|6D|6E|6F|70|71|72|73|74|75|76|77|78|79|7A|7B|7C|7D|7E|7F|80|81|82|83|84|85|86|87|90|91|A0|A1|A2|A3|A4|A5|BA|BB|BC|BD|BE|BF|C0|DB|DC|DD|", "|")


While Not FileExists($ini)
    CreateConf()
WEnd

$Key1Code = IniRead($ini, "MouseLocker", "KEY1", "A2")
$Key2Code = IniRead($ini, "MouseLocker", "KEY2", "11")
$DisplaTutorial = IniRead($ini, "MouseLocker", "Tutorial", "False")

$Key1String = $KeylistSplitted[ArrayFind($KeyListSplitedMatchList, $Key1Code)]
$Key2String = $KeylistSplitted[ArrayFind($KeyListSplitedMatchList, $Key2Code)]

Func ArrayFind($Array, $Val)
    For $i = 1 To $Array[0]
        If $Array[$i] = $Val Then Return $i
    Next
EndFunc   ;==>ArrayFind

Func CreateConf()

    MsgBox(64, "Configuration not found", "Configuration not found, click OK to create.")
    $hGui = GUICreate("Mouselocker configuration", 320, 180)
    $TutorialCheck = GUICtrlCreateCheckbox("Display tutorials", 5, 10)
    GUICtrlCreateLabel("Please select key combination.", 5, 35)
    GUICtrlCreateLabel("Key 1:", 5, 55)
    $Key1 = _GUICtrlComboBox_Create($hGui, $KeyList, 40, 55, 250)
    _GUICtrlComboBox_SetCurSel($Key1, 112)
    GUICtrlCreateLabel("Key 2:", 5, 85)
    $Key2 = _GUICtrlComboBox_Create($hGui, $KeyList, 40, 85, 250)
    _GUICtrlComboBox_SetCurSel($Key2, 11)
    $Save = GUICtrlCreateButton("Save", 10, 125, 75)
    $Cancel = GUICtrlCreateButton("Discard", 95, 125, 75)
    GUICtrlCreateLabel("If you want reconfigure this program later, delete mouselock.ini", 5, 155)
    GUISetState()

    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case -3
                Exit
            Case $Cancel
                Return
                GUIDelete()
            Case $Save
                $VK_KEY1 = $KeyListSplitedMatchList[_GUICtrlComboBox_GetCurSel($Key1) + 1]
                $VK_KEY2 = $KeyListSplitedMatchList[_GUICtrlComboBox_GetCurSel($Key2) + 1]
                $IsChecked = GUICtrlRead($TutorialCheck)
                IniWrite($ini, "MouseLocker", "KEY1", $VK_KEY1)
                IniWrite($ini, "MouseLocker", "KEY2", $VK_KEY2)
                If $IsChecked = 1 Then
                    IniWrite($ini, "MouseLocker", "Tutorial", "True")
                Else
                    IniWrite($ini, "MouseLocker", "Tutorial", "False")
                EndIf
                MsgBox(0, "Done", "Configuration file created in: " & @ScriptDir & "\mouselock.ini")
                GUIDelete()
                Return
        EndSwitch
    WEnd

EndFunc   ;==>CreateConf

Func DisplayTutorial($sTitle, $sText, $iW, $iH)
    If $DisplaTutorial = "True" Then
        $hGui = GUICreate($sTitle, $iW, $iH + 35)
        GUICtrlCreateLabel($sText, 10, 10, $iW - 20, $iH)
        $NoTutorial = GUICtrlCreateButton("Do not show tutorials anymore", 10, $iH + 5, 170)
        $Continue = GUICtrlCreateButton("Continue >>", 180, $iH + 5, 75)
        GUISetState()

        While 1
            $nMsg = GUIGetMsg()
            Switch $nMsg
                Case -3
                    Exit
                Case $Continue
                    GUIDelete()
                    Return
                Case $NoTutorial
                    GUIDelete()
                    IniWrite($ini, "MouseLocker", "Tutorial", "False")
                    $DisplaTutorial = "False"
                    Return
            EndSwitch
        WEnd
    EndIf
    Return
EndFunc   ;==>DisplayTutorial

While 1
    DisplayTutorial("Select window", "Select window in where you want to lock your mouse" & @CRLF & "After that press " & $Key1String & " + " & $Key2String & "to lock mouse in selected window", 450, 70)
    WinWaitSelected()
WEnd

Func WinWaitSelected()
    Sleep(250);Prevent bugs

    $dll = DllOpen("user32.dll")

    While 1
        Sleep(10)
        If _IsPressed($Key1Code, $dll) And _IsPressed($Key2Code, $dll) Then
            $Title = WinGetTitle("[Active]")
            DisplayTutorial("Selected: " & $Title, "To allow your mouse leave from selected window, press " & $Key1String & " + " & $Key2String, 450, 70)
            If MouseLock($Title) = False Then MsgBox(16, "Error", "Window lost")
            ExitLoop
        EndIf
    WEnd
    DllClose($dll)

EndFunc   ;==>WinWaitSelected

Func MouseLock($Title)
    $xywh = WinGetPos($Title)
    ConsoleWriteLine("Mouse Locked in window: " & $Title)
    $Lock = True
    $dll = DllOpen("user32.dll")
    If $DisplaTutorial = "true" Then
        SplashTextOn("Selected: " & $Title, "To allow your mouse leave from selected window, press " & $Key1String & " + " & $Key2String, 450, 70, 0, 0)
    EndIf
    Sleep(250);In order not to let _IsPressed capture keystoke used to lock mouse
    ;If you remove this sleep, mouse will unlocked instantly
    While $Lock = True
        If Not WinExists($Title) Then Return False
        $xy = MouseGetPos()
        If $xy[0] < $xywh[0] Then MouseMove($xywh[0], $xy[1], 0)
        If $xy[0] > $xywh[0] + $xywh[2] Then MouseMove($xywh[0] + $xywh[2], $xy[1], 0)
        If $xy[1] < $xywh[1] Then MouseMove($xy[0], $xywh[1], 0)
        If $xy[1] > $xywh[1] + $xywh[3] Then MouseMove($xy[0], $xywh[1] + $xywh[3], 0)
        If Not WinActive($Title) Then WinActivate($Title)
        If _IsPressed($Key1Code, $dll) And _IsPressed($Key2Code, $dll) Then
            ConsoleWriteLine("Deactivated (" & $Key1Code & ";" & $Key2Code & ")")
            If $DisplaTutorial = "true" Then
                SplashOff()
            EndIf
            DisplayTutorial("Unselected", "You can now freely move your mouse again", 450, 70)
            $Lock = False
            Return True
        EndIf
        Sleep(10)
    WEnd
    DllClose($dll)
EndFunc   ;==>MouseLock

Func ConsoleWriteLine($sData)
    ConsoleWrite($sData & @CRLF)
EndFunc   ;==>ConsoleWriteLine
Edited by E1M1

edited

Share this post


Link to post
Share on other sites
darkmaster071

Cool, works nice. Thanks. :mellow:

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  

×