Beispiel #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="hdevice"></param>
        public void ProcessRawInput(IntPtr hdevice)
        {
            var size = 0;

            // Determine Size to be allocated
            //var dwSiz = 0;
            //int ret2 = Win32.GetRawInputData(hdevice, DataCommand.RID_INPUT, IntPtr.Zero, ref dwSiz, Marshal.SizeOf(typeof(Rawinputheader)));
            var ret = Win32.GetRawInputData(hdevice, DataCommand.RID_INPUT, IntPtr.Zero, ref size, Marshal.SizeOf(typeof(Rawinputheader)));

            if (ret == -1)
            {
                Console.WriteLine("error");
                return;
            }

            int sizeToAllocate = Math.Max(size, Marshal.SizeOf(typeof(RawInput_Marshalling)));

            IntPtr pData = Marshal.AllocHGlobal(sizeToAllocate);

            try
            {
                //Populate alocated memory
                ret = Win32.GetRawInputData(hdevice, DataCommand.RID_INPUT, pData, ref sizeToAllocate, Marshal.SizeOf(typeof(Rawinputheader)));

                if (ret == -1)
                {
                    throw new System.ComponentModel.Win32Exception();
                }

                Rawinputheader header = (Rawinputheader)Marshal.PtrToStructure(pData, typeof(Rawinputheader));

                //RAWINPUT starts with RAWINPUTHEADER, so we can do this
                RawInputDeviceType type = (RawInputDeviceType)header.dwType;
                switch (type)
                {
                case RawInputDeviceType.RIM_TYPEHID:
                {
                    //As described on page of RAWHID, RAWHID needs special treatement
                    RawInput_Marshalling raw = (RawInput_Marshalling)Marshal.PtrToStructure(pData, typeof(RawInput_Marshalling));

                    //Get marshalling version, it contains information about block size and count
                    RawInput_NonMarshalling raw2 = default(RawInput_NonMarshalling);

                    //Do some copying
                    raw2.header      = raw.header;
                    raw2.hid.dwCount = raw.hid.dwCount;

                    raw2.hid.dwSizHid = raw.hid.dwSizHid;

                    var    numBytes = raw.hid.dwCount * raw.hid.dwSizHid;
                    byte[] data     = new byte[numBytes];

                    // ERROR: Not supported in C#: ReDimStatement

                    //Allocate array
                    //Populate the array
                    IntPtr rawData = new IntPtr(
                        pData.ToInt64() + Marshal.SizeOf(typeof(Rawinputheader)) + Marshal.SizeOf(typeof(Rawhid_Marshalling)));

                    Marshal.Copy(rawData, data, 0, (int)numBytes);

                    // Extract X & Y
                    byte[] xBytes = new byte[4];
                    Buffer.BlockCopy(data, 6, xBytes, 0, 4);
                    byte[] yBytes = new byte[4];
                    Buffer.BlockCopy(data, 10, yBytes, 0, 4);


                    int x = BitConverter.ToInt32(xBytes, 0);
                    int y = BitConverter.ToInt32(yBytes, 0);


                    Console.WriteLine($"X: {x}\tY: {y}");
                    if (TouchActivated != null)
                    {
                        TouchActivated(this, new RawInputEventArg(x, y));
                    }
                    //return raw2;
                    break;
                }

                default:
                {
                    //No additional processing is needed
                    var x = (RawInput_Marshalling)Marshal.PtrToStructure(pData, typeof(RawInput_Marshalling));
                    break;
                }
                }
            }
            finally
            {
                Marshal.FreeHGlobal(pData);
            }
        }
Beispiel #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="hdevice"></param>
        public void ProcessRawInput(IntPtr hdevice)
        {
            var size = 0;

            // Determine Size to be allocated
            //var dwSiz = 0;
            //int ret2 = Win32.GetRawInputData(hdevice, DataCommand.RID_INPUT, IntPtr.Zero, ref dwSiz, Marshal.SizeOf(typeof(Rawinputheader)));
            var ret = Win32.GetRawInputData(hdevice, DataCommand.RID_INPUT, IntPtr.Zero, ref size, Marshal.SizeOf(typeof(Rawinputheader)));

            if (ret == -1)
            {
                Console.WriteLine("error");
                return;
            }

            int sizeToAllocate = Math.Max(size, Marshal.SizeOf(typeof(RawInput_Marshalling)));

            IntPtr pData = Marshal.AllocHGlobal(sizeToAllocate);

            try
            {
                //Populate alocated memory
                ret = Win32.GetRawInputData(hdevice, DataCommand.RID_INPUT, pData, ref sizeToAllocate, Marshal.SizeOf(typeof(Rawinputheader)));

                if (ret == -1)
                {
                    throw new System.ComponentModel.Win32Exception();
                }

                Rawinputheader header = (Rawinputheader)Marshal.PtrToStructure(pData, typeof(Rawinputheader));

                //RAWINPUT starts with RAWINPUTHEADER, so we can do this
                RawInputDeviceType type = (RawInputDeviceType)header.dwType;
                switch (type)
                {
                case RawInputDeviceType.RIM_TYPEHID:
                {
                    //As described on page of RAWHID, RAWHID needs special treatement
                    RawInput_Marshalling raw = (RawInput_Marshalling)Marshal.PtrToStructure(pData, typeof(RawInput_Marshalling));

                    //Do some copying
                    //raw2.header = raw.header;

                    ReadDeviceInfo(raw.header.hDevice);

                    var    numBytes = raw.header.dwSize - (Marshal.SizeOf(typeof(Rawinputheader)) + Marshal.SizeOf(typeof(Rawhid_Marshalling)));     // * raw.hid.dwSizHid;
                    byte[] data     = new byte[numBytes];

                    // ERROR: Not supported in C#: ReDimStatement

                    //Allocate array
                    //Populate the array

                    // new pointer - offset by header sizes
                    IntPtr rawData = new IntPtr(
                        pData.ToInt64() + Marshal.SizeOf(typeof(Rawinputheader)) + Marshal.SizeOf(typeof(Rawhid_Marshalling)));

                    Marshal.Copy(rawData, data, 0, (int)numBytes);

                    if (numBytes > 0)
                    {
                        for (int i = 0; i < numBytes; ++i)
                        {
                            Console.Write(String.Format("{0:X2}", data[i]));
                        }
                        Console.WriteLine();

                        TouchInfo ti = new TouchInfo(data);
                        //Console.WriteLine(ti.X() + " " + ti.Y());

                        if (TouchActivated != null)
                        {
                            TouchActivated(this, ti);
                        }
                    }
                    else
                    {
                        Console.WriteLine("no bytes...");
                    }
                    //return raw2;
                    break;
                }

                default:
                {
                    //No additional processing is needed
                    var x = (RawInput_Marshalling)Marshal.PtrToStructure(pData, typeof(RawInput_Marshalling));
                    break;
                }
                }
            }
            finally
            {
                Marshal.FreeHGlobal(pData);
            }
        }