Sprite and keyboard response.

hog

Well-known member
Joined
Mar 17, 2003
Messages
984
Location
UK
I notice that when moving my sprites the response to keypressing is somewhat slow.

If I increment the x axis by 1 the movement is too slow, if I increment by x * n the motion is quicker, but jerky.

When I used to program in C++ I wrote a keyboard interrupt handle which made the response to keypressing extremely fast resulting in smooth fast motion.

I am currently using DrawImageUnscaled to draw the sprite and DrawImage to erase it. I have not got into DirectX yet as that is many chapters away!

1. Is there a way to program in VB.NET a new keyboard interrrupt or is that just too low level for it?

2. Is there a better way to respond to keypress or draw the sprite using GDI+

3. Am I expecting too much. Like will my problem be solved once (if) I learn to program DirectX and should simply accept the current state of affairs as part of the road to learning greater things?

Thnx
 
The problem you are facing is the difference between .net and C++. I did a drawing test on this topic myself. You can download these programs and see for yourself.

Here are the links:

You can trust each program. I did nothing wierd or nasty.
You must unzip all three, put them in the same folder and then use the merge file located in the 3rd Chunk to put the file together again. There are directions in the download on how to use the drawing tests.

1st Chunk
2nd Chunk
3rd Chunk

If you have problems and are interested, let me know. I can e-mail the programs too.
 
Last edited by a moderator:
Dont know if this will be of any help, but I just happen to be playing with some keyboard stuff in one of my apps. I found this, and have been messing with it, so it isnt very clean at the moment. Put this in a keyboard module:

Code:
Imports System.Runtime.InteropServices
Imports System.Reflection
Imports System.Drawing
Imports System.Threading

Module Keyboard

    Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Integer) As Integer
    Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As KeyboardHookDelegate, ByVal hmod As Integer, ByVal dwThreadId As Integer) As Integer
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Integer) As Integer
    Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Integer, ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As KBDLLHOOKSTRUCT) As Integer

    Public Structure KBDLLHOOKSTRUCT
        Public vkCode As Integer
        Public scanCode As Integer
        Public flags As Integer
        Public time As Integer
        Public dwExtraInfo As Integer
    End Structure

     Low-Level Keyboard Constants
    Private Const HC_ACTION As Integer = 0
    Private Const LLKHF_EXTENDED As Integer = &H1
    Private Const LLKHF_INJECTED As Integer = &H10
    Private Const LLKHF_ALTDOWN As Integer = &H20
    Private Const LLKHF_UP As Integer = &H80

     Virtual Keys
    Public Const VK_TAB = &H9
    Public Const VK_CONTROL = &H11
    Public Const VK_ESCAPE = &H1B
    Public Const VK_DELETE = &H2E
    Public Const VK_A = &H41
    Public Const VK_B = &H42
    Public Const VK_C = &H43
    Public Const VK_D = &H44
    Public Const VK_E = &H45
    Public Const VK_F = &H46
    Public Const VK_G = &H47
    Public Const VK_H = &H48
    Public Const VK_I = &H49
    Public Const VK_J = &H4A
    Public Const VK_K = &H4B
    Public Const VK_L = &H4C
    Public Const VK_M = &H4D
    Public Const VK_N = &H4E
    Public Const VK_O = &H4F
    Public Const VK_P = &H50
    Public Const VK_Q = &H51
    Public Const VK_R = &H52
    Public Const VK_S = &H53
    Public Const VK_T = &H54
    Public Const VK_U = &H55
    Public Const VK_V = &H56
    Public Const VK_W = &H57
    Public Const VK_X = &H58
    Public Const VK_Y = &H59
    Public Const VK_Z = &H5A

    Private Const WH_KEYBOARD_LL As Integer = 13&
    Public KeyboardHandle As Integer


     Implement this function to catch keys
    Public Function IsHooked(ByRef Hookstruct As KBDLLHOOKSTRUCT) As Boolean

        Static Dim x As Integer = 0

        Debug.Write(vbCrLf)
        Debug.Write("vkcode: " & Hookstruct.vkCode & vbCrLf)
        Debug.Write("time: " & Hookstruct.time & vbCrLf)
        Debug.Write("dwextrainfo: " & Hookstruct.dwExtraInfo & vbCrLf)
        Debug.Write("flags: " & Hookstruct.flags & vbCrLf)
        Debug.Write("scancode: " & Hookstruct.scanCode & vbCrLf)

        CBool(GetAsyncKeyState(VK_CONTROL) And &H8000)


        If Hookstruct.vkCode = VK_S Then
            Debug.Write("Key - S" & vbCrLf)
        End If

        Return False
    End Function


    Public Function KeyboardCallback(ByVal Code As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer

        If (Code = HC_ACTION) Then

            If (IsHooked(lParam)) Then
                Return 1
            End If

        End If

        Return CallNextHookEx(KeyboardHandle, Code, wParam, lParam)

    End Function


    Public Delegate Function KeyboardHookDelegate(ByVal Code As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer
    <MarshalAs(UnmanagedType.FunctionPtr)> Private callback As KeyboardHookDelegate

    Public Sub HookKeyboard()
        callback = New KeyboardHookDelegate(AddressOf KeyboardCallback)
        KeyboardHandle = SetWindowsHookEx(WH_KEYBOARD_LL, callback, Marshal.GetHINSTANCE([Assembly].GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
    End Sub

    Public Function Hooked()
        Hooked = KeyboardHandle <> 0
    End Function

    Public Sub UnhookKeyboard()
        If (Hooked()) Then
            Call UnhookWindowsHookEx(KeyboardHandle)
        End If
    End Sub

End Module

"keyboard.hookkeyboard" to start (on form load?)
"keyboard.unhookkeyboard" to stop (on form close?)
 
Back
Top