获取输入设备的vid和pid

一、获取/dev/input/event16设备的vid和pid

testhid.c

#include <linux/types.h>
#include <linux/input.h>
#include <linux/hidraw.h>

#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <poll.h>
#include <time.h>
#include <math.h>

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
int main(){
  char filename[64];
  strcpy(filename,"/dev/input/event16");
  char name[80];
  int fd = open(filename, O_RDWR /*| O_NONBLOCK*/);
  if(fd <= 0) {
    printf("TK----------->>>>open error\n");
    return -1;
  }
  /
  struct input_id inputId;
  int rc = ioctl(fd, EVIOCGID, &inputId);
  printf("TK-------->>>>info.vendor is 0x%x\n",inputId.vendor);
  printf("TK-------->>>>info.product is 0x%x\n",inputId.product);
  printf("TK-------->>>>info.bustype is 0x%x\n",inputId.bustype);
  /*
  struct hidraw_devinfo info;
  int rc = ioctl(fd, HIDIOCGRAWINFO, &info);
  printf("TK-------->>>>info.vendor is 0x%x\n",info.vendor);
  printf("TK-------->>>>info.product is 0x%x\n",info.product);
  printf("TK-------->>>>info.bustype is 0x%x\n",info.bustype);
  */
  ///
  struct hidraw_report_descriptor descriptor;
  rc = ioctl(fd, HIDIOCGRDESC, &descriptor);
  printf("TK-------->>>>descriptor.size is 0x%04x\n",descriptor.size);
  //
  int descriptorSize=0;
  rc = ioctl(fd, HIDIOCGRDESCSIZE, &descriptorSize);
  printf("TK-------->>>>descriptorSize is 0x%04x\n",descriptorSize);  
  //
  name[sizeof(name) - 1] = '\0';
  if(ioctl(fd, EVIOCGNAME(sizeof(name) - 1), &name) < 1) {
    name[0] = '\0';
  }
  printf("TK------->>>name is %s\n",name);
  //
  close(fd);
  return 0;
}
二、编译

Android.mk

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

LOCAL_SRC_FILES:= \
	testhid.c

LOCAL_SHARED_LIBRARIES := \
	libutils 

LOCAL_MODULE:= testinput

LOCAL_MODULE_TAGS := optional

include $(BUILD_EXECUTABLE)
三、运行
TK-------->>>>info.vendor is 0x0
TK-------->>>>info.product is 0x1
TK-------->>>>info.bustype is 0x19
TK-------->>>>descriptor.size is 0x0000
TK-------->>>>descriptorSize is 0x0000
TK------->>>name is Power Button

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C++中处理USB输入信号可以通过调用Windows平台提供的API函数来实现。其中,使用WinUSB API是处理USB输入信号的一种常见方式。以下是一个简单的例子,演示如何使用WinUSB API在C++中读取USB输入信号: ```cpp #include <Windows.h> #include <winusb.h> #define USB_VID 0x1234 #define USB_PID 0x5678 int main() { // 打开WinUSB设备 HANDLE hDev = NULL; hDev = CreateFile(L"\\\\.\\WinUSBInterface1", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL); if (hDev == INVALID_HANDLE_VALUE) { printf("Failed to open device!\n"); return 1; } // 初始化WinUSB上下文 WINUSB_INTERFACE_HANDLE hWinUSB = NULL; BOOL bResult = WinUsb_Initialize(hDev, &hWinUSB); if (!bResult) { printf("Failed to initialize WinUSB context!\n"); return 1; } // 读取USB输入信号 UCHAR buffer[64] = { 0 }; ULONG length = 0; bResult = WinUsb_ReadPipe(hWinUSB, 0x81, buffer, sizeof(buffer), &length, NULL); if (!bResult) { printf("Failed to read USB input signal!\n"); return 1; } // 输出读取到的数据 printf("Read %d bytes from USB input signal: ", length); for (ULONG i = 0; i < length; i++) { printf("%02X ", buffer[i]); } printf("\n"); // 关闭WinUSB设备 WinUsb_Free(hWinUSB); CloseHandle(hDev); return 0; } ``` 需要注意的是,在使用WinUSB API进行USB输入信号处理时,需要先获取设备的句柄,然后通过WinUsb_Initialize函数初始化WinUSB上下文,最后使用WinUsb_ReadPipe函数读取USB输入信号。此外,还需要根据USB设备的厂商ID和产品ID来打开正确的设备
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值