通过wmi可以获取当前连接的键盘的名称等信息,但将键盘拔下后,获取的信息依然不变。
是不是应通过其他方法获取该动作?
谢谢!

解决方案 »

  1.   

    添加WM_DEVICECHANGE的消息处理函数OnDeviceChange,在该函数中捕获DBT_DEVICEARRIVAL事件(当U盘插入时,OS会发WM_DEVICECHANGE广播消息,该消息的wParam中会包含DBT_DEVICEARRIVAL事件),同时得到DEV_BROADCAST_VOLUME结构的数据
      

  2.   

    西北狼,谢谢
    你确认这样可以监测到键盘拔出吗?能否讲详细一点,或者有没有demo程序参考一下呢?
      

  3.   

    afx_msg void OnDeviceChange(WPARAM wParam,LPARAM lParam); ON_MESSAGE(WM_DEVICECHANGE,OnDeviceChange)
    void  CWndMessDlg::OnDeviceChange(WPARAM wParam,LPARAM lParam)
    {
    UINT Event = (UINT) wParam;
        PDEV_BROADCAST_HDR pHDR = (PDEV_BROADCAST_HDR) lParam;
    CString strMes ;

    switch(Event) {
    case DBT_CONFIGCHANGECANCELED:
    strMes = "A request to change the current configuration (dock or undock) has been canceled. ";
    break;
    case DBT_CONFIGCHANGED:
    strMes = "The current configuration has changed, due to a dock or undock.";
    break;
    case DBT_DEVICEARRIVAL:
    strMes = "A device has been inserted and is now available. " ;
    strMes += "\n";
    switch(pHDR->dbch_devicetype) {
    case DBT_DEVTYP_OEM :
    strMes += "OEM- or IHV-defined device type. ";
    break;
    case DBT_DEVTYP_VOLUME:
    strMes += " Logical volume.  ";
    break;
    case  DBT_DEVTYP_PORT:
    strMes += " Port device (serial or parallel). ";
    break;
    default:
    break;
    }
    break;
    case DBT_DEVICEQUERYREMOVE :
    strMes = "Permission is requested to remove a device. Any application can deny this request and cancel the removal. ";
    break;
    case DBT_DEVICEQUERYREMOVEFAILED:
    strMes = "A request to remove a device has been canceled. ";
    break;
    case DBT_DEVICEREMOVECOMPLETE:
    strMes = "A device has been removed. ";
    break;
    case DBT_DEVICEREMOVEPENDING:
    strMes = "A device is about to be removed. Cannot be denied. ";
    break;
    case DBT_DEVICETYPESPECIFIC:
    strMes = "A device-specific event has occurred. ";
    break;
    case DBT_QUERYCHANGECONFIG:
    strMes = "Permission is requested to change the current configuration (dock or undock).  ";
    break;
    case DBT_USERDEFINED:
    strMes ="The meaning of this message is user-defined. ";
    break;
    default:
    break;

    }  
      AfxMessageBox(strMes);}
    还有一个 afx_msg void OnDevModeChange(WPARAM wParam,LPARAM lParam);
    ON_MESSAGE(WM_DEVMODECHANGE,OnDevModeChange)
    void CWndMessDlg::OnDevModeChange(WPARAM wParam,LPARAM lParam)
    {
    CString str  = (LPTSTR )lParam;
    AfxMessageBox(str);
    }
      

  4.   

    谢谢,我测试了监听WM_DEVICECHANGE消息,但只能监测到u盘的插入和拔出,键盘插拔无任何响应,是不是这个消息对键盘无效?
      

  5.   

    建议楼主在MSDN下搜索WM_DEVICECHANGE,看看MSDN先刚才稍微看了下,感觉西北狼的方法应该是有希望的看到这个结构体没?DEV_BROADCAST_HDR你在监听这个WM_DEVICECHANGE事件的类型号为DBT_DEVICEREMOVECOMPLETE(拔出设备时),
    它的lParam的解释如下:
    lParam 
    Pointer to a structure identifying the device removed. The structure consists of an event-independent header, followed by event-dependent members that describe the device. To use this structure, treat the structure as a DEV_BROADCAST_HDR structure, then check its dbch_devicetype member to determine the device type.重要的是最后一句,也就是DEV_BROADCAST_HDR的第二个参数dbch_devicetype,你再去看下这个里面,应该可以得到启示了
    这个参数的值可以为DBT_DEVTYP_PORT这个结构体,再跟下去看下,第四个参数(设备名称)
    dbcp_name 
    Pointer to a null-terminated string specifying the friendly name of the port or the device connected to the port. Friendly names are intended to help the user quickly and accurately identify the device—for example, "COM1" and "Standard 28800 bps Modem" are considered friendly names.我也不太确定,如果我们的键盘是接在PS/2接口上,应该这里填上这个就可以了
    应该就可以搜索到键盘了呵呵,我也只是看了MSDN做下猜测,不对之处,见谅