Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I want to check that whether the mouse/Keyboard is connected to PC or not.

I have tried by using user32.dll . Its showing me the cursor position only.

Also I have tried with the Win32_keyboard class, I am getting the status as " OK ". But when I disconnect the Keyboard from PC. Still it shows the "OK" status. It should give me the Error status. But its not happening here.

Any other solution for solving this problem. Plz Help !!

Thanks in Advance ..
hjgc2001 12-Nov-13 20:19pm    
I meet the problem same. How you solve this problem lastly?Thanks

You may use the SetupDiGetClassDevsEx [^] Setup API function to retrieve information on keyboards and mouses (you may connect more than one of each).

To detect arrival or removal of devices (including mouse and keyboard), you can add a WM_DEVICECHANGE [^] handler to your app.

[UPDATE: Example code]

When using MFC, add the WM_DEVICECHANGE handler to your main frame class (or the main dialog with dialog apps). With Win32 apps, handle the message in the message loop of your main window/app. The wParam is the event type and the lParam is the data pointer.

#include <dbt.h>

// Declaration in header file of main frame class.
// afx_msg BOOL CMainFrame::OnDeviceChange(UINT nEventType, DWORD_PTR dwData);

// Message map entry

// Implementation
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
    if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
        return -1;
    // other init code here
    ::ZeroMemory(&Notify, sizeof(DEV_BROADCAST_DEVICEINTERFACE));
    Notify.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
    Notify.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
        { 0x4d1e55b2, 0xf16f, 0x11cf, { 0x88, 0xcb, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30}};
    Notify.dbcc_classguid = GUID_DEVINTERFACE_HDI;
    HDEVNOTIFY hDevNotify = ::RegisterDeviceNotification(
    return 0;

BOOL CMainFrame::OnDeviceChange(UINT nEventType, DWORD_PTR dwData)
    // Call base class handler (base class name may need adjustment)
    BOOL nResult = CMDIFrameWnd::OnDeviceChange(nEventType, dwData);
    // Process events here. 
    // Arrival and removal send a DEV_BROADCAST_HDR.
        TRACE1("WM_DEVICECHANGE event %#X\n", nEventType);
        PDEV_BROADCAST_HDR pHdr = reinterpret_cast<PDEV_BROADCAST_HDR>(dwData);
        if (pHdr->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)
            // Interface data
            TRACE2("%s %s\n", 
                nEventType == DBT_DEVICEARRIVAL ? "Arrival of" : "Removed", 
            TCHAR lpszBuf[256];
            if (::StringFromGUID2(pDev->dbcc_classguid, lpszBuf, 256))
                TRACE1("GUID: %s\n", lpszBuf);
    return nResult;

Example output when removing and inserting USB mouse:
Removed \\?\HID#VID_046D&PID_C018#6&1e99df8d&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
GUID: {4D1E55B2-F16F-11CF-88CB-001111000030}
Arrival of \\?\HID#VID_046D&PID_C018#6&1e99df8d&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
GUID: {4D1E55B2-F16F-11CF-88CB-001111000030}
Share this answer
trotwa 3-May-12 9:52am    
Yes for USB. Does it work also for PS/2 and serial ?
Jochen Arndt 3-May-12 10:07am    
I did not have done that so far for keyboard and mouse. But the Setup API returns the states as shown in the Windows system config which includes at least PS/2 (I'm not sure with serial ones). I don't know if WM_DEVICECHANGE messages are send for PS/2 and serial. These interfaces are not designed for hot plugging (the system should be switched off to avoid damages). But Windows itself recognizes insertion.
yoyogesh 15-May-12 7:16am    
Thank you for giving valuable response...

I am interested in WM_DEVICECHANGE . By using this API ,Will you plz give me some sample program for getting the status of Mouse or Keyboard. I want to detect whether these devices are removed or pluged in while windows is in running mode.

Thanks in Advance.
Jochen Arndt 15-May-12 8:23am    
I have just tested it. When plugging an USB mouse, the device change is indicated with the event DBT_DEVNODES_CHANGED that provides no additional information about the device itself. To receive notifications for such devices, you may use the RegisterDeviceNotification API function with the GUID for human interface devices (see See also this CP article:
yoyogesh 15-May-12 8:42am    
Thank you for the quick reply..

Will you please post one sample code for Mouse. ?
Have a look at:[^]
...cannot determine, if the device is later physically removed.
Share this answer
Agree to Jochen Arndt and trotwa solutions, in addition I also don't think PS/2 is working because this interface isn't designed for Hot plugging Devices (even if it works sometimes), as Jochen also said.

But there's a simple solution to detect it (if it's connected to PS/2 when booting your system) through WMI's SELECT * FROM Win32_Keyboard query and Status Property.

using System;
using System.Management;
using System.Windows.Forms;

namespace WMISample
    public class MyWMIQuery
        public static void Main()
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher("root\\CIMV2",
                    "SELECT * FROM Win32_Keyboard");

                foreach (ManagementObject queryObj in searcher.Get())
                    Console.WriteLine("Win32_Keyboard instance");
                    Console.WriteLine("Status: {0}", queryObj["Status"]);
            catch (ManagementException e)
                MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);

Share this answer
hjgc2001 12-Nov-13 9:35am    
I hvae used WMI to retrieve the staus of keyboard, but the method cann't work correctly. When I removed these devices, the program shows OK also.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900