"INPUT" is innaccesible due to its protection level

731 views Asked by At

Im trying to build a small app that would send input to a game. After searching for a while, I found many answers that suggest SendInput should be used.

Below there is the code that I'm working on (basically doing a lots of tests to see what works, hence the messiness). I would suggest you ignore most of the code, the line that interests me , or should I say that causes the build problem is:

public static extern uint SendInput(
    uint nInputs,[MarshalAs(UnmanagedType.LPArray), In] INPUT[] pInputs, int cbSize);

This gives me the error :

'INPUT' is inaccessible due to its protection level.

I am new to C# (and Windows programming), so I cannot really figure out what I should do to fix that.

Any help would be appreciated.

Thanks in advance.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using WindowsInput;

namespace WindowsFormsApplication1
{
    [Flags]
    public enum KeyFlag
    {
        KeyDown = 0x0000,
        KeyUp = 0x0002,
        Scancode = 0x0008
    }


    internal static class VirtualKeyboard
    {

        public static void KeyDown(System.Windows.Forms.Keys key)
        {

            Console.WriteLine("Sending keybdevent: " + (byte)key);
            //keybd_event((byte)key, 0, 0, 0);
            /*INPUT[] InputData = new INPUT[2];
            Key ScanCode = Microsoft.DirectX.DirectInput.Key.W;

            InputData[0].type = 1; //INPUT_KEYBOARD
            InputData[0].wScan = (ushort)ScanCode;
            InputData[0].dwFlags = (uint)SendInputFlags.KEYEVENTF_SCANCODE;

            InputData[1].type = 1; //INPUT_KEYBOARD
            InputData[1].wScan = (ushort)ScanCode;
            InputData[1].dwFlags = (uint)(SendInputFlags.KEYEVENTF_KEYUP | SendInputFlags.KEYEVENTF_UNICODE);

            // send keydown
            if (SendInput(2, InputData, Marshal.SizeOf(InputData[1])) == 0)
            {
                System.Diagnostics.Debug.WriteLine("SendInput failed with code: " +
                Marshal.GetLastWin32Error().ToString());
            }*/
            //InputSimulator.SimulateKeyDown(VirtualKeyCode.VK_E);

            Console.WriteLine("Exit.....");
        }
        [DllImport("user32.dll", SetLastError = true)]
        public static extern uint SendInput(uint nInputs, [MarshalAs(UnmanagedType.LPArray), In] INPUT[] pInputs, int cbSize);
        public static void SendKey(short keyCode, KeyFlag keyFlag)
        {
            INPUT[] InputData = new INPUT[1];

            InputData[0].type = 1;
            InputData[0].ki.wScan = keyCode; // 0x14 = T for example
            InputData[0].ki.dwFlags = (int)keyFlag;
            InputData[0].ki.time = 0;
            InputData[0].ki.dwExtraInfo = IntPtr.Zero;

            SendInput(1, InputData, Marshal.SizeOf(typeof(INPUT)));
        }

        public static void PressKey(short key)
        {
            SendKey(key, KeyFlag.KeyDown | KeyFlag.Scancode);
            SendKey(key, KeyFlag.KeyUp | KeyFlag.Scancode);
        }

        public static void KeyUp(System.Windows.Forms.Keys key)
        {
            //keybd_event((byte)key, 0, 0x7F, 0);
        }
    }
}
2

There are 2 answers

8
Cyral On BEST ANSWER

The INPUT struct must be a higher access level, such as public so other classes can access it. See access modifiers.

public struct INPUT 
{  
   ...
}
0
Salah Akbari On

In C# you have to explicitly mark struct members as public. In c++ they are automatically public. So you must mark INPUT as public.