0

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");
            }
        }
    }
}
dandan
  • 509
  • 3
  • 8
  • 21
  • 2
    `since my code is different` - well, change it back to what it was in the [post](https://stackoverflow.com/a/6833311/11683) you are linking to. You have removed the `MouseKeybdHardwareInputUnion` struct. It's there [for a reason](https://devblogs.microsoft.com/oldnewthing/20090813-00/?p=17113). – GSerg Jun 14 '21 at 09:20
  • No special reason. I guess I copied this a while a go from somewhere and since I am not using the mouse inputs I did not miss them. It was working very well for a long time in x32 but now, in x64 it stopped working – dandan Jun 14 '21 at 09:22
  • I did not ask why you removed the struct. I said that it was there [for a reason](https://devblogs.microsoft.com/oldnewthing/20090813-00/?p=17113), so you should put it back regardless of why you removed it. – GSerg Jun 14 '21 at 09:34
  • Thanks, I added it. Will this solve the x64 problem? – dandan Jun 14 '21 at 09:35

0 Answers0