DirectX:DirectInput:Tutorials:VBNET:DX9:Keyboard Handling

From GPWiki

Files:GUITutorial_warn.gif The Game Programming Wiki has moved! Files:GUITutorial_warn.gif

The wiki is now hosted by GameDev.NET at wiki.gamedev.net. All gpwiki.org content has been moved to the new server.

However, the GPWiki forums are still active! Come say hello.

Minimal code you can use to demonstrate how to get keyboard input using managed DX9 ...

Download this source.

Imports Microsoft.DirectX
Imports Microsoft.DirectX.DirectInput.CooperativeLevelFlags
Imports Microsoft.DirectX.DirectInput
 
Public Class Form1
    Inherits System.Windows.Forms.Form
 
    Dim KB As New DirectInput.Device(DirectInput.SystemGuid.Keyboard)
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        KB.SetCooperativeLevel(Me, Background Or NonExclusive)
        Me.Show()
        KB.Acquire()
    End Sub
 
    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        KB.Dispose()
        KB = Nothing
    End Sub
 
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim State As DirectInput.KeyboardState
        State = KB.GetCurrentKeyboardState
 
        If State.Item(Key.Escape) Then Me.Close()
        If State.Item(Key.A) Then Label1.Text &= "a"
        If State.Item(Key.B) Then Label1.Text &= "b"
        If State.Item(Key.C) Then Label1.Text &= "c"
 
    End Sub
End Class