我有一个32位的Windows应用程序(平台目标:x86)。它处理Windows消息以识别按下的键盘键。 我需要将其平台目标更改为:64位平台的任何CPU,但在将其平台类型更改为任何CPU时,它无法正常工作。当我调试时,我发现rawinput.keyboard.Message的值对于两个配置都有差异,例如,当按下控制键时,对于x86为256,对于任何CPU为29
这里,Message是一个uint类型的变量,它是从窗口消息的Lparam值填充的。
我怎样才能使它成为通用的?
CODE:
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;
}
(处理密钥的其余代码) 。
GetRawInputData:
[DllImport("User32.dll")]
extern internal static uint GetRawInputData(IntPtr hRawInput, uint uiCommand, IntPtr pData, ref uint pcbSize, uint cbSizeHeader);
答案 0 :(得分:7)
RAWINPUT
结构使用显式布局,这需要64位的不同字段偏移。
Pinvoke.Net提供了RAWINPUT
的x86 / x64安全实现,您可以使用它:
/// <summary>
/// Contains the raw input from a device.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct RawInput
{
/// <summary>
/// Header for the data.
/// </summary>
public RawInputHeader Header;
public Union Data;
[StructLayout(LayoutKind.Explicit)]
public struct Union
{
/// <summary>
/// Mouse raw input data.
/// </summary>
[FieldOffset(0)]
public RawMouse Mouse;
/// <summary>
/// Keyboard raw input data.
/// </summary>
[FieldOffset(0)]
public RawKeyboard Keyboard;
/// <summary>
/// HID raw input data.
/// </summary>
[FieldOffset(0)]
public RawHID HID;
}
}
答案 1 :(得分:0)
几年后...我建议使用以下库https://github.com/Slion/SharpLibHid