我有一个 32 位 Windows 应用程序(平台目标:x86)。它处理 Windows 消息以识别按下的键盘键。我需要将其平台目标更改为:64 位平台的任何 CPU,但将其平台类型更改为任何 CPU 时,它不起作用。当我调试时,我发现两种配置的 rawinput.keyboard.Message 值存在差异,例如,当按下控制键时,x86 为 256,任何 CPU 为 29
这里,Message 是一个 uint 类型的变量,由窗口消息的 Lparam 值填充。
我怎样才能使它通用?
代码:
private void ProcessInputCommand(Message message)
{
uint dwSize = 0;
// First call to GetRawInputData sets the value of dwSize,
// which can then be used to allocate the appropriate amount of memory,
// storing the pointer in "buffer".
UnsafeNativeMethods.GetRawInputData(message.LParam,
UnsafeNativeMethods.RID_INPUT, IntPtr.Zero,
ref dwSize,
(uint)Marshal.SizeOf(typeof(RAWINPUTHEADER)));
IntPtr buffer = Marshal.AllocHGlobal((int)dwSize);
try
{
// Check that buffer points to something, and if so,
// call GetRawInputData again to fill the allocated memory
// with information about the input
if (buffer != IntPtr.Zero &&
UnsafeNativeMethods.GetRawInputData(message.LParam,
UnsafeNativeMethods.RID_INPUT,
buffer,
ref dwSize,
(uint)Marshal.SizeOf(typeof(RAWINPUTHEADER))) == dwSize)
{
// Store the message information in "raw", then check
// that the input comes from a keyboard device before
// processing it to raise an appropriate KeyPressed event.
RAWINPUT raw = (RAWINPUT)Marshal.PtrToStructure(buffer, typeof(RAWINPUT));
if (raw.header.dwType == UnsafeNativeMethods.RIM_TYPEKEYBOARD)
{
// Filter for Key Down events and then retrieve information
// about the keystroke
if (raw.keyboard.Message == UnsafeNativeMethods.WM_KEYDOWN || raw.keyboard.Message == UnsafeNativeMethods.WM_SYSKEYDOWN)
{
ushort key = raw.keyboard.VKey;
}
(处理密钥的其余代码)。.
获取原始输入数据:
[DllImport("User32.dll")]
extern internal static uint GetRawInputData(IntPtr hRawInput, uint uiCommand, IntPtr pData, ref uint pcbSize, uint cbSizeHeader);