键盘,鼠标状态 [英] Keyboard, Mouse Status

查看:121
本文介绍了键盘,鼠标状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要检查鼠标/键盘是否已连接到PC.

我已经尝试过使用user32.dll.它只显示光标位置.

我也尝试过Win32_keyboard类,我的状态为"OK".但是,当我断开键盘与PC的连接时.它仍然显示确定"状态.它应该给我错误状态.但是这里没有发生.

解决此问题的任何其他解决方案.请帮助!!

在此先感谢..

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

推荐答案

您可以使用SetupDiGetClassDevsEx [ WM_DEVICECHANGE [ ^ ]处理程序.

[更新:示例代码]

使用MFC时,将WM_DEVICECHANGE处理程序添加到您的主框架类(或具有对话框应用程序的主对话框)中.使用Win32应用程序时,请在主窗口/应用程序的消息循环中处理消息. wParam是事件类型,而lParam是数据指针.

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
ON_WM_DEVICECHANGE()

// Implementation
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
        return -1;
    
    // other init code here
    
    DEV_BROADCAST_DEVICEINTERFACE Notify;
    ::ZeroMemory(&Notify, sizeof(DEV_BROADCAST_DEVICEINTERFACE));
    Notify.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
    Notify.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
    const GUID GUID_DEVINTERFACE_HDI =
        { 0x4d1e55b2, 0xf16f, 0x11cf, { 0x88, 0xcb, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30}};
    Notify.dbcc_classguid = GUID_DEVINTERFACE_HDI;
    ASSERT(this->GetSafeHwnd());
    HDEVNOTIFY hDevNotify = ::RegisterDeviceNotification(
        this->GetSafeHwnd(),
        &Notify, 
        DEVICE_NOTIFY_WINDOW_HANDLE);
    ASSERT(HDEVNOTIFY);
    	
    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.
    if (nEventType == DBT_DEVICEARRIVAL || nEventType == DBT_DEVICEREMOVECOMPLETE)
    {
        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
            PDEV_BROADCAST_DEVICEINTERFACE pDev = 
                reinterpret_cast<PDEV_BROADCAST_DEVICEINTERFACE>(dwData);
            TRACE2("%s %s\n", 
                nEventType == DBT_DEVICEARRIVAL ? "Arrival of" : "Removed", 
                pDev->dbcc_name);
            TCHAR lpszBuf[256];
            if (::StringFromGUID2(pDev->dbcc_classguid, lpszBuf, 256))
                TRACE1("GUID: %s\n", lpszBuf);
        }
    }
    return nResult;
}


卸下和插入USB鼠标时的示例输出:


Example output when removing and inserting USB mouse:

WM_DEVICECHANGE event 0X8004
Removed \\?\HID#VID_046D&PID_C018#6&1e99df8d&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
GUID: {4D1E55B2-F16F-11CF-88CB-001111000030}
WM_DEVICECHANGE event 0X8000
Arrival of \\?\HID#VID_046D&PID_C018#6&1e99df8d&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
GUID: {4D1E55B2-F16F-11CF-88CB-001111000030}


看看:
http://msdn.microsoft.com/zh-我们/library/windows/hardware/ff542441%28v=vs.85%29.aspx [
Have a look at:
http://msdn.microsoft.com/en-us/library/windows/hardware/ff542441%28v=vs.85%29.aspx[^]
...cannot determine, if the device is later physically removed.


同意Jochen Arndt和trotwa解决方案,此外,我也认为PS/2不能正常工作,因为该接口如Jochen所说,它并不是为热插拔设备设计的(即使有时可以使用).

但是,有一个简单的解决方案可以通过WMI的SELECT * FROM Win32_Keyboard查询和Status属性检测到它(如果在启动系统时已连接到PS/2).

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()
        {
            try
            {
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher("root\\CIMV2",
                    "SELECT * FROM Win32_Keyboard");

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




问候




Regards


这篇关于键盘,鼠标状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