Jump to content

Hook mouse


Alek
 Share

Recommended Posts

Hello, I'm trying to make a script that will "rotate" cursor movement direction by hooking the mouse (when moving the mouse up, it moves the cursor to the left etc.).

but I'm having some problems getting it to work properly.

it does sometimes move the cursor too its new position, but only for a split second before returning too the "real" correct position

example code.

#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <StructureConstants.au3>

Global $hHook, $hStub_KeyProc, $buffer = ""

HotKeySet("{esc}","_Exit")
_Main()

Func _Main()
    Local $hmod

    $hStub_KeyProc = DllCallbackRegister("_KeyProc", "long", "int;wparam;lparam")
    $hmod = _WinAPI_GetModuleHandle(0)
    $hHook = _WinAPI_SetWindowsHookEx($WH_MOUSE_LL, DllCallbackGetPtr($hStub_KeyProc), $hmod)

    While 1
        Sleep(10)
    WEnd
EndFunc 

Func _KeyProc($nCode, $wParam, $lParam)
    Local $tKEYHOOKS
    If $nCode < 0 Then
        Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
    EndIf
    
    $tKEYHOOKS = DllStructCreate("int X;int Y", $lParam)
    
    Switch $wParam
        Case 512 ;Mouse move.
            ConsoleWrite($lParam & " = "  & DllStructGetData($tKEYHOOKS, "X") & ", " & DllStructGetData($tKEYHOOKS, "Y") & @CRLF)
            DllStructSetData($tKEYHOOKS, "X", 100) ;Should set the new possition of the mouse to X = 100 :(
    EndSwitch
    
    Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
EndFunc   ;==>_KeyProc

Func OnAutoItExit()
    _WinAPI_UnhookWindowsHookEx($hHook)
    DllCallbackFree($hStub_KeyProc)
EndFunc   ;==>OnAutoItExit

Func _Exit()
    Exit
EndFunc
Edited by Alek

[font="Impact"]Never fear, I is here.[/font]

Link to comment
Share on other sites

Well, when your hook gets control, you get passed that $tKEYHOOKS ... but it's no use changing it, because it doesn't get passed to the next hook, it's for your own use only.

This command :

DllStructSetData($tKEYHOOKS, "X", 100) ;Should set the new possition of the mouse to X = 100

is totally useless, you're only changing your structure ...

I tried to put a MouseMove there but it seems to not work properly inside a mouse hook callback ?!

Edited by Inverted
Link to comment
Share on other sites

Well, when your hook gets control, you get passed that $tKEYHOOKS ... but it's no use changing it, because it doesn't get passed to the next hook, it's for your own use only.

This command :

DllStructSetData($tKEYHOOKS, "X", 100) ;Should set the new possition of the mouse to X = 100oÝ÷ Ú+-¢Ö¥+¬zW¬³*.ßÚÞ¢yrr§)àÊ«²ÚîrÛ«xkçm¢­hÊ.±ã(½ëaz·ºØ­±ç¦²Ú'¢Ü(®Jk¢«(§²'^jj.±èh¢GVÚrM÷þ«¨µëa¢è!´±ÈZ§SÚ­©¬²ÚîrÛ«{^ë"®¶­sbb33c·D´Uôôµ2ÒFÆÅ7G'V7D7&VFRgV÷C¶çB¶çBgV÷C²Âb33c¶Å&Ò

[font="Impact"]Never fear, I is here.[/font]

Link to comment
Share on other sites

You have to split up $wParam (and look at the lower 'word' of it for the event code).

Local $iEvent = BitAND($wParam, 0xFFFF)

See MrCreatoR's _MouseSetOnEvent() UDF - which is unfortunately missing the 'callnexthook' code, but otherwise does the job:

http://www.autoitscript.com/forum/index.ph...=64738&st=0

Oh, and you may want to use 'PostMessage' to post a WM_MOUSEMOVE event (or whichever code it is) to force a change in the other direction.

Edited by ascendant
Link to comment
Share on other sites

You have to split up $wParam (and look at the lower 'word' of it for the event code).

Local $iEvent = BitAND($wParam, 0xFFFF)

See MrCreatoR's _MouseSetOnEvent() UDF - which is unfortunately missing the 'callnexthook' code, but otherwise does the job:

http://www.autoitscript.com/forum/index.ph...=64738&st=0

Oh, and you may want to use 'PostMessage' to post a WM_MOUSEMOVE event (or whichever code it is) to force a change in the other direction.

$iEvent = BitAND($wParam, 0xFFFF)oÝ÷ ÙØ×hç!ܱê®j[hÓ~°=ªÚëÞ¯'Vî¶'buø§vÊ&zØbajy,~í«b|u«­¢+Ø%MÝ¥Ñ ÀÌØíÝAÉ´($%
ÍÔÄÈ($$%
½¹Í½±]ɥѡ±±MÑÉÕÑÑAÑÈ ÀÌØíÑ-e!==-L¤µÀìÅÕ½Ðì´ÁàÅÕ½ÐìµÀì!à ÀÌØí±AÉ´¤µÀìÅÕ½ÐìôÅÕ½ÐìµÀì±±MÑÉÕÑÑÑ ÀÌØíÑ-e!==-L°ÅÕ½Ðí`ÅÕ½Ð줵ÀìÅÕ½Ðì°ÅÕ½ÐìµÀì±±MÑÉÕÑÑÑ ÀÌØíÑ-e!==-L°ÅÕ½ÐídÅÕ½Ð줵Àì
I1¤($$%±±MÑÉÕÑMÑÑ ÀÌØíÑ-e!==-L°ÅÕ½Ðí`ÅÕ½Ðì°ÄÀÀ¤íM¡½Õ±ÍÐÑ¡¹ÜÁ½Íͥѥ½¸½Ñ¡µ½ÕÍѼ`ôÄÀÀè ($$%5½ÕÍ5½Ù¡±±MÑÉÕÑÑÑ ÀÌØíÑ-e!==-L°ÅÕ½Ðí`ÅÕ½Ð줰±±MÑÉÕÑÑÑ ÀÌØíÑ-e!==-L°ÅÕ½ÐídÅÕ½Ð줰À¤($$%IÑÕɸÄí=¹±ä¥¹ÑÉÕÁÑÌÑ¡µ½Õ͸(%¹MÝ¥Ñ

it kinda works, but its a bit choppy and it doesn't work well with > 1 monitor.

I would much rather change the "new" cursor coords in the hook and continue like normal, rather then interupting it and making autoit move the mouse. (this should make it move much smoother)

maybe i havent made my self clear enought, what I'm trying to do is something similar to a "joke" application called Sakasa-Mouse.

[font="Impact"]Never fear, I is here.[/font]

Link to comment
Share on other sites

HotKeySet("!{ESC}", "Terminate") 

Global $easing = 5
Global $sleep = 0010
While 1
    _main()
WEnd

Func Terminate()
    Exit 0
EndFunc   ;==>Terminate

Func _main()
    Sleep($sleep)
    Local $crazypos = MouseGetPos()
    Local $Nwposx, $Nwposy
    If not IsArray($crazypos) then return 
    Local $xPcalc = @DesktopWidth/2
    Local $yPcalc = @DesktopHeight/2
       If $crazypos[1] > $yPcalc Then                       
        $Nwposy =  $crazypos[1] - $easing
       Else
           $Nwposy =  $crazypos[1] + $easing
       EndIf
        If $crazypos[0] > $xPcalc Then 
            $Nwposx =  $crazypos[0] - $easing
        Else
            $Nwposx =  $crazypos[0] + $easing
        EndIf
    MouseMove($Nwposx,$Nwposy)
EndFunc

Take a look at the code above if it's a gag type of event you want. This code you can set the sleep and easing effects to whatever you want. No matter where they move the mouse it constantly eases back to the center and gives a slight bounce effect if you set the easing greater than 1.

You can check out my mouse location pointer code below to get an idea of how to check versus other objects as well.

My Projects: [topic="89413"]GoogleHack Search[/topic], [topic="67095"]Swiss File Knife GUI[/topic], [topic="69072"]Mouse Location Pointer[/topic], [topic="86040"]Standard Deviation Calculator[/topic]

Link to comment
Share on other sites

HotKeySet("!{ESC}", "Terminate") 

Global $easing = 5
Global $sleep = 0010
While 1
    _main()
WEnd

Func Terminate()
    Exit 0
EndFunc   ;==>Terminate

Func _main()
    Sleep($sleep)
    Local $crazypos = MouseGetPos()
    Local $Nwposx, $Nwposy
    If not IsArray($crazypos) then return 
    Local $xPcalc = @DesktopWidth/2
    Local $yPcalc = @DesktopHeight/2
       If $crazypos[1] > $yPcalc Then                       
        $Nwposy =  $crazypos[1] - $easing
       Else
           $Nwposy =  $crazypos[1] + $easing
       EndIf
        If $crazypos[0] > $xPcalc Then 
            $Nwposx =  $crazypos[0] - $easing
        Else
            $Nwposx =  $crazypos[0] + $easing
        EndIf
    MouseMove($Nwposx,$Nwposy)
EndFunc

Take a look at the code above if it's a gag type of event you want. This code you can set the sleep and easing effects to whatever you want. No matter where they move the mouse it constantly eases back to the center and gives a slight bounce effect if you set the easing greater than 1.

You can check out my mouse location pointer code below to get an idea of how to check versus other objects as well.

neat script lol
[u][font="Century Gothic"]~я α и d γ ĵ . ċ . ѕ қ ϊ и и ε я~- My Programs -auto shutdownSleep funcdisallow programs[/font][/u]
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...