Introduction
Written by JustinDaubenmire
In order to begin to work with DirectInput and the keyboard, you have to setup your Visual Basic 6 project to use the master library DirectX. To do this, follow these steps:
Open up Visual Basic 6, or Visual Basic 5, and select standard exe for the type of project.
Press alt+p for the project pull down menu then arrow to references and press enter on it.
Down arrow, or press the letter d repeatedly, until you find and entry "DirectX 8 for visual basic type library"
Press your space bar to check the box then tab to the ok button and press enter.
If you cannot locate "DirectX 8 for visual basic type library" in the list, make certain you have the DirectX 8 SDK or higher installed on your computer. To pick up a copy of the latest DirectX SDK, go to
The Direct``X home page and locate the latest DirectX SDK.
Note: DirectX 9 or higher contains both DirectX 7 and DirectX 8 for Visual Basic type libraries. If you have DirectX 8 or higher installed on your computer, but cannot locate "DirectX 8 for visual basic type library" in the list of references, in the project references screen, tab to the browse button and press your space bar. Manually browse for the file named dx8VB.dll. On windows 98/ME computers, you can find it in the system folder. On computers running windows 2000 or any version of xp, you can find it in your system32 folder. Press enter on the file then tab to the ok button on the project references screen and press enter to set the reference and return to your vb 6 project.
Source Code
Open up the code editor for form1 in your project, copy and paste all the code below overtop of any text in the code editor for form1. Once you do that, press f5 to run the project and test the keystrokes.
If you are using a screen reader, it will interfere with DirectInput and the keyboard. For example, if you press tab to see the message box "tab key pressed" it won't work. For JFW users, you have to press insert+3 to pass a key through and then press tab and it will work. This is just something to be aware of when testing DirectInput and the keyboard while using your brand of screen reader.
*** Begin Source Code ***
Option Explicit
' create a single DirectX8 object as overall manager
Private dx As DirectX8
' create a single DirectInput8 object as overall manager
Private di As DirectInput8
' create keyboard device
Private diDev As DirectInputDevice8
Private key_state As DIKEYBOARDSTATE
Private lHandle As Long ' used with direct input
' pull in this event for callback handeling is important for DirectInput
Implements DirectXEvent8
Private Sub DirectXEvent8_DXCallback(ByVal eventid As Long)
' this is where we trap keyboard presses
' or game pad buttons etc
' if it is our program calling the window
' using our handle then act
If eventid = lHandle Then
' get the state of the keyboard
diDev.GetDeviceStateKeyboard key_state
' we hitting tab key?
If key_state.Key(DIK_TAB) Then
MsgBox "tab key hit"
Exit Sub
End If
' we hitting left control key?
If key_state.Key(DIK_LCONTROL) Then
MsgBox "left control key hit"
Exit Sub
End If
' we hitting left shift key?
If key_state.Key(DIK_LSHIFT) Then
MsgBox "left shift pressed"
Exit Sub
End If
End If ' from first if test
End Sub
Private Sub Form_Load()
On Error Resume Next
Me.Show
Set dx = New DirectX8
' initialize direct input
Set di = dx.DirectInputCreate()
' create keyboard device
Set diDev = di.CreateDevice("GUID_SysKeyboard")
' now setup keyboard parameters
' set keyboard device data format
Call diDev.SetCommonDataFormat(DIFORMAT_KEYBOARD)
' to gain access to the keyboard
' now we must set the device's behavior
diDev.SetCooperativeLevel Me.hWnd, _
DISCL_EXCLUSIVE Or DISCL_BACKGROUND
' set up the call back event
lHandle = dx.CreateEvent(Me)
diDev.SetEventNotification lHandle
' now gain access to the keyboard
' The application must acquire the keyboard device before retrieving data
' From it
Call diDev.Acquire
Exit Sub
eh:
MsgBox Err.Number & " " & Err.Description
End Sub
Private Sub Form_Unload(Cancel As Integer)
' clean up the Di and DX objects
Set di = Nothing
Set dx = Nothing
End Sub