Direct Input 7 Mouse Class

Environment: Windows 9x, Windows 2000; Visual C++ 6 (SP4), DirectX 7 Installed


DirectInput is essential for responsive games, therefore to help ease the transition of DOS games programmers, you can use this class to read mouse input directly.  

To use the class, define a global variable :-

CDIMouse myMouse;

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:


void CDIMouse.SetHWND(HWND) Set the windows handle to which Direct Input Will be attached to.  Must be the Parent Window.
void CDIMouse.SetHWND(CWnd*) Set the windows handle for MFC applications to which Direct Input Will be attached to.  Must be the Parent Window.
int CDIMouse.GetAxisCount(void) Returns the number of Axis the mouse has.
int CDIMouse.GetButtonCount(void) Returns the number of Buttons the mouse has.
bool CDIMouse.PollDevice(void) Update Mouse Data, Must be called Every Time Program Needs Input
bool SetCoOpLevel(bool) Sets the Co-Operative Level (true=exclusive, false = Non-Exclusive)
int CDIMouse.GetRelativeX(void) Returns the movement on the X-Axis since data was last read. 
int CDIMouse.GetRelativeY(void) Returns the movement on the Y-Axis since data was last read. 
int CDIMouse.GetRelativeZ(void) Returns the movement on the Z-Axis since data was last read. 
bool CDIMouse.IsMouseLeft(void) True if mouse moving Left
bool CDIMouse.IsMouseRight(void) True if mouse moving Right
bool CDIMouse.IsMouseUp(void) True if mouse moving Up
bool CDIMouse.IsMouseDown(void) True if mouse moving Down
bool CDIMouse.IsMouseWheelUp(void) True if mouse wheel moving away from user.
bool CDIMouse.IsMouseWheelDown(void) True if mouse wheel moving Down from user.
bool CDIMouse.IsButton0(void) True if mouse button 0 pressed.
bool CDIMouse.IsButton1(void) True if mouse button 1 pressed.
bool CDIMouse.IsButton2(void) True if mouse button 2 pressed.
bool CDIMouse.IsButton3(void) True if mouse button 3 pressed.
bool CDIMouse.IsButton4(void) True if mouse button 4 pressed.
bool CDIMouse.IsButton5(void) True if mouse button 5 pressed.
bool CDIMouse.IsButton6(void) True if mouse button 6 pressed.
bool CDIMouse.IsButton7(void) True if mouse button 7 pressed.
bool CDIMouse.IsButtonDown(int button) True if button is pressed.
bool CDIMouse.IsDoubleClicked(int button) True if button double clicked.
void ResetDoubleClick(void)  Reset Double Click Status' i.e. Acknowledge last press.
bool CDIMouse.Acquire(bool state) Acquire/Unacquire the Mouse Device.  You will need to Unacquire the device if you want Windows to handle Mouse messages or another application is being switched in.
state=true To Acquire
state=false To UnAcquire

Returns : true=Operation Successful, false=Operation unsuccessful

Before obtaining mouse state information you will need to set the HWND of the parent window of your application.  You can do this by calling CDIMouse.SetHWND(g_hwnd) for Win32 Applications and CDIMouse.SetHWND(this) for MFC applications.  

Always use PollDevice method before checking the Mouse state information.

The following code snippet can be used to show the mouse status

BOOL CMouseTestDlg::OnInitDialog()

 // Add "About..." menu item to system menu.

 // IDM_ABOUTBOX must be in the system command range.

 CMenu* pSysMenu = GetSystemMenu(FALSE);
 if (pSysMenu != NULL)
  CString strAboutMenu;
  if (!strAboutMenu.IsEmpty())

 // Set the icon for this dialog.
 // The framework does this automatically
 //  when the application's main window is not a dialog
 SetIcon(m_hIcon, TRUE);  // Set big icon
 SetIcon(m_hIcon, FALSE); // Set small icon


 // TODO: Add extra initialization here

 // return TRUE  unless you set the focus to a control
 return TRUE;  

void CMouseTestDlg::OnTimer(UINT nIDEvent)
 // TODO: Add your message handler code here
 // and/or call default

  char buff[80]={"01234567"};
  buff[0]=myMouse.IsButton0()?'0':' ';
  buff[1]=myMouse.IsButton1()?'1':' ';
  buff[2]=myMouse.IsButton2()?'2':' ';
  buff[3]=myMouse.IsButton3()?'3':' ';
  buff[4]=myMouse.IsButton4()?'4':' ';
  buff[5]=myMouse.IsButton5()?'5':' ';
  buff[6]=myMouse.IsButton6()?'6':' ';
  buff[7]=myMouse.IsButton7()?'7':' ';

  if(myMouse.IsMouseLeft())m_Left="X";else m_Left=" ";
  if(myMouse.IsMouseRight())m_Right="X";else m_Right=" ";
  if(myMouse.IsMouseUp())m_Up="X";else m_Up=" ";
  if(myMouse.IsMouseDown())m_Down="X";else m_Down=" ";

  CString temp;
  for(int i=0;i<8;i++)



#pragma warning(disable : 4800)
LRESULT CMouseTestDlg::DefWindowProc(UINT message, 
                                     WPARAM wParam, 
                                     LPARAM lParam) 
 // TODO: Add your message handler code here
 // and/or call default
  case WM_ACTIVATEAPP: myMouse.Acquire((bool)wParam);

 return CDialog::DefWindowProc(message, wParam, lParam);

#pragma warning(error : 4800)


Download base source project - 4 Kb
Download demo project - 2 Kb


  • Questions about how to detect multiple USB mice

    Posted by Legacy on 04/30/2003 12:00am

    Originally posted by: Tam Duong

    Thanks for your code. I try to detect multiple USB mice on my system. When I use DirectInput to query MOUSE attached I just got only one in my callback funtion. There are any methods to detect these USB mice. The reason I use these USB mice to test all my USB port working fine.



  • It's has latency bug

    Posted by Legacy on 07/01/2001 12:00am

    Originally posted by: Lee joo-young

    When CDIMouse initializing, it doesn't initialize member variable m_Button0, m_Buton1, ... m_Button7. So these members would have non-zero (ture) value. Though I don't press mouse button, IsButton0(), IsButton1(), ... , IsButton7() may be return ture at result.

    p.s In CDIMouse::InitMouse() Line 43,
    'ZeroMemory(&m_Buttons, sizeof(m_Buttons))'
    must change to
    'ZeroMemory(m_Buttons, sizeof(m_Buttons))'

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 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds