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 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


Comments

  • 굿잡

    Posted by Legacy on 09/24/2003 12:00am

    Originally posted by: gogo

    ...

    Reply
  • go raibh a mile maith agat

    Posted by Legacy on 08/14/2002 12:00am

    Originally posted by: Elaine

    Thanks....I was going spare with this before hand

    Reply
  • appreciados

    Posted by Legacy on 10/06/2001 12:00am

    Originally posted by: d.martin

    thank you for sharing your time and wealth of knowledge.............

    • help!!!!!!!!!!!!

      Posted by bahar on 08/16/2004 03:58am

      I wanna to implement DirectInput rof keyboard in my project. I have read this article and downloaded the tow filws. Now I do not no what is the next step?! I added files to a Dialog Based project. but compiling it resulted in many errors. I do not know where I must use these files. Can u plz help me? thanx

      Reply
    • help!!!!!!!!!!!

      Posted by bahar on 08/16/2004 03:56am

      I wanna to implement DirectInput rof keyboard in my project. I have read this article and downloaded the tow filws. Now I do not no what is the next step?! I added files to a Dialog Based project. but compiling it resulted in many errors. I do not know where I must use these files. Can u plz help me? thanx

      Reply
    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 23, 2014 @ 12:00 p.m. ET / 9:00 a.m. PT Despite the current "virtualize everything" mentality, there are advantages to utilizing physical hardware for certain tasks. This is especially true for backups. In many cases, it is clearly in an organization's best interest to make use of physical, purpose-built backup appliances rather than relying on virtual backup software (VBA - Virtual Backup Appliances). Join us for this eSeminar to learn why physical appliances are preferable to …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds