Sign in to follow this  
Followers 0
MDCT

Get Mouse positions problem on low-level mouse hook ($WH_MOUSE_LL)

3 posts in this topic

Hello again,

I have a problem with this only when my mouse pointer is on the edges of the screen.

When the cursor is on left edge of the screen, the Xpos sometimes returns negative (-1, -2).

When on the top edge the Ypos sometimes returns negative (-1, -2)

When on right edge, Xpos sometimes it returns bigger than my screen width (mine is 1024, sometimes it returns 1025). And when on bottom edge, sometimes it return value higher than my screen height.

I believe I have tried all the examples on the forum but, they all have the same problem. Does this problem happen only on my computer?

Below is the example script.

HotKeySet ( "{Esc}" , "OnAutoItExit")
Global Const $WH_MOUSE_LL=14
Global Const $tagPOINT="int X;int Y"
Global $MposX
Global $MposY
$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(128)
   ConsoleWrite($MposX&" "&$MposY&@CRLF)
WEnd

Func _Mouse_Proc($nCode, $wParam, $lParam)
    Local $info
    If $nCode < 0 Then
        $ret = DllCall("user32.dll", "long", "CallNextHookEx", "hwnd", $hM_Hook[0], _
                "int", $nCode, "ptr", $wParam, "ptr", $lParam)
        Return $ret[0]
    EndIf
    $info = DllStructCreate($tagPOINT, $lParam)
    $MposX = DllStructGetData($info, 1)
    $MposY = DllStructGetData($info, 2)

    $ret = DllCall("user32.dll", "long", "CallNextHookEx", "hwnd", $hM_Hook[0], _
            "int", $nCode, "ptr", $wParam, "ptr", $lParam)
    Return $ret[0]
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

Can someone help me fix this problem?

Thank you very much.

Share this post


Link to post
Share on other sites



This happens because the actual virtual desktop is much larger than the visible desktop. Here's a work-around using buffered positions.

HotKeySet("{Esc}", "OnAutoItExit")
Global Const $WH_MOUSE_LL = 14
Global Const $tagPOINT = "int X;int Y"
Global $MposX
Global $MposY
$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)
Global $MposX_Buffer = MouseGetPos(0), $MposY_Buffer = MouseGetPos(1)
While 1
 Sleep(250)
 ConsoleWrite($MposX_Buffer & " " & $MposY_Buffer & @CRLF)
WEnd
Func _Mouse_Proc($nCode, $wParam, $lParam)
 Local $info
 If $nCode < 0 Then
  $ret = DllCall("user32.dll", "long", "CallNextHookEx", "hwnd", $hM_Hook[0], _
    "int", $nCode, "ptr", $wParam, "ptr", $lParam)
  Return $ret[0]
 EndIf
 $info = DllStructCreate($tagPOINT, $lParam)
 Local $MposX = DllStructGetData($info, 1)
 Local $MposY = DllStructGetData($info, 2)
 If $MposX > @DesktopWidth Then $MposX = @DesktopWidth
 If $MposX < 0 Then $MposX = 0
 If $MposY > @DesktopHeight Then $MposY = @DesktopHeight
 If $MposY < 0 Then $MposY = 0
 If $MposX <> $MposX_Buffer Or $MposY <> $MposY_Buffer Then
  $MposX_Buffer = $MposX
  $MposY_Buffer = $MposY
  ; ConsoleWrite($MposX_Buffer & "x" & $MposY_Buffer & @CRLF)
 EndIf
 $ret = DllCall("user32.dll", "long", "CallNextHookEx", "hwnd", $hM_Hook[0], _
   "int", $nCode, "ptr", $wParam, "ptr", $lParam)
 Return $ret[0]
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

Share this post


Link to post
Share on other sites

Owww, bro KaFu. Thanks for the help. It works, but is there any workout without expressions (Ifs) like this?

Really appreciate it, but it's not what I was looking for.

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