Direct Input 7 Mouse Class
Environment: Windows 9x, Windows 2000; Visual C++ 6 (SP4), DirectX 7 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 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:
|
Method |
Description |
| 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()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING,
IDM_ABOUTBOX,
strAboutMenu);
}
}
// 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
myMouse.SetHWND(this);
myMouse.Acquire(true);
SetTimer(1,50,NULL);
// 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
if(m_AppActive)
{
myMouse.PollDevice();
m_MouseX.Format("%d",myMouse.GetRelativeX());
m_MouseY.Format("%d",myMouse.GetRelativeY());
m_MouseZ.Format("%d",myMouse.GetRelativeZ());
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=" ";
m_Pressed2.Empty();
CString temp;
for(int i=0;i<8;i++)
{
if(myMouse.IsDoubleClicked(i))m_Pressed2+=i+0x30;
}
m_Pressed=buff;
UpdateData(FALSE);
}
CDialog::OnTimer(nIDEvent);
}
#pragma warning(disable : 4800)
LRESULT CMouseTestDlg::DefWindowProc(UINT message,
WPARAM wParam,
LPARAM lParam)
{
// TODO: Add your message handler code here
// and/or call default
switch(message)
{
case WM_ACTIVATEAPP: myMouse.Acquire((bool)wParam);
m_AppActive=wParam;
break;
}
return CDialog::DefWindowProc(message, wParam, lParam);
}
#pragma warning(error : 4800)
Downloads
Download base source project - 4 KbDownload demo project - 2 Kb

Comments
Questions about how to detect multiple USB mice
Posted by Legacy on 04/30/2003 12:00amOriginally 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.
Thanks...
Tam
ReplyIt's has latency bug
Posted by Legacy on 07/01/2001 12:00amOriginally 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,
Reply'ZeroMemory(&m_Buttons, sizeof(m_Buttons))'
must change to
'ZeroMemory(m_Buttons, sizeof(m_Buttons))'