获取游戏手柄的按键输入[2]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 mrh123 的 blog

   _tprintf("产品版本号:0x%04x\n",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("生产商:\t%s\n",buffer);
   
   hidd_getproductstring(hdevicehandle,mstring,sizeof(mstring));
   if (wcstombs(buffer,mstring,256) == -1)
    buffer[0] = null;
   _tprintf("产品名称:\t%s\n",buffer);

   // 通信:
   phidp_preparsed_data phidppreparseddata;
   hidp_caps    hidpcaps;
   if (!hidd_getpreparseddata(hdevicehandle,&phidppreparseddata))
   {
    _tprintf("获取 hid prepared data 失败!\n");
    return;
   }
   ntstatus status = hidp_getcaps(phidppreparseddata,&hidpcaps);
   if (status == hidp_status_success)
   {
    _tprintf("cap信息如下:\n");
    _tprintf("     inputreportbytelength %d\n", hidpcaps.inputreportbytelength);
    _tprintf("     outputreportbytelength %d\n", 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\r",pinputreport[nreadbytes-1]);
    }
    if (pinputreport[nreadbytes-2] == 0x20)  //break the loop when pressing a specific key
    {
     _tprintf("\n");
     break;
    }
    sleep(10);
   }while(hidpcaps.inputreportbytelength == nreadbytes);

   //释放句柄资源
   closehandle(hdevicehandle);
  }
    
  memberindex++;
 }while(bsuccess);

 setupdidestroydeviceinfolist(hdevinfo);
}

本文关键:游戏,手柄,HID
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top