龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > VC开发 >

VC学习:获取游戏手柄的按键输入(2)

时间:2009-12-30 15:42来源:未知 作者:admin 点击:
分享到:
_tprintf("找到了一个USB设备:"); //若找到了一个USB设备,则获取该设备的细节信息 PSP_DEVICE_INTERFACE_DETAIL_DATA pDeviceInterfaceDetailData; DWORD Length = 0; SetupDiGetDeviceIn
  _tprintf("找到了一个USB设备: ");
  //若找到了一个USB设备,则获取该设备的细节信息
  PSP_DEVICE_INTERFACE_DETAIL_DATA pDeviceInterfaceDetailData;
  DWORD Length = 0;
  SetupDiGetDeviceInterfaceDetail(hDevInfo,
  &DeviceInterfaceData,
  NULL,
  0,
  &Length,
  NULL);
  pDeviceInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(Length);
  pDeviceInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); //MUST BE!!!

  if (!SetupDiGetDeviceInterfaceDetail(hDevInfo,
  &DeviceInterfaceData,
  pDeviceInterfaceDetailData,
  Length,
  NULL,
  NULL))
  _tprintf("查找路径设备时出错! ");
  else
  _tprintf("设备路径:%s ",pDeviceInterfaceDetailData->DevicePath );

  //打开设备句柄
  HANDLE hDeviceHandle = CreateFile(pDeviceInterfaceDetailData->DevicePath ,
  GENERIC_READ | GENERIC_WRITE,
  FILE_SHARE_READ | FILE_SHARE_WRITE,
  NULL,
  OPEN_EXISTING,
  0,
  NULL);
  if (hDeviceHandle == INVALID_HANDLE_VALUE)
  _tprintf("打开设备路径出错! ");
  else

  {
  HIDD_ATTRIBUTES Attributes;
  HidD_GetAttributes(hDeviceHandle,&Attributes);
  //将有关该设备的标识显示出来
  _tprintf("供应商ID :0X%04X ",Attributes.VendorID);
  _tprintf("产品ID :0X%04X ",Attributes.ProductID);
  _tprintf("产品版本号:0X%04X ",Attributes.VersionNumber);

  WCHAR mString[256];
  TCHAR Buffer[256];

  HidD_GetManufacturerString(hDeviceHandle,mString,sizeof(mString));
  if (wcstombs(Buffer,mString,256) == -1) // fail
  Buffer[0] = NULL;
  _tprintf("生产商: %s ",Buffer);

  HidD_GetProductString(hDeviceHandle,mString,sizeof(mString));
  if (wcstombs(Buffer,mString,256) == -1)
  Buffer[0] = NULL;
  _tprintf("产品名称: %s ",Buffer);

  // 通信:
  PHIDP_PREPARSED_DATA pHidpPreparsedData;
  HIDP_CAPS hidPCaps;
  if (!HidD_GetPreparsedData(hDeviceHandle,&pHidpPreparsedData))
  {
  _tprintf("获取 HID PREPARED DATA 失败! ");
  return;
}
  NTSTATUS status = HidP_GetCaps(pHidpPreparsedData,&hidPCaps);
  if (status == HIDP_STATUS_SUCCESS)
  {
  _tprintf("CAP信息如下: ");
  _tprintf(" InputReportByteLength %d ", hidPCaps.InputReportByteLength);
  _tprintf(" OutputReportByteLength %d ", hidPCaps.OutputReportByteLength);
  }
  DWORD nReadBytes = 0;
  BYTE *pInputReport = new BYTE[hidPCaps.InputReportByteLength];
  memset(pInputReport,0,hidPCaps.InputReportByteLength);

  do
  {
  ReadFile(hDeviceHandle,pInputReport,hidPCaps.InputReportByteLength,&nReadBytes,NULL);
  if (hidPCaps.InputReportByteLength == nReadBytes)
  {
  for(unsigned int i=0; i<(nReadBytes-1);i++)
  _tprintf("%02x-",pInputReport[i]);
  _tprintf("%02x ",pInputReport[nReadBytes-1]);
  }
  if (pInputReport[nReadBytes-2] == 0x20) //break the loop when pressing a specific key
  {
  _tprintf(" ");
  break;
  }
  Sleep(10);
  }while(hidPCaps.InputReportByteLength == nReadBytes);

  //释放句柄资源
  CloseHandle(hDeviceHandle);
  }

  MemberIndex++;
  }while(bSuccess);

  SetupDiDestroyDeviceInfoList(hDevInfo);
  }

精彩图集

赞助商链接