1
我寫程序sendInput到遊戲和桌面應用程序,而不是採取從keyboard.This輸入結果:SendInput無法在Windows 8(64位),但工作在Windows 7(32位)工作
的Windows 7 (32位):在遊戲和桌面應用程序中非常完美。
Windows 8(64位):DID不能在遊戲中工作,但在notepad.exe中工作。
so爲什麼它沒有與勝8一起工作?
下面是代碼:
[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]));
}
什麼錯誤代碼,你得到它失敗時有什麼不同?另外,請參閱http://stackoverflow.com/q/6830651/56778,看起來相關。 – 2013-03-13 19:20:58
沒有錯誤,它只是沒有按預期在Windows 8中的遊戲按鍵,而在Windows 7中的相同遊戲中工作 – 2013-03-13 19:22:41
我沒有看到任何錯誤。黑客遊戲永遠不是問題。也許它現在使用原始輸入,也許鍵盤焦點不在你期望的位置。難以猜測。 – 2013-03-13 19:53:12