现在的位置: 首页 > 综合 > 正文

关于原始输入

2013年11月09日 ⁄ 综合 ⁄ 共 6985字 ⁄ 字号 评论关闭

文章摘自Microsoft官网和http://www.cppblog.com/shenhuafeng/

 

About Raw Input

There are many user-input devices beside the traditional keyboard and mouse. For example, user input can come from a joystick, a touch screen, a microphone, or other devices that allow great flexibility in user input. These devices are collectively known as Human Interface Devices (HIDs). The raw input API provides a stable and robust way for applications to accept raw input from any HID, including the keyboard and mouse.

This section covers the following topics:

 

Raw Input Model

Previously, the keyboard and mouse typically generated input data. The system interpreted the data coming from these devices in a way that eliminated the device-specific details of the raw information. For example, the keyboard generates the device-specific scan code but the system provides an application with the virtual key code. Besides hiding the details of the raw input, the window manager did not support all the new HIDs. To get input from the unsupported HIDs, an application had to do many things: open the device, manage the shared mode, periodically read the device or set up the I/O completion port, and so forth. The raw input model and the associated APIs were developed to allow simple access to raw input from all input devices, including the keyboard and mouse.

The raw input model is different from the original Windows input model for the keyboard and mouse. In the original input model, an application receives device-independent input in the form of messages that are sent or posted to its windows, such as WM_CHAR, WM_MOUSEMOVE, and WM_APPCOMMAND. In contrast, for raw input an application must register the devices it wants to get data from. Also, the application gets the raw input through the WM_INPUT message.

There are several advantages to the raw input model:

  • An application does not have to detect or open the input device.
  • An application gets the data directly from the device, and processes the data for its needs.
  • An application can distinguish the source of the input even if it is from the same type of device. For example, two mouse devices.
  • An application manages the data traffic by specifying data from a collection of devices or only specific device types.
  • HID devices can be used as they become available in the marketplace, without waiting for new message types or an updated OS to have new commands in WM_APPCOMMAND.

Note that WM_APPCOMMAND does provide for some HID devices. However, WM_APPCOMMAND is a higher level device-independent input event, while WM_INPUT sends raw, low level data that is specific to a device.

Registration for Raw Input

By default, no application receives raw input. To receive raw input from a device, an application must register the device.

To register devices, an application first creates an array of RAWINPUTDEVICE structures that specify the top level collection (TLC) for the devices it wants. The TLC is defined by a Usage Page (the class of the device) and a Usage (the device within the class). For example, to get the keyboard TLC, set UsagePage = 1 and Usage = 6. The application calls RegisterRawInputDevices to register the devices.

Note that an application can register a device that is not currently attached to the system. When this device is attached, the Windows Manager will automatically send the raw input to the application. To get the list of raw input devices on the system, an application calls GetRawInputDeviceList. Using the hDevice from this call, an application calls GetRawInputDeviceInfo to get the device information.

Through the dwFlags member of RAWINPUTDEVICE, an application can select the devices to listen to and also those it wants to ignore. For example, an application can ask for input from all telephony devices except for answering machines. For sample code, see Registering for Raw Input.

Note that the mouse and the keyboard are also HIDs, so data from them can come through both the HID message WM_INPUT and from traditional messages. An application can select either method by the proper selection of flags in RAWINPUTDEVICE.

To get an application's registration status, call GetRegisteredRawInputDevices at any time.

Reading Raw Input

An application receives raw input from any HID whose top level collection (TLC) matches a TLC from the registration. When an application receives raw input, its message queue gets a WM_INPUT message and the queue status flag QS_RAWINPUT is set (QS_INPUT also includes this flag). An application can receive data when it is in the foreground and when it is in the background.

There are two ways to read the raw data: the unbuffered (or standard) method and the buffered method. The unbuffered method gets the raw data one RAWINPUT structure at a time and is adequate for many HIDs. Here, the application calls GetMessage to get the WM_INPUT message. Then the application calls GetRawInputData using the RAWINPUT handle contained in WM_INPUT. For an example, see Doing a Standard Read of Raw Input.

In contrast, the buffered method gets an array of RAWINPUT structures at a time. This is provided for devices that can produce large amounts of raw input. In this method, the application calls GetRawInputBuffer to get an array of RAWINPUT structures. Note that the NEXTRAWINPUTBLOCK macro is used to traverse an array of RAWINPUT structures. For an example, see Doing a Buffered Read of Raw Input.

To interpret the raw input, detailed information about the HIDs is required. An application gets the device information by calling GetRawInputDeviceInfo with the device handle. This handle can come either from WM_INPUT or from the hDevice member of RAWINPUTHEADER.

 

 

 

引用:http://www.cppblog.com/shenhuafeng/archive/2006/09/15/12530.html

关于原始输入

除了传统的键盘和鼠标以外还有很多其他的输入设备。例如:用户输入可以来自游戏杆设备,触摸屏,麦克风以及其他可以提供用户输入的设备。这些设备被统称为HID(人体工程学)设备。Raw Input的API为应用程序提供了稳定健壮的读取原始数据数据的方式,包括键盘和鼠标。

这篇文章主要包括3个部分:

·                原始输入模型

·                注册原始输入

·                读取原始输入

原始输入模型

以前鼠标和键盘数据处理模式是这样的,鼠标和键盘产生输入数据,系统中断去处理这些与设备信息相关的数据,让这些数据变得与设备无关。例如:键盘产生与键盘设备相关的ScanCode但是系统提供给应用程序虚拟键码。除了隐藏原始输入的细节,Windows管理器还不支持所有新的HID设备。为了要从HID设备里面得到信息,一个应用程序必须处理以下步骤:打开设备,管理共享模式,周期性读取设备或者设置IO完成端口,或者更多操作。原始输入模型及其相关的API允许比较容易的从输入设备中获取原始输入,包括键盘和鼠标。

那么原始输入模型和微软原来的鼠标键盘输入模型有什么差别呢?在原来的输入模型,一个应用程序通过发送到他窗口的消息获取与设备无关的消息,例如WM_CHAR,WM_MOUSEMOVE和WM_APPCOMMAND。与之原来模式不同的是:一个应用程序想获取原始数据的必须注册他想要获取原始输入的那些设备,应用程序会收到WM_INPUT消息。

原始输入模型有很多优点:

  1.  应用程序不需要查找和打开输入设备。
  2.  应用程序直接从设备获取数据并根据他们的需要获取数据。
  3. 应用程序能够甄别来自两个不同设备的输入,尽管他们有可能是同一个类型的设备。例如:两个鼠标设备。
  4. 应用程序能够管理数据流,可以从一类设备或者某种制定类型设备获取输入数据。
  5. 当HID设备在市场上可以买到时候,能够立即被使用,而不需要等待操作系统更新新的消息定义或者操作系统更新WM_APPCOMMAND消息。

需要注意的是:WM_APPCOMMAND确实是为一些HID设备提供的。然而WM_APPCOMMAND是一个高层的非设备相关的输入事件,而WM_INPUT消息发送原始的底层的设备相关的消息。

注册原始输入

默认情况下,没有应用程序会接受WM_INPUT消息。为了接受从一个设备发送原始输入,你必须注册这个设备。

为了注册这个设备,一个应用程序首先必须创建一个指明他所希望接受设备类别的(top level collection――
TLC)RAWINPUTDEVICE结构。TLC被定义成为UsagePage(设备类)和Usage(设备类内的具体设备)。例如为了从键盘获取原始输入,设置UsagePage = 1 and Usage = 6,应用程序调用RegisterRawInputDevice去注册这个设备。

注意:应用程序可以注册系统当前没有的设备。当设备可用之后,Windows管理器会自动将原始输入数据发送到应用程序。应用程序可以调用GetRawInputDeviceList来获取系统中原始输入设备的列表。用GetRawInputDeviceList获取的hDevice,应用程序调用GetRawInputDeviceInfo获取设备信息。

通过RAWINPUTDEVICE中的dwFlag,应用程序可以选择是否监听还是忽略来自某个指定设备的信息。例如:一个应用程序能够监听所有的电话设备除了答录机。

注意:鼠标和键盘也是HID设备,所以能够从Hid设备的WM_INPUT消息或者从传统的消息中获取信息。应用程序能够通过指定RAWINPUTDEVICE中的标志位选择任意一个。

可以调用GetRegisteredRawInputDevice来得到应用程序的该设备的注册状态。

读取原始输入

应用程序会收到符合所注册的TLC的HID设备的原始输入消息。当一个应用程序收到了原始输入,应用程序的消息队列就会得到一个WM_INPUT消息,系统状态被置成QS_RAWINPUT(QS_INPUT)同样包含这个标志。不管应用程序在前台和后台都能够收到消息。

有两种方法去读取原始数据:标准(没有缓冲的)方法和缓冲方法。前者获取原始输入时,每次获取一个RAWINPUT数据,而且对于大多数HID设备都是可以用这种方式读取的。应用程序调用CallMessage得到WM_INPUT消息,然后应用程序通过WM_INPUT消息,调用GetRawInputData来获取RAWINPUT句柄。

相对应的,缓存方式每次得到一系列的RAWINPUT结构。这是给那些能够构产生很大数据量的原始输入。用这种方法去获取数据,首先调用GetRawInputBuffer去获取一系列的RAWINPUT结构。注意NEXTTRAWINPUTBLOCK宏是用来获取下一个RAWINPUT结构的。

为了获取原始输入HID设备的详细信息。应用程序可以用GetRawInputdeviceInfo来查询相对应的句柄。这个句柄可以是从WM_INPUT消息或者RAWINPUTHRADER.hDevice获取。

抱歉!评论已关闭.