Please support our sponsor:
DirectX4VB.Com - All You Need For Multimedia Visual Basic Programming

Main Site Links Resources Tutorials
News VB Gaming Code Downloads DirectX 7
Contact Webmaster VB Programming Product Reviews DirectX 8
  General Multimedia Articles DirectX 9
      Miscellaneous

 

DirectInput: Keyboard control
By: Jack Hoxley
Written: May 2000

Download: DI_Keyboard.Zip (12kb)


In my personal opinion; This feature of DirectInput is only really useful to C/C++ developers; as they normally have to write their own input code anyway. In VB we already have a certain degree of keyboard control; although using DirectX does offer some new features and some added control; It makes the simplest of things much more difficult. If you just wanted to check if the space bar had been pressed; you could use 5 lines of code in the "KeyPress", "KeyDown" or "KeyUp" procedures in a form object OR you could write out 50 lines for getting DirectInput to do the same thing.

I'm not dicouraging you from using DirectInput, I'm just making sure you're aware that you may not actually need to use it. Thankfully, it isn't too difficult to use if you want to though.

NB: If you use the Win32 DLL to create a window - as done in the Win32 Window Creation tutorial; you will have to use DirectInput to get user input.

There are two ways of using DirectInput Keyboard control; the first is on a loop basis, the second is by using DirectXEvent. First off, use the DirectXEvent always. Lots of people use the first method (including myself up to a certain point) - there are two major disadvantages to using it though:

  1. If you check the keys on EVERY loop of your game you're going to knock a fair amount of speed out of the final product. Okay, using this method mean that you can specify the keyboards repeat speed; but what if you're user rarely presses buttons? you could check the keys 10,000 times between each key press - an awful waste of time.
  2. Missing keystrokes. This can be a big problem on slower computers. If you set the keyboard checking speed to 10 a second (for example) it means that a person must hold down a key for an entire 10th of a second - may not sound long, but you get a fast typer or someone knocking lots of keys during a crucial stage of the game and it will really hit you. People wont be pleased with your game if they die because you have faulty key detection. Then there is a second problem; what if the game slows down to below 10 frames a second (when you're checking 10 times a second) - it can mean that you miss lots of keystrokes. I think I've made my point.

Using DirectXEvent means that DirectX tells you when the keyboard has been changed - this way you dont have to keep checking - it tells you, not you ask it. The DirectXEvent is raised every time a key is pressed - you then have to work out which one. If you use this structure; when there are lots of keys being pressed it works fine; then if no keys are being pressed no unnecessary code is being executed. An all round better option.

Checking the keys is also fairly easy; like VB, DirectInput provides us with a complete set of constants to replace the numbers. You can either use these constants (recommended) or you can refer to keys by number. I suggest you look through this list of keys; you dont need to remember them though. If you want to find out what they are without this page press F2 in VB to bring up the object browser; look through the DirectX library and you'll find a complete list of them - without descriptions though.

Note: Not all of them override their default uses. ie, pressing the windows key will still bring up the start menu. You cant stop it doing this (this way) but you can make it so that it does something else as well.

DIK_A
DIK_B
DIK_C
DIK_D
DIK_E
DIK_F
DIK_G
DIK_H
DIK_I
DIK_J
DIK_K
DIK_L
DIK_M
DIK_N
DIK_O
DIK_P
DIK_Q
DIK_R
DIK_S
DIK_T
DIK_U
DIK_V
DIK_W
DIK_X
DIK_Y
DIK_Z
DIK_1
On main keyboard
DIK_2 On main keyboard
DIK_3 On main keyboard
DIK_4 On main keyboard
DIK_5 On main keyboard
DIK_6 On main keyboard
DIK_7 On main keyboard
DIK_8 On main keyboard
DIK_9 On main keyboard
DIK_0 On main keyboard
DIK_F1
DIK_F2
DIK_F3
DIK_F4
DIK_F5
DIK_F6
DIK_F7
DIK_F8
DIK_F9
DIK_F10
DIK_F11
DIK_F12
DIK_F13
DIK_F14
DIK_F15
DIK_NUMPAD0
DIK_NUMPAD1
DIK_NUMPAD2
DIK_NUMPAD3
DIK_NUMPAD4
DIK_NUMPAD5
DIK_NUMPAD6
DIK_NUMPAD7
DIK_NUMPAD8
DIK_NUMPAD9
DIK_ESCAPE
DIK_MINUS
On main keyboard
DIK_EQUALS On main keyboard
DIK_BACK BackSpace
DIK_TAB
DIK_LBRACKET
[
DIK_RBRACKET ]
DIK_RETURN Enter on main keyboard
DIK_LCONTROL Left CTRL Key
DIK_SEMICOLON
DIK_APOSTROPHE
DIK_GRAVE
Grave accent (`)
DIK_LSHIFT Left SHIFT
DIK_BACKSLASH
DIK_COMMA
DIK_PERIOD
On main keyboard
DIK_SLASH Forward slash (/)on main keyboard
DIK_RSHIFT Right SHIFT
DIK_MULTIPLY Asterisk on numeric keypad
DIK_LMENU Left ALT
DIK_SPACE Spacebar
DIK_CAPITAL Caps Lock
DIK_NUMLOCK
DIK_SCROLL
Scroll Lock
DIK_SUBTRACT Hyphen (minus sign) on numeric keypad
DIK_ADD Plus sign on numeric keypad
DIK_DECIMAL Period (decimal point) on numeric keypad
DIK_NUMPADENTER
DIK_RCONTROL Right CTRL key
DIK_NUMPADCOMMA Comma on NEC PC98 numeric keypad
DIK_DIVIDE Forward slash (/)on numeric keypad
DIK_SYSRQ
DIK_RMENU Right ALT
DIK_HOME
DIK_UP
Up arrow
DIK_PRIOR Page Up
DIK_LEFT Left arrow
DIK_RIGHT Right arrow
DIK_END
DIK_DOWN
Down arrow
DIK_NEXT Page Down
DIK_INSERT
DIK_DELETE
DIK_LWIN
Left Windows key
DIK_RWIN Right Windows key
DIK_APPS Application key
DIK_PAUSE

You can download a working (if overcomplicated) example from the downloads page; or from the top of this page.

DirectX 4 VB © 2000 Jack Hoxley. All rights reserved.
Reproduction of this site and it's contents, in whole or in part, is prohibited,
except where explicitly stated otherwise.
Design by Mateo
Contact Webmaster
This site is hosted by Exhedra Solutions, Inc., the parent company of RentACoder.com and PlanetSourceCode.com