Direct Input 7 Keyboard Class | CodeGuru

Direct Input 7 Keyboard Class

Environment: Win9x, Windows NT/2000, DirectX Installed Introduction DirectInput is essential for responsive games, therefore to help ease the transition of DOS games programmers, you can use this class to read the keyboard direct trapping input before Windows starts performing it translation functions.  To use the class, define a global variable :- CDIKeyboard myKeyboard; You will […]

Written By
CodeGuru Staff
CodeGuru Staff
Aug 31, 2000
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Environment: Win9x, Windows NT/2000, DirectX Installed

Introduction

DirectInput is essential for responsive games, therefore to help ease the
transition of DOS games programmers, you can use this class to read the keyboard
direct trapping input before Windows starts performing it translation
functions. 

To use the class, define a global variable :-

CDIKeyboard myKeyboard;

You will have to supply the HWND for the parent window under which you wish
to take Keyboard Control.

The class has the following public methods:-

  • CDIKeyboard.SetHWND(HWND)
    

    Set the windows handle to which Direct Input Will be attached to.
    Must be the Parent Window.

  •  

  • void CDIKeyboard.SetHWND(CWnd*)
    

    Set the windows handle to which Direct Input Will be attached to.
    Must be the Parent Window. MFC Version.

    CDIKeyboard mkbrd;
    mkbrd.SetHWND(this);
    
  •  

  • bool CDIKeyboard.PollDevice(void)
    

    Obtain and update the keyboard state information.

  •  

  • bool CDIKeyboard.IsKeyPressed(unsigned char)
    

    returns true if a DIK_KEY is pressed, false if not. Example:

    mkbrd.IsKeyPressed(DIK_F1);
    
  •  

  • bool CDIKeyboard.Acquire(bool state)
    

    Acquire/Unacquire
    the Keyboard Device.  You will need to Unacquire the device if you
    want Windows to handle WM_CHAR, WM_KEYUP, WM_KEYDOWN messages.

    state=true To Acquire

    state=false To UnAcquire

    Returns : true=Operation Successful, false=Operation unsuccessful

Before obtaining keyboard state information you will need to set the HWND of
the parent window of your application.  Use PollDevice method before
checking the Keyboard state information and IsKeyPressed(key) method to check a
particular key has been pressed.

The following code snippet can be used to show which keys have been pressed

bool m_bInput;
myKeyboard.PollDevice();

m_bInput=myKeyboard.IsKeyPressed(DIK_F1)?true:false;

Downloads

Download base source project – 4 Kb

Download source – 2 Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.