Giving mouse movement lag

C

c.k.

I was wondering if there was a way to give mouse movement on an empty form some lag/delay? For instance, user moves mouse, and the cursor is
20ms (possible adjustable from some option) behind the movement. So when user stops moving mouse, 20ms later the cursor itself stops
moving...
I tried using:
Windows.Forms.Cursor.Current.Position = New System.Drawing.Point(x,y)
and forms of sleep/timer events but cant seem to get it to work correctly....
Well anyway if anyone knows how to do it, some help would be appreciated.
 
S

SurturZ

I think making the mouse invisible and then displaying a fake mouse pointer
would be your best bet, since you need to know where the "future" mouse will
be.
 
C

c.k.

Thanks for the advice, changing the mouse speed isnt quite what i want, i'd like the speed to be same, just happen x milliseconds later.
Anyway, I wrote some code around the idea of just hiding the mouse pointer (altho in the below code it's commented out), but doesn't seem to
want to work correctly. I'll paste the code here..just open up a big form, and add a radio button with no text (acting as a temp 'fake'
mouse cursor):

Public Class Form1

Dim t As New Timer
Dim iX(0) As Integer
Dim iY(0) As Integer
Dim i1 As Integer = 0
Dim i2 As Integer = 0
Dim tmEvent1(0) As DateTime
Dim tmEventNow As DateTime
Dim tmSpan As TimeSpan
Dim tmEventMisc As DateTime

Private Sub Form1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Click
Me.Close()
End Sub

Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
t.Interval = 1
AddHandler t.Tick, AddressOf Me.MoveFakeMouse
t.Start()
End Sub

Private Sub Form1_MouseEnter(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.MouseEnter
'Cursor.Hide()
End Sub

Private Sub Form1_MouseLeave(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.MouseLeave
'Cursor.Show()
End Sub

Private Sub Form1_MouseMove(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
ReDim iX(i1)
ReDim iY(i1)
ReDim tmEvent1(i1)
iX(i1) = e.X
iY(i1) = e.Y
tmEvent1(i1) = Now
i1 += 1
End Sub

Private Sub MoveFakeMouse()
Me.Text = CStr(i1) & " " & CStr(i2) & " - X:" & CStr(iX(i2)) & _
" Y:" & CStr(iY(i2))
If i2 >= (tmEvent1.Count - 1) Then Exit Sub
tmEventNow = Now
tmEventMisc = tmEvent1(i2)
tmSpan = tmEventNow.Subtract(tmEventMisc)

If tmSpan.TotalMilliseconds > 20 Then
RadioButton1.Top = iY(i2)
RadioButton1.Left = iX(i2)
i2 += 1
End If
End Sub

End Class

The idea is, Form1_MouseMove adds the current mouse coords to an array (iX, iY), and also the current time to a secondary array (tmEvent).
And the timer, every milisecond, triggers MoveFakeMouse(). This sub then checks the time on the tmEvent array with current time and if its
20ms difference, moves the radiobutton using the coords from the associated iX & iY arrays. Then moves on to next array and just keeps doing
that...
But the radiobutton doesn't seem to move, or if it does, it will only move after the mouse has stopped moving and the MoveFakeMouse has
'caught up'. I have a me.text line in there to show it catching up and how the x/y coords dont show up til then.... Anyway, any ideas on why
it's not working, or if i'm completely doing it wrong, another way of doing it... Just looking for a way to delay mouse movement, or have a
fake cursor do exact same movements, just a few moments later....
 
J

James Hahn

Your REDIM is wiping out the values in the tables before they get processed.
use ReDim Preserve instead.
 
J

James Hahn

Thanks for letting us know the information was useful.

You are aware, I presume, that your real code will need to use a
System.Collections.Queue instead of an array (which will make the redim
unnecessary).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top