获取鼠标的dpi和回报率可以通过Windows API函数来实现。具体步骤如下:

  1. 引入Windows.h头文件
#include <Windows.h>
  1. 定义MOUSE_CAPABILITIES结构体
typedef struct _MOUSE_CAPABILITIES {
  USHORT  HardwareType;
  USHORT  HardwareSubType;
  USHORT  FirmwareVersion;
  USHORT  DPI;
  ULONG   SampleRate;
  ULONG   NumberOfButtons;
  ULONG   HasHorizontalWheel;
} MOUSE_CAPABILITIES, *PMOUSE_CAPABILITIES;
  1. 定义HIDP_CAPS结构体
typedef struct _HIDP_CAPS {
  USHORT    Usage;
  USHORT    UsagePage;
  USHORT    InputReportByteLength;
  USHORT    OutputReportByteLength;
  USHORT    FeatureReportByteLength;
  USHORT    Reserved[17];
  USHORT    NumberLinkCollectionNodes;
  USHORT    NumberInputButtonCaps;
  USHORT    NumberInputValueCaps;
  USHORT    NumberInputDataIndices;
  USHORT    NumberOutputButtonCaps;
  USHORT    NumberOutputValueCaps;
  USHORT    NumberOutputDataIndices;
  USHORT    NumberFeatureButtonCaps;
  USHORT    NumberFeatureValueCaps;
  USHORT    NumberFeatureDataIndices;
} HIDP_CAPS, *PHIDP_CAPS;
  1. 定义HIDP_VALUE_CAPS结构体
typedef struct _HIDP_VALUE_CAPS {
  USHORT    UsagePage;
  UCHAR     ReportID;
  BOOLEAN   IsAlias;
  USHORT    BitField;
  USHORT    LinkCollection;
  USHORT    LinkUsage;
  USHORT    LinkUsagePage;
  BOOLEAN   IsRange;
  BOOLEAN   IsStringRange;
  BOOLEAN   IsDesignatorRange;
  BOOLEAN   IsAbsolute;
  BOOLEAN   HasNull;
  UCHAR     Reserved;
  USHORT    BitSize;
  USHORT    ReportCount;
  USHORT    Reserved2[5];
  ULONG     UnitsExp;
  ULONG     Units;
  LONG      LogicalMin;
  LONG      LogicalMax;
  LONG      PhysicalMin;
  LONG      PhysicalMax;
  union {
    struct {
      USHORT Usage;
      USHORT Reserved;
    } Range;
    struct {
      USHORT UsageMin;
      USHORT UsageMax;
      USHORT Reserved;
    } RangeWithID;
  };
} HIDP_VALUE_CAPS, *PHIDP_VALUE_CAPS;
  1. 定义HIDP_BUTTON_CAPS结构体
typedef struct _HIDP_BUTTON_CAPS {
  USHORT    UsagePage;
  UCHAR     ReportID;
  BOOLEAN   IsAlias;
  USHORT    BitField;
  USHORT    LinkCollection;
  USHORT    LinkUsage;
  USHORT    LinkUsagePage;
  BOOLEAN   IsRange;
  BOOLEAN   IsStringRange;
  BOOLEAN   IsDesignatorRange;
  BOOLEAN   IsAbsolute;
  UCHAR     Reserved;
  USHORT    UsageMin;
  USHORT    UsageMax;
  USHORT    StringMin;
  USHORT    StringMax;
  USHORT    DesignatorMin;
  USHORT    DesignatorMax;
  USHORT    DataIndexMin;
  USHORT    DataIndexMax;
} HIDP_BUTTON_CAPS, *PHIDP_BUTTON_CAPS;
  1. 定义HIDP_DATA结构体
typedef struct _HIDP_DATA {
  USHORT DataIndex;
  USHORT Reserved;
  union {
    ULONG   RawValue;
    BOOLEAN On;
  };
} HIDP_DATA, *PHIDP_DATA;
  1. 定义HIDP_PREPARSED_DATA类型
typedef PVOID HIDP_PREPARSED_DATA;
  1. 实现获取鼠标dpi和回报率的函数
void GetMouseCapabilities()
{
  // 获取鼠标的HIDP_PREPARSED_DATA
  HANDLE hMouse = GetStdHandle(STD_INPUT_HANDLE);
  PHIDP_PREPARSED_DATA pPreparsedData = NULL;
  HidD_GetPreparsedData(hMouse, &pPreparsedData);

  // 获取鼠标的HIDP_CAPS
  HIDP_CAPS Caps;
  HidP_GetCaps(pPreparsedData, &Caps);

  // 获取鼠标的MOUSE_CAPABILITIES
  MOUSE_CAPABILITIES MouseCaps;
  HidD_GetAttributes(hMouse, (PVOID)&MouseCaps);

  // 获取鼠标的HIDP_BUTTON_CAPS和HIDP_VALUE_CAPS
  PHIDP_BUTTON_CAPS pButtonCaps = new HIDP_BUTTON_CAPS[Caps.NumberInputButtonCaps];
  PHIDP_VALUE_CAPS pValueCaps = new HIDP_VALUE_CAPS[Caps.NumberInputValueCaps];
  ULONG ulButtonCapsLength, ulValueCapsLength;
  HidP_GetButtonCaps(HidP_Input, pButtonCaps, &ulButtonCapsLength, pPreparsedData);
  HidP_GetValueCaps(HidP_Input, pValueCaps, &ulValueCapsLength, pPreparsedData);

  // 获取鼠标的回报率
  ULONG ReportRate = 0;
  for (ULONG i = 0; i < Caps.NumberInputValueCaps; i++) {
    if (pValueCaps[i].UsagePage == 0x84 && pValueCaps[i].Usage == 0x4d) {
      ReportRate = 1000000 / pValueCaps[i].Units;
      break;
    }
  }

  // 获取鼠标的dpi
  USHORT DPI = MouseCaps.DPI;

  // 输出鼠标的dpi和回报率
  printf("Mouse DPI: %d\n", DPI);
  printf("Mouse Report Rate: %d Hz\n", ReportRate);

  // 释放资源
  delete[] pButtonCaps;
  delete[] pValueCaps;
  HidD_FreePreparsedData(pPreparsedData);
}

