Free Web Hosting Provider - Web Hosting - E-commerce - High Speed Internet - Free Web Page
Search the Web

vbProgramming | Tutorials | Smart Keyboard Movement
    vbProgramming 
Tutorials | Miscellaneous | Smart Keyboard Movement
 
     

vbProgramming Home :: vbProgramming Forums :: Tutorials :: Contact :: Links 

 

 

It's time to finally get rid of the response time when you press a key

What's the point of this tutorial?
The point is to eliminate the response time when you press a key.
For example, open up notepad and hold down K. You notice how there's a slight delay after the first K?
K <Pause> KKKKKKKKKKKKK
That's what we're trying to eliminate here.

Small codebase
I'll start you off with some code.

Dim zx As Integer
Dim zy As Integer


form1_paint:
e.Graphics.FillRectangle(Brushes.Red, zx, zy, 30, 30)

form1_keydown:
Select Case e.KeyCode
 Case Keys.Up
     zy -= 5
 Case  Keys.Down
    zy += 5
 Case  Keys.Left
    zx -= 5
 Case Keys.Right
   zx +=5 
End Select
Me.Invalidate

form1_load:
Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.DoubleBuffer Or ControlStyles.UserMouse, True)
I used the Or's on purpose. Take a look at the Bit String Flicking tutorial. It's the same thing as typing in 3 separate lines. And if you remember, these 3 flags reduce the flicker.

Move around, the usual, blah. The dreaded delay!

First method - Using a loop
This tutorial will be updated as I figure out more and more methods of fixing this. One method, which elnerdo (a vbProgrammer) kindly showed me was using timers. I said no :). Timers are a HUGE memory hog.

Now let's get this thing working. First of all, you're going to need to declare 4 booleans.

Dim hDown As Boolean
Dim hUp As Boolean
Dim hLeft As Boolean
Dim hRight As Boolean
Dim FormClosed As Boolean

I guess the h's stand for holding (I couldn't use Left because it interfered with the form1.Left property).

Replace form1_keydown code with:

        Select Case e.KeyCode
            Case Keys.Down
                hDown = False
            Case Keys.Up
                hUp = False
            Case Keys.Left
                hLeft = False
            Case Keys.Right
                hRight = False
        End Select

Add this to form1_keyUp:
        Select Case e.KeyCode
            Case Keys.Down
                down = False
            Case Keys.Up
                up = False
            Case Keys.Left
                hLeft = False
            Case Keys.Right
                hRight = False
        End Select

And now, add this sub:
    Public Sub LoopMovement()
        Do While Not FormClosed
            Application.DoEvents()
            If hup Then
                zy -= 1
            End If
            If hdown Then
               zy += 1
            End If
            If hLeft Then
               zx -= 1
            End If
            If hRight Then
               zx += 1
            End If
            Me.Invalidate()
        Loop
    End Sub

Form1_Closed:
FormClosed = True

Form1_load (add this to the end):
LoopMovement()

Run it, slight problem (at least for me), the form isn't shown, so before LoopMovement(), add Me.Show and Me.Focus. (The reason we added Me.Focus was because 
the form (for me at least) was out of focus and inactive. Run it, it should work.
However, the movement might be a bit fast, so just increment/decrement the zx and zy by .1 or .01 (however fast your computer is). Loops tend to go pretty fast. 
But don't worry about this problem, on the next tutorial (Counting and Limiting the FPS), we'll limit the number of times it loops per second (or frames per second), so 
that on each computer, it runs the same speed. As a matter of fact, in the FPS tutorial we'll use this source code as the base, so it might help to have it ready.

Bonus: In this tutorial, you can now move the character in diagonals - press up and right at the same time.

If anyone wants to submit more methods of doing this, just run it by me on vbProgramming forums (you can post without registering now). I'd rather not use timers.

Sorry if this tutorial seemed .... rushed. Because I did rush through it (and it took only 10 minutes to write this). If anyone feels that this tutorial requires more
explanation, just Contact me, or post on vbProgramming forums and I'll gladly add some explanation to this tutorial. 

The next tutorial in the FPS (Frames Per Second) tutorial, followed by an Introduction to APIs tutorial.

There is no source code for this tutorial.