Jump to content

MouseTrap / Hook


Recommended Posts

Hi guys,

For a project i need to define the size of the working desktop ( the use of an external software ), i have try with _MouseTrap but not work if i switch the multi desktop.

So i have think to a Mouse hook:

#include <WinAPI.au3>

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

Local $XCenter = Int(@DesktopWidth / 2)
Local $YCenter = Int(@DesktopHeight / 2)

If $XCenter < $YCenter Then
    Local $Radius = Int($XCenter / 2)
Else
    Local $Radius = Int($YCenter / 2)
EndIf
MouseMove($XCenter, $YCenter)
$RadSq = $Radius * $Radius

Global Const $WM_MOUSEMOVE = 0x0200
Global Const $MSLLHOOKSTRUCT = $tagPOINT & ";dword mouseData;dword flags;dword time;ulong_ptr dwExtraInfo"

$hKey_Proc = DllCallbackRegister("_Mouse_Proc", "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)

While 1
    Sleep(100)
WEnd

Func _Mouse_Proc($nCode, $wParam, $lParam)
    Local $info, $ptx, $pty, $mouseData, $flags, $time, $dwExtraInfo, $Res
    Local $xevent = "Unknown", $xmouseData = ""

    If $nCode < 0 Then
        $ret = DllCall("user32.dll", "long", "CallNextHookEx", "hwnd", $hM_Hook[0], _
                "int", $nCode, "ptr", $wParam, "ptr", $lParam) ;recommended
        Return $ret[0]
    EndIf

    $info = DllStructCreate($MSLLHOOKSTRUCT, $lParam) 
    DllStructGetData($info, 1)$
        ptx = DllStructGetData($info, 1)
    $pty = DllStructGetData($info, 2)
    $Res = 0

    Select
        Case $wParam = $WM_MOUSEMOVE
            If ($ptx - $XCenter) ^ 2 + ($pty - $YCenter) ^ 2 > $RadSq Then
                $Res = 1
            EndIf
    EndSelect

    If $Res = 1 Then
        Return 1
    Else
        $ret = DllCall("user32.dll", "long", "CallNextHookEx", "hwnd", $hM_Hook[0], _
                "int", $nCode, "ptr", $wParam, "ptr", $lParam)
        Return $ret[0]
    EndIf
EndFunc   ;==>_Mouse_Proc

Func OnAutoItExit()
    DllCall("user32.dll", "int", "UnhookWindowsHookEx", "hwnd", $hM_Hook[0])
    $hM_Hook[0] = 0
    DllCallbackFree($hKey_Proc)
    $hKey_Proc = 0
    Exit
EndFunc   ;==>OnAutoItExit

I the example the area is a circle, how i can define an rectangle area like @desktopwidth - 200 and @desktopheight - 50?

Thanks

Edited by MyEarth
Link to comment
Share on other sites

This is probably not the best way to do it but here you go:

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

$x = @DesktopWidth / 2 - 100
$y = @DesktopHeight / 2 - 50
$width = 200
$height = 100
$outoflimit = False

While 1
    $pos = MouseGetPos()
    If $pos[ 0 ] > $x + $width Then
        $pos[ 0 ] = $x + $width
        $outoflimit = True
    ElseIf $pos[ 0 ] < $x Then
        $pos[ 0 ] = $x
        $outoflimit = True
    EndIf

    If $pos[ 1 ] > $y + $height Then
        $pos[ 1 ] = $y + $height
        $outoflimit = True
    ElseIf $pos[ 1 ] < $y Then
        $pos[ 1 ] = $y
        $outoflimit = True
    EndIf


    If $outoflimit Then
        MouseMove( $pos[ 0 ],$pos[ 1 ],0 )
        $outoflimit = False
    EndIf
WEnd

Func OnAutoItExit()
    Exit
EndFunc

Cause no one here can ever stop us, they can try but we won't let them.. No way

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