Jump to content

Mouse movement detection(only left and right)


Recommended Posts

This looked easy but i can't do it.

I want script that will detect if mouse has been moved left or right, and hold a button while the mouse is moving. Like "while ispressed |do |send", but with mouse movement instead of ispressed. I searched the forum and closest i could find was MouseGetPos, but i can't see how can that be used for this.

Thanks for your time reading

Link to comment
Share on other sites

Use MouseGetPos(0) in a loop to get only the X coordinate, and monitor it in a loop (While/WEnd) for changes within a certain amount of time.

Try to code it and post what you've got if you need more help.

;)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

As far as i understand, MouseGetPos can only detect coordinates at one moment. So, to do what i want i would need to create loop in 1ms intervals and check for mouse position changes. The only idea i got is something like this:

#include <Misc.au3>
$dll = DllOpen("user32.dll")

      While _IsPressed("button", $dll)
        Do
            $pos1=mousegetpos(0)
            sleep(1)
            $pos2=mousegetpos(0)
               if $pos2>$pos1 then
                 Send ("{button1 down}")
               else 
                 Send ("{button2 down}")
               endif
        until Not _IsPressed("button", $dll)
      WEnd
DllClose($dll)

I know this does not work. I think "if $pos2>$pos1 then" is wrong.

Edited by TwistedMind
Link to comment
Share on other sites

  • Moderators

TwistedMind,

This is an easier way - although it involves some quite advanced concepts. :evil:

You need to register the message that Windows sends when the mouse is moved so that you can intercept it. Then whenever the mouse moves, you read the x-coordinate and check in your normal GUIGetMsg loop whether the value has changed. If the value has changed then you know the mouse is moving - you set a timer and if the mouse has not moved for a certain time you decide the mouse is no longer moving.

Look at this example:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

; Create Global variables
Global $iMouse_Pos
Global $iMouse_Curr_Pos = 0
Global $iBegin = 0

; Create GUI
$hGUI = GUICreate("Test", 500, 500)

; Create labels on GUI
$hLabel = GUICtrlCreateLabel("The Mouse X co-ordinate is: ", 10, 10, 150, 20)
$hCoord = GUICtrlCreateLabel("", 160, 10, 50, 20)
$hMoving = GUICtrlCreateLabel("", 10, 50, 200, 20)

GUISetState()

; Register the Windows message
GUIRegisterMsg($WM_MOUSEMOVE, "__MY_WM_MOUSEMOVE")

While 1

    ; Check for closure
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

    ; Check if mouse has moved in x-direction
    If $iMouse_Pos <> $iMouse_Curr_Pos Then
        ; Set new position
        $iMouse_Curr_Pos = $iMouse_Pos
        ; Write coordinate
        GUICtrlSetData($hCoord, $iMouse_Pos)
        ; Reset the timer to keep moving 
        $iBegin = TimerInit()
        ; Set Moving label
        GUICtrlSetData($hMoving, "Mouse is moving")
    EndIf

    ; Check if timer has run for more than 1/10 sec
    If TimerDiff($iBegin) > 100 Then
        ; Clear moving label
        If GUICtrlRead($hMoving) = "Mouse is moving" Then GUICtrlSetData($hMoving, "")
    EndIf

WEnd

Func __MY_WM_MOUSEMOVE($hWnd, $iMsg, $wParam, $lParam)
    Switch $iMsg
        Case $WM_MOUSEMOVE
            If IsHWnd($hGUI) Then $iMouse_Pos = MouseGetPos(0) ; Get only X coordinate
    EndSwitch
EndFunc   ;==>__MY_WM_MOUSEMOVE

I think it is clear enough, but if not - please ask. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

  • Moderators

TwistedMind,

It is not that difficult. What is too much for you? We are here to help and explain. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

As far as i understand, MouseGetPos can only detect coordinates at one moment. So, to do what i want i would need to create loop in 1ms intervals and check for mouse position changes.

I was thinking more like this:
HotKeySet("{ESC}", "_Quit")

Global $iLastX = MouseGetPos(0), $iCurrentX = 0

While 1
    $iCurrentX = MouseGetPos(0)
    If $iCurrentX = $iLastX Then
        ToolTip("Mouse not moving...")
    Else
        $iLastX = $iCurrentX
        ToolTip("Mouse is on the move!")
    EndIf
    Sleep(250)
WEnd

Func _Quit()
    Exit
EndFunc

Note that testing every 1ms is not required. At the speed a human hand normally moves a mouse, it can take much longer than that to change the X coordinate, even while the mouse IS in motion.

;)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

M23, it is like reading unknown language. I just can't understand most of the commands. Even with the comments. Things like "Check for closure" and "Clear moving label", i have no idea what that does and why is it there. I am so sory. I know you are ready to help but i am a total begginer and it is simply to advanced for me.

PsaltyDS, this one is a bit easier. But, it will only detect if the mouse has been moved. It won't detect the side. So i modified it to this:

#include <Misc.au3>
$dll = DllOpen("user32.dll")

      Global $iLastX = MouseGetPos(0), $iCurrentX = 0

While 1
    While _IsPressed("04", $dll)
        Do
            $iCurrentX = MouseGetPos(0)
            If $iCurrentX > $iLastX Then
                Send ("{button1}")
            ElseIf $iCurrentX < $iLastX Then
                Send ("{button2}")
            EndIf
            $iLastX = $iCurrentX
            Sleep(250)
        until Not _IsPressed("04", $dll)
    WEnd    
WEnd
DllClose($dll)

and it works perfectly. Except in game, where i need it ;) . I saw somewhere that while in game, mouse is fixed to the center. Is there anything i can do to make this work?

Murphy's law, the script from M23 would probably work without a problem. Just because i don't get it :evil:

So, I will go to sleep now, and start all over in the morning. Hopefuly i will be smarter.

Link to comment
Share on other sites

  • Moderators

Except in game

Same here..... ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

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