修改dpi和回报率可以通过Windows API函数来实现。具体步骤如下:

  1. 引入Windows.h头文件
#include <Windows.h>
  1. 定义HIDP_VALUE_CAPS结构体
typedef struct _HIDP_VALUE_CAPS {
  USHORT    UsagePage;
  UCHAR     ReportID;
  BOOLEAN   IsAlias;
  USHORT    BitField;
  USHORT    LinkCollection;
  USHORT    LinkUsage;
  USHORT    LinkUsagePage;
  BOOLEAN   IsRange;
  BOOLEAN   IsStringRange;
  BOOLEAN   IsDesignatorRange;
  BOOLEAN   IsAbsolute;
  BOOLEAN   HasNull;
  UCHAR     Reserved;
  USHORT    BitSize;
  USHORT    ReportCount;
  USHORT    Reserved2[5];
  ULONG     UnitsExp;
  ULONG     Units;
  LONG      LogicalMin;
  LONG      LogicalMax;
  LONG      PhysicalMin;
  LONG      PhysicalMax;
  union {
    struct {
      USHORT Usage;
      USHORT Reserved;
    } Range;
    struct {
      USHORT UsageMin;
      USHORT UsageMax;
      USHORT Reserved;
    } RangeWithID;
  };
} HIDP_VALUE_CAPS, *PHIDP_VALUE_CAPS;
  1. 实现修改鼠标dpi和回报率的函数
void SetMouseDPIAndReportRate(USHORT DPI, ULONG ReportRate)
{
  // 获取鼠标的HIDP_PREPARSED_DATA
  HANDLE hMouse = GetStdHandle(STD_INPUT_HANDLE);
  PHIDP_PREPARSED_DATA pPreparsedData = NULL;
  HidD_GetPreparsedData(hMouse, &pPreparsedData);

  // 获取鼠标的HIDP_CAPS
  HIDP_CAPS Caps;
  HidP_GetCaps(pPreparsedData, &Caps);

  // 获取鼠标的HIDP_VALUE_CAPS
  PHIDP_VALUE_CAPS pValueCaps = new HIDP_VALUE_CAPS[Caps.NumberInputValueCaps];
  ULONG ulValueCapsLength;
  HidP_GetValueCaps(HidP_Input, pValueCaps, &ulValueCapsLength, pPreparsedData);

  // 修改鼠标的dpi
  for (ULONG i = 0; i < Caps.NumberInputValueCaps; i++) {
    if (pValueCaps[i].UsagePage == 0x1 && pValueCaps[i].Usage == 0x48) {
      LONG LogicalMin = pValueCaps[i].LogicalMin;
      LONG LogicalMax = pValueCaps[i].LogicalMax;
      LONG PhysicalMin = pValueCaps[i].PhysicalMin;
      LONG PhysicalMax = pValueCaps[i].PhysicalMax;
      ULONG UnitsExp = pValueCaps[i].UnitsExp;
      ULONG Units = pValueCaps[i].Units;
      USHORT ReportCount = pValueCaps[i].ReportCount;
      pValueCaps[i].PhysicalMax = PhysicalMin + (DPI - LogicalMin) * (PhysicalMax - PhysicalMin) / (LogicalMax - LogicalMin);
      pValueCaps[i].Units = DPI;
      HidP_SetValueCaps(HidP_Input, pValueCaps, ulValueCapsLength, pPreparsedData);
      break;
    }
  }

  // 修改鼠标的回报率
  for (ULONG i = 0; i < Caps.NumberInputValueCaps; i++) {
    if (pValueCaps[i].UsagePage == 0x84 && pValueCaps[i].Usage == 0x4d) {
      pValueCaps[i].Units = 1000000 / ReportRate;
      HidP_SetValueCaps(HidP_Input, pValueCaps, ulValueCapsLength, pPreparsedData);
      break;
    }
  }

  // 释放资源
  delete[] pValueCaps;
  HidD_FreePreparsedData(pPreparsedData);
}
``
获取鼠标的dpi 和回报率 那个hz单位的 这种怎么获取 用C++代码 修改的也写一下

原文地址: https://www.cveoy.top/t/topic/eOuo 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录