SendInput不能在Windows 8(64位)中工作,但可以在Windows 7(32位)中工作
本文关键字:Windows 工作 32位 但可以 64位 不能 SendInput | 更新日期: 2023-09-27 18:03:58
我编写了一个程序,将input发送到游戏和桌面应用程序,而不是从键盘输入。结果如下:
Windows 7(32位):在游戏和桌面应用程序中工作完美。
Windows 8(64位):不能在游戏中工作,但可以在notepad.exe中工作。
那么为什么它不能在win8中工作呢?
代码如下:
[StructLayout(LayoutKind.Sequential)]
struct MOUSEINPUT
{
public int dx;
public int dy;
public uint mouseData;
public uint dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
struct KEYBDINPUT
{
public ushort wVk;
public ushort wScan;
public uint dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
struct HARDWAREINPUT
{
public int uMsg;
public short wParamL;
public short wParamH;
}
[StructLayout(LayoutKind.Explicit)]
struct MouseKeybdHardwareInputUnion
{
[FieldOffset(0)]
public MOUSEINPUT mi;
[FieldOffset(0)]
public KEYBDINPUT ki;
[FieldOffset(0)]
public HARDWAREINPUT hi;
}
[StructLayout(LayoutKind.Sequential)]
struct INPUT
{
public uint type;
public MouseKeybdHardwareInputUnion mkhi;
}
enum SendInputFlags
{
KEYEVENTF_EXTENDEDKEY = 0x0001,
KEYEVENTF_KEYUP = 0x0002,
KEYEVENTF_UNICODE = 0x0004,
KEYEVENTF_SCANCODE = 0x0008,
}
[DllImport("user32.dll")]
static extern UInt32 SendInput(UInt32 nInputs, [MarshalAs(UnmanagedType.LPArray, SizeConst = 1)] INPUT[] pInputs, Int32 cbSize);
public static void StrokeKey(ushort codeOfKey)
{
INPUT[] InputData = new INPUT[1];
ushort ScanCode = codeOfKey;
InputData[0].type = 1;
InputData[0].mkhi.ki.wScan = (ushort)ScanCode;
InputData[0].mkhi.ki.dwFlags = (uint)SendInputFlags.KEYEVENTF_SCANCODE;
if (SendInput(1, InputData, Marshal.SizeOf(InputData[0])) == 0)
{
String blad = "SendInput failed with code: " + Marshal.GetLastWin32Error().ToString();
System.Diagnostics.Debug.WriteLine(blad);
}
}
public static void ReleaseKey(ushort codeOfKey)
{
INPUT[] InputData = new INPUT[1];
ushort ScanCode = codeOfKey;
InputData[0].type = 1;
InputData[0].mkhi.ki.wScan = (ushort)ScanCode;
InputData[0].mkhi.ki.dwFlags = (uint)SendInputFlags.KEYEVENTF_KEYUP | (uint)SendInputFlags.KEYEVENTF_SCANCODE;
InputData[0].mkhi.ki.time = 0;
InputData[0].mkhi.ki.dwExtraInfo = IntPtr.Zero;
SendInput(1, InputData, Marshal.SizeOf(InputData[0]));
}
最有可能的是指针大小在32位时不同,它的大小为4,在64位时不同,它的大小为8