I create a plugin for a game (Falcon BMS) which, amongst others needs to 'press' the keyboard. It was all working well on x32 but now I need to port it to x64 since the main app is miving to x64.
I tried to follow this: enter link description here But since my code is different it does not work. I get too many errors for mismatch types. What alteration in my code below do I need to do in order to make it compatible for both x32 and x64? I assume these are minor changes but I don't know which... Thanks.
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace JanJanBMS_VaPlugin.keyStrokes
{
public class SendKeyStrokesDxBck
{
public enum INPUT_TYPE : uint
{
INPUT_MOUSE = 0,
INPUT_KEYBOARD = 1,
INPUT_HARDWARE = 2
}
[StructLayout(LayoutKind.Sequential)]
struct MOUSEINPUT
{
public int dx;
public int dy;
public int mouseData;
public int dwFlags;
public int time;
public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
struct KEYBDINPUT
{
public short wVk;
public short wScan;
public KEYEVENTF dwFlags;
public int time;
public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
struct HARDWAREINPUT
{
public int uMsg;
public short wParamL;
public short wParamH;
}
[StructLayout(LayoutKind.Explicit)]
struct INPUT
{
[FieldOffset(0)] public INPUT_TYPE type;
[FieldOffset(4)] public MOUSEINPUT mi;
[FieldOffset(4)] public KEYBDINPUT ki;
[FieldOffset(4)] public HARDWAREINPUT hi;
}
public enum KEYEVENTF : uint
{
EXTENDEDKEY = 0x0001,
KEYUP = 0x0002,
UNICODE = 0x0004,
SCANCODE = 0x0008
}
[DllImport("user32.dll")]
static extern UInt32 SendInput(UInt32 nInputs, [MarshalAs(UnmanagedType.LPArray, SizeConst = 1)]
INPUT[] pInputs, Int32 cbSize);
//[DllImport("User32.dll")]
//static extern int SetForegroundWindow(IntPtr point);
public static void SendMultiDxKey(dynamic vaProxy, List<short> keyCodes, bool press, bool release, string application = "Falcon BMS")
{
//make sure modifier keys are send first!!
/*var sleepBefore = 100; //ms
var p = Process.GetProcessesByName(application).FirstOrDefault();
var h = p.MainWindowHandle;
SetForegroundWindow(h);
Thread.Sleep(sleepBefore);*/ //need to wait for app to be ready to receive keys
foreach (var scanCode in keyCodes)
{
if (!press && !release)
{
return;
}
var numInputs = press && release ? 2 : 1;
var inputs = new INPUT[numInputs];
var curInput = 0;
if (press)
{
var input = new INPUT
{
ki = new KEYBDINPUT
{
wScan = scanCode,
time = 0,
dwFlags = KEYEVENTF.SCANCODE
}
};
if ((scanCode & 0x80) > 0)
{
input.ki.dwFlags |= KEYEVENTF.EXTENDEDKEY;
}
input.type = INPUT_TYPE.INPUT_KEYBOARD;
inputs[curInput] = input;
curInput++;
}
if (release)
{
var input = new INPUT
{
ki = new KEYBDINPUT
{
wScan = scanCode,
time = 0,
dwFlags = KEYEVENTF.KEYUP | KEYEVENTF.SCANCODE
}
};
if ((scanCode & 0x80) > 0)
{
input.ki.dwFlags |= KEYEVENTF.EXTENDEDKEY;
}
input.type = INPUT_TYPE.INPUT_KEYBOARD;
inputs[curInput] = input;
}
SendInput((uint)numInputs, inputs, Marshal.SizeOf(typeof(INPUT)));
//vaProxy.WriteToLog(string.Format("Send 0x" + scanCode.ToString("X") + "=>" ), "Orange");
}
}
}
}