Hospital

Predicting mouse position

23 posts in this topic

#1 ·  Posted (edited)

How would you predict mouse position and direction? like let's say there would be a box that would try to go infront of your mouse in whatever direction it's moving.

would this be difficult? many interesting ideas stem with this in mind.

 

/edit

Hey guys, I did some research and I've found something that does just this in another language.

I have been looking over it for a little while now and I think I get it but I don't entirely enough to know how to transfer this to autoit.

Autoit has so much potential, it has to be possible to do this right?

http://jsfiddle.net/pfyruc14/5/

 

sorry to confuse

Edited by Hospital

Share this post


Link to post
Share on other sites



hey john, i'm glad you saw my thread. i'm sure you can help me.

i have been looking at it for the past hours now and i still don't know how to move something like that to autoit

Share this post


Link to post
Share on other sites

Please wait at least 24 hours before bumping a thread. 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-02-03 - Version 1.4.7.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-03 - Version 1.2.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Check the mouse position using MouseGetPos in a loop and move the box accordingly.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-02-03 - Version 1.4.7.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-03 - Version 1.2.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Check the mouse position using MouseGetPos in a loop and move the box accordingly.

 that is not what im trying to do, though that is necessary as part the challenge is like i said before predicting

thank you for your answer though water ; here is an example of what i mean brother --

https://jsfiddle.net/pfyruc14/34/

Edited by Hospital

Share this post


Link to post
Share on other sites

Hospital,

Does this help?

#include <WindowsConstants.au3>

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

Global $aOldMousePos[2] = [@DesktopWidth / 2, @DesktopHeight / 2], $aMousePos[2], $iSensitivity = 3

AdlibRegister("_Mouse_Pos")

$hGUI = GUICreate("", 5, 5, -1, -1, $WS_POPUP)
GUISetBkColor(0xFF0000)
GUISetState()

While 1
    Sleep(10)
WEnd



Func _Mouse_Pos()

    $aMousePos = MouseGetPos()

    $iXdiff = $aMousePos[0] - $aOldMousePos[0]
    $iYdiff = $aMousePos[1] - $aOldMousePos[1]

    WinMove($hGUI, "", $aMousePos[0] + ($iSensitivity * $iXdiff), $aMousePos[1] + ($iSensitivity * $iYdiff))

    $aOldMousePos = $aMousePos



EndFunc



Func _Exit()
    Exit
EndFunc

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Hospital,

Does this help?

#include <WindowsConstants.au3>

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

Global $aOldMousePos[2] = [@DesktopWidth / 2, @DesktopHeight / 2], $aMousePos[2], $iSensitivity = 3

AdlibRegister("_Mouse_Pos")

$hGUI = GUICreate("", 5, 5, -1, -1, $WS_POPUP)
GUISetBkColor(0xFF0000)
GUISetState()

While 1
    Sleep(10)
WEnd



Func _Mouse_Pos()

    $aMousePos = MouseGetPos()

    $iXdiff = $aMousePos[0] - $aOldMousePos[0]
    $iYdiff = $aMousePos[1] - $aOldMousePos[1]

    WinMove($hGUI, "", $aMousePos[0] + ($iSensitivity * $iXdiff), $aMousePos[1] + ($iSensitivity * $iYdiff))

    $aOldMousePos = $aMousePos



EndFunc



Func _Exit()
    Exit
EndFunc

M23

So cool!! 

I'm definitely gonna be looking at this and coming up with all kinds of ideas.

why does the red box act so jerky as compared to the example in js though? whats the difference?

when I was looking over the code, i saw that somehow it stays a certain distance from the mouse cursor or the point

 

thank  you guys

/edit i think the reason is because that js one must be a complex verison of the example? i wonder whats so different

Edited by Hospital

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

By the way thank you again so much, this is really interesting.

How do you just make this stuff like this lol

 

/edit this sensitivity thing is pretty interesting

Edited by Hospital

Share this post


Link to post
Share on other sites

Hospital,

I have no idea why it behaves "so jerky" compared to the js example as I did not look at the js code before writing my script - it is "all my own work". You could try reducing the Adlib time (it currently uses the default 250ms interval) which could well make the movement smoother.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

Share this post


Link to post
Share on other sites

Hospital,

I have no idea why it behaves "so jerky" compared to the js example as I did not look at the js code before writing my script - it is "all my own work". You could try reducing the Adlib time (it currently uses the default 250ms interval) which could well make the movement smoother.

M23

like the red dot , could it be a certain distance from the mouse? 

Share this post


Link to post
Share on other sites

Expanding on M23 example, here is a little more control for fine tuning...

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

Global $aOldMousePos[2] = [@DesktopWidth / 2, @DesktopHeight / 2], $aMousePos[2], $iSensitivity = 3
Global $iFrequency = 10, $iTolerance = 5

AdlibRegister("_Mouse_Pos", $iFrequency)

$hGUI = GUICreate("", 5, 5, -1, -1, $WS_POPUP)
GUISetBkColor(0xFF0000)
GUISetState()

While 1
    Sleep(10)
WEnd

Func _Mouse_Pos()
    $aMousePos = MouseGetPos()

    If _ToleranceExceeded($aMousePos, $aOldMousePos) Then

        $iXdiff = $aMousePos[0] - $aOldMousePos[0]
        $iYdiff = $aMousePos[1] - $aOldMousePos[1]

        WinMove($hGUI, "", $aMousePos[0] + ($iSensitivity * $iXdiff), $aMousePos[1] + ($iSensitivity * $iYdiff))

        $aOldMousePos = $aMousePos
    EndIf
EndFunc   ;==>_Mouse_Pos

Func _ToleranceExceeded(ByRef $m1, ByRef $m2)
    If Abs($m1[0] - $m2[0]) >= $iTolerance Or Abs($m1[1] - $m2[1]) >= $iTolerance Then
        Return True
    EndIf
    Return False
EndFunc   ;==>_ToleranceExceeded

Func _Exit()
    Exit
EndFunc   ;==>_Exit

 


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

Hospital,

And if you want the dot at a fixed distance from the mouse, you can do something like this:

#include <WindowsConstants.au3>

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

Global $aOldMousePos[2] = [@DesktopWidth / 2, @DesktopHeight / 2], $aMousePos[2], $iOffsetDist = 20

AdlibRegister("_Mouse_Pos", 50)

$hGUI = GUICreate("", 5, 5, -1, -1, $WS_POPUP)
GUISetBkColor(0xFF0000)
GUISetState()

While 1
    Sleep(10)
WEnd



Func _Mouse_Pos()

    $aMousePos = MouseGetPos()


    $iXdiff = $aMousePos[0] - $aOldMousePos[0]
    Select
        Case $iXdiff = 0
            $iXOffset = 0
        Case $iXdiff > 0
            $iXOffset = $iOffsetDist

        Case $iXdiff < 0
            $iXOffset = -$iOffsetDist

    EndSelect



    $iYdiff = $aMousePos[1] - $aOldMousePos[1]
    Select
        Case $iYdiff = 0
            $iYOffset = 0
        Case $iYdiff > 0
            $iYOffset = $iOffsetDist

        Case $iYdiff < 0
            $iYOffset = -$iOffsetDist

    EndSelect



    WinMove($hGUI, "", $aMousePos[0] + $iXOffset, $aMousePos[1] + $iYOffset)

    $aOldMousePos = $aMousePos



EndFunc



Func _Exit()
    Exit
EndFunc

I increased the frequency of the Adlib call and it is now much smoother.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

Share this post


Link to post
Share on other sites

Been trying to improve this a bit but it's doing my head in now.

In the below code, the diagonal movement is great, but the horizontal and vertical are lousy.

If you uncomment the block then the vertical and horizontal are great but the diagonal is lousy.

#include <WindowsConstants.au3>

HotKeySet("{ESC}", "_Exit")
Global $aOldMousePos[2] = [@DesktopWidth / 2, @DesktopHeight / 2], $aMousePos[2], $iOffsetDist = 100
Global $iFrequency = 10, $iTolerance = 15
AdlibRegister("_Mouse_Pos", $iFrequency)

$hGUI = GUICreate("", 5, 5, -1, -1, $WS_POPUP, $WS_EX_TOPMOST)
GUISetBkColor(0xFF0000)
GUISetState()

While 1
    Sleep(10)
WEnd



Func _Mouse_Pos()

    Local  $iXOffset
    Local  $iYOffset
    ;Local Static $aOldMousePos = MouseGetPos()
    Local  $iXdiff
    Local  $iYdiff
    $aMousePos = MouseGetPos()

    $xTolExceeded = _XToleranceExceeded($aMousePos, $aOldMousePos)
    $yTolExceeded = _YToleranceExceeded($aMousePos, $aOldMousePos)


    If $xTolExceeded Then
        $iXdiff = $aMousePos[0] - $aOldMousePos[0]

        Select
            Case $iXdiff = 0
                $iXOffset = 0
            Case $iXdiff > 0
                $iXOffset = $iOffsetDist

            Case $iXdiff < 0
                $iXOffset = -$iOffsetDist

        EndSelect
    EndIf


    If $yTolExceeded Then
        $iYdiff = $aMousePos[1] - $aOldMousePos[1]

        Select
            Case $iYdiff = 0
                $iYOffset = 0
            Case $iYdiff > 0
                $iYOffset = $iOffsetDist

            Case $iYdiff < 0
                $iYOffset = -$iOffsetDist

        EndSelect
    EndIf

    If $xTolExceeded And $yTolExceeded Then
        WinMove($hGUI, "", $aMousePos[0] + $iXOffset, $aMousePos[1] + $iYOffset)
        $aOldMousePos = $aMousePos
        Return
    EndIf
    #cs
    If $xTolExceeded Then
        WinMove($hGUI, "", $aOldMousePos[0] + $iXOffset, $aMousePos[1] + $iYOffset)
        $aOldMousePos = $aMousePos
        Return
    EndIf
    If $yTolExceeded Then
        WinMove($hGUI, "", $aMousePos[0] + $iXOffset, $aOldMousePos[1] + $iYOffset)
        $aOldMousePos = $aMousePos
        Return
    EndIf
    #ce
EndFunc   ;==>_Mouse_Pos

Func _XToleranceExceeded(ByRef $m1, ByRef $m2)
    If (Abs($m1[0] - $m2[0]) >= $iTolerance) Then
        Return True
    EndIf
    Return False
EndFunc   ;==>_XToleranceExceeded

Func _YToleranceExceeded(ByRef $m1, ByRef $m2)
    If (Abs($m1[1] - $m2[1]) >= $iTolerance) Then
        Return True
    EndIf
    Return False
EndFunc   ;==>_YToleranceExceeded

Func _ToleranceExceeded(ByRef $m1, ByRef $m2)
    If (Abs($m1[0] - $m2[0]) >= $iTolerance) Or (Abs($m1[1] - $m2[1]) >= $iTolerance) Then
        Return True
    EndIf
    Return False
EndFunc   ;==>_ToleranceExceeded

Func _Exit()
    Exit
EndFunc   ;==>_Exit

 


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

Here is an example using a line to indicate mouse direction.

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

Opt("MouseCoordMode", 1) ;1=absolute, 0=relative, 2=client
HotKeySet("{ESC}", "Terminate")
Global $hDC, $hPen, $o_Orig
$hDC = _WinAPI_GetWindowDC(0) ; DC of entire screen (desktop)
$hPen = _WinAPI_CreatePen($PS_SOLID, 2, 0xFF)
$o_Orig = _WinAPI_SelectObject($hDC, $hPen)

Local $exit = 1
While $exit
    Sleep(10)
    _MouseDirection()
WEnd

; clear resources
_WinAPI_SelectObject($hDC, $o_Orig)
_WinAPI_DeleteObject($hPen)
_WinAPI_ReleaseDC(0, $hDC)


Func _MouseDirection()
    Local Static $iOldX = 0, $iOldY = 0

    Local $Pi = 4 * ATan(1), $iLength, $aPos
    $aPos = MouseGetPos()
    While (Abs($iOldX - $aPos[0]) > 0) Or (Abs($iOldY - $aPos[1]) > 0)
        $aPos = MouseGetPos()
        $iGradient = ATan(($aPos[1] - $iOldY) / ($aPos[0] - $iOldX))

        $iLength = Sqrt(($aPos[1] - $iOldY) ^ 2 + ($aPos[0] - $iOldX) ^ 2); <--- Length varies with speed of mouse movement.
        ; Or
        ;$iLength = 40 ;                                                  <--- Direction line has fixed length.

        If $iLength < 40 Then $iLength = 40 ; <--- Direction line has minimum length.
        If (($aPos[0] - $iOldX) < 0) And (Abs($aPos[1] - $iOldY) < 2) Then ; X neg, Y = 0
            _WinAPI_DrawLine($hDC, $aPos[0], $aPos[1], $aPos[0] - $iLength, $aPos[1])
        ElseIf (Abs($aPos[0] - $iOldX) < 2) And (($aPos[1] - $iOldY) < 0) Then ; X= 0, Y neg
            _WinAPI_DrawLine($hDC, $aPos[0], $aPos[1], $aPos[0], $aPos[1] - $iLength)
        ElseIf (Abs($aPos[0] - $iOldX) < 2) And (($aPos[1] - $iOldY) > 0) Then ; X= 0, Y pos
            _WinAPI_DrawLine($hDC, $aPos[0], $aPos[1], $aPos[0], $aPos[1] + $iLength)
        ElseIf (($aPos[0] - $iOldX) < 0) And (($aPos[1] - $iOldY) > 0) Then ; X neg, Y pos
            _WinAPI_DrawLine($hDC, $aPos[0], $aPos[1], $aPos[0] - Cos($iGradient) * $iLength, $aPos[1] - Sin($iGradient) * $iLength)
        ElseIf (($aPos[0] - $iOldX) < 0) And (($aPos[1] - $iOldY) < 0) Then ; X neg, Y neg
            _WinAPI_DrawLine($hDC, $aPos[0], $aPos[1], $aPos[0] - Cos($iGradient) * $iLength, $aPos[1] - Sin($iGradient) * $iLength)
        Else
            _WinAPI_DrawLine($hDC, $aPos[0], $aPos[1], $aPos[0] + Cos($iGradient) * $iLength, $aPos[1] + Sin($iGradient) * $iLength)
        EndIf
        If $iOldX <> $aPos[0] Or $iOldY <> $aPos[1] Then
            $iOldX = $aPos[0]
            $iOldY = $aPos[1]
        EndIf
        Sleep(50)
        _WinAPI_RedrawWindow("", 0, 0, $RDW_INVALIDATE + $RDW_ALLCHILDREN)
    WEnd
    Return
EndFunc   ;==>_MouseDirection

Func Terminate()
    $exit = 0
EndFunc   ;==>Terminate

 

Edited by Malkey
Removed "(($aPos[0] - $iOldX) > 0) And (($aPos[1] - $iOldY) > 0)"

Share this post


Link to post
Share on other sites

Malkey,

I am afraid that does not work for me. I have to correct the Else case for what looks like an extraneous line and then I get a single red line which vanishes as soon as I move the mouse.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

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