Jump to content

Dragging a mouse exact distance back and forth


kewlak
 Share

Go to solution Solved by AutoXenon,

Recommended Posts

Hi. A total newbie here. I need to somehow program some software to make my mouse hold left click (in some not specific place on screen chosen by me, which will be changed each time), then move some distance in x axis to the left, and then the same distance back to the right, then release LMB in the exact same place it started. I need 2 commands like these. One i described, the second the same, but starting to the right instead of left, then back to the left. I want to decide myself when to use these operations, so maybe some keyboard shortcuts?

Can AutoIt do it for me? Please help.

Link to comment
Share on other sites

I am not sure as the nature of what you are trying to do - my response assumes you read the forum rules.  That said, look at MouseMove with MouseDown and MouseUp. I would also encourage you to code something and post the code as that usually tends to attract the most help.  

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Link to comment
Share on other sites

Thanks for the answer. I've read the rules. I will try to explain more deeply why I need it for.

I work with animation program. There is some horizontal slider i've made which controls phases of the animation of a character (the smoother move of a slider, the smoother animation of the character). Unfortunately this controller is a bit bugged, so only the beginning and the end of a slider's move is registered by the character. The frames between are empty. However move of the slider is fully animated - from the beginning till the end. There is some trick to fix it to make character react on slider. You just need to slightly move the slider (then character begins to register its move), but then the slider isn't exactly where it's supposed to be, because i moved it and i broke the smoothness of the animation. I need to get back with mouse exactly where i started. Furthermore i need to do repeat this move for every empty frame (it can be 20 times). Unfortunately from some reason only the move of a mouse can "awake" the character. Well i can also use the arrow keys (click on it slider, then press left, right arrows - or right, left - doesn't really matter), but it not always works and for 90% of attempts it causes program to crash.

The slider isn't exactly in one place of the screen. Its location is more stuck to the canvas, where character is. Canvas can be zoomed in, out etc, so the slider as well. That's why i need AutoIt or something similar to let me choose the start point (and end point - which is the same as start) of the whole operations.

I have no experiance in coding. What do you mean MouseDown and MouseUp? I need mouse to move left and right. If you necessarily want to watch me stumbling in the dark I will provide some more specific question soon. After basing maybe on some YT tutorial.

Link to comment
Share on other sites

#include <WinAPISys.au3>
Global Const $MOVE_DIST = 127    ; How much to move
Global Const $DELAY = 100        ; How long to wait before moving back and releasing

GUICreate("script")
GUISetState()
HotKeySet("+{LEFT}", LeftCmd)    ; Activate with Shift + Left
HotKeySet("+{RIGHT}", RightCmd)  ; Activate with Shift + Right

Do
Until GUIGetMsg()=-3

Func LeftCmd()
     _WinAPI_Mouse_Event($MOUSEEVENTF_LEFTDOWN)
     _WinAPI_Mouse_Event($MOUSEEVENTF_MOVE, -$MOVE_DIST)
     Wait($DELAY)
     _WinAPI_Mouse_Event($MOUSEEVENTF_MOVE, $MOVE_DIST)
     _WinAPI_Mouse_Event($MOUSEEVENTF_LEFTUP)
EndFunc
Func RightCmd()
     _WinAPI_Mouse_Event($MOUSEEVENTF_LEFTDOWN)
     _WinAPI_Mouse_Event($MOUSEEVENTF_MOVE, $MOVE_DIST)
     Wait($DELAY)
     _WinAPI_Mouse_Event($MOUSEEVENTF_MOVE, -$MOVE_DIST)
     _WinAPI_Mouse_Event($MOUSEEVENTF_LEFTUP)
EndFunc
Func Wait($waitTime)
     Local $time = TimerInit()
     While TimerDiff($time) < $waitTime
     WEnd
EndFunc

 

Edited by AutoXenon
Link to comment
Share on other sites

Thank you AutoXenon. It kinda works, but...

Ok, first of all i needed to change hotkeys for some not used by the animation software itself - solved. Then i needed to run SciTE as an administrator to make it even work on top of the software - solved. When i finally used it with the software it didn't work. I think script works too fast, but it kinda works when a hotkey is being held (maybe some code lines should be repeated?) However see for yourself:

 

Link to comment
Share on other sites

Try this instead :

Global Const $MOVE_DIST = 127    ; How much to move
Global Const $DELAY = 100        ; How long to wait before moving back and releasing

GUICreate("script")
GUISetState()
HotKeySet("+{LEFT}", LeftCmd)    ; Activate with Shift + Left
HotKeySet("+{RIGHT}", RightCmd)  ; Activate with Shift + Right

Do
Until GUIGetMsg()=-3

Func LeftCmd()
  Local $aPos = MouseGetPos()
  MouseDown("left")
  MouseMove($aPos[0] - $MOVE_DIST, $aPos[1], 50)  ; ajust 50 to the right speed (between 0 and 100)
  MouseUp("left")
EndFunc

Func RightCmd()
  Local $aPos = MouseGetPos()
  MouseDown("left")
  MouseMove($aPos[0] + $MOVE_DIST, $aPos[1], 50)  ; ajust 50 to the right speed
  MouseUp("left")
EndFunc

 

Link to comment
Share on other sites

  • Solution
#include <WinAPISys.au3>
Global Const $TOTAL_DIST = 125    ; How much to move
Global Const $TOTAL_TIME = 100    ; How long to last

GUICreate("script")
GUISetState()
HotKeySet("+{LEFT}", LeftCmd)    ; Activate with Shift + Left
HotKeySet("+{RIGHT}", RightCmd)  ; Activate with Shift + Right

Do
Until GUIGetMsg()=-3

Func LeftCmd()
     Local $delta = $TOTAL_DIST/($TOTAL_TIME/2/10)
     _WinAPI_Mouse_Event($MOUSEEVENTF_LEFTDOWN)
     For $i=0 to $TOTAL_TIME/2 Step 10
         _WinAPI_Mouse_Event($MOUSEEVENTF_MOVE, -$delta)
         Sleep(10)
     Next
     For $i=$TOTAL_TIME/2 to $TOTAL_TIME Step 10
         _WinAPI_Mouse_Event($MOUSEEVENTF_MOVE, $delta)
         Sleep(10)
     Next
     _WinAPI_Mouse_Event($MOUSEEVENTF_LEFTUP)
EndFunc
Func RightCmd()
     Local $delta = $TOTAL_DIST/($TOTAL_TIME/2/10)
     _WinAPI_Mouse_Event($MOUSEEVENTF_LEFTDOWN)
     For $i=0 to $TOTAL_TIME/2 Step 10
         _WinAPI_Mouse_Event($MOUSEEVENTF_MOVE, $delta)
         Sleep(10)
     Next
     For $i=$TOTAL_TIME/2 to $TOTAL_TIME Step 10
         _WinAPI_Mouse_Event($MOUSEEVENTF_MOVE, -$delta)
         Sleep(10)
     Next
     _WinAPI_Mouse_Event($MOUSEEVENTF_LEFTUP)
EndFunc

 

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