近期公司项目用到扫码枪,有可能会遇到焦点不在程序上的情况,扫码枪做为一个键盘设备,焦点在哪数据也将输出到焦点的位置,在网上找了好久,基本上用Hook的比较多,但是都不尽人意,键盘和扫码枪的数据无法准确的区分,整合了好几位网友给出的方案并查阅了相关信息后终于相对完美的解决了这一个问题。
首先,根据小学狗喵喵叫的文章获取到了系统中的所有VID设备,在使用时选择指定的设备即可,C++获取所有USB接口的vid、pid等信息_小学狗喵喵叫的博客-CSDN博客_c++ usb vid。实现代码如下:
.h
#pragma once
#include "afxdialogex.h"
#include <iostream>
#include <string>
#include <fstream>
#include <setupapi.h>
#include <devguid.h>
#include <winioctl.h>
#include <regstr.h>
#include <conio.h>
#include <vector>
#pragma comment(lib,"setupapi.lib")
extern "C" {
#include "setupapi.h"
#include <hidsdi.h>
};
using namespace::std;
struct _VID_PID_
{
USHORT V_ID;
USHORT P_ID;
_VID_PID_( USHORT vid, USHORT pid )
{
V_ID = vid;
P_ID = pid;
}
bool operator==( const _VID_PID_ &other )
{
if (V_ID == other.V_ID && P_ID == other.P_ID)
return true;
return false;
}
};
class Scanner
{
public:
vector<_VID_PID_> GetDeviceVector( );
private:
void HID_FindAllDevices( unsigned short *FIFO_Length );
private:
vector<_VID_PID_> vecVidPid;
};
.cpp
#include "pch.h"
#include "Scanner.h"
vector<_VID_PID_> Scanner::GetDeviceVector( )
{
unsigned shor