示例#1
0
 public RawInputEventArg(JoystickEvent arg)
 {
     JoystickEvent = arg;
 }
示例#2
0
        public void EnumerateDevices()
        {
            lock (_padLock)
            {
                _deviceList.Clear();

                var keyboardNumber = 0;

                var globalDevice = new JoystickEvent
                {
                    DeviceName   = "Global Joystick",
                    DeviceHandle = IntPtr.Zero,
                    DeviceType   = Win32.GetDeviceType(DeviceType.RimTypeHid),
                    Name         = "Fake Joystick?",
                    Source       = keyboardNumber++.ToString(CultureInfo.InvariantCulture)
                };

                _deviceList.Add(globalDevice.DeviceHandle, globalDevice);

                var  numberOfDevices = 0;
                uint deviceCount     = 0;
                var  dwSize          = (Marshal.SizeOf(typeof(Rawinputdevicelist)));

                if (Win32.GetRawInputDeviceList(IntPtr.Zero, ref deviceCount, (uint)dwSize) == 0)
                {
                    var pRawInputDeviceList = Marshal.AllocHGlobal((int)(dwSize * deviceCount));
                    Win32.GetRawInputDeviceList(pRawInputDeviceList, ref deviceCount, (uint)dwSize);

                    for (var i = 0; i < deviceCount; i++)
                    {
                        uint pcbSize = 0;

                        // On Window 8 64bit when compiling against .Net > 3.5 using .ToInt32 you will generate an arithmetic overflow. Leave as it is for 32bit/64bit applications
                        var rid = (Rawinputdevicelist)Marshal.PtrToStructure(new IntPtr((pRawInputDeviceList.ToInt64() + (dwSize * i))), typeof(Rawinputdevicelist));

                        Win32.GetRawInputDeviceInfo(rid.hDevice, RawInputDeviceInfo.RIDI_DEVICENAME, IntPtr.Zero, ref pcbSize);

                        if (pcbSize <= 0)
                        {
                            continue;
                        }

                        var pData = Marshal.AllocHGlobal((int)pcbSize);
                        Win32.GetRawInputDeviceInfo(rid.hDevice, RawInputDeviceInfo.RIDI_DEVICENAME, pData, ref pcbSize);
                        var deviceName = Marshal.PtrToStringAnsi(pData);

                        // Debug.WriteLine(rid.dwType);

                        if (rid.dwType == DeviceType.RimTypeHid)
                        {
                            var deviceDesc = Win32.GetDeviceDescription(deviceName);

                            var dInfo = new JoystickEvent
                            {
                                DeviceName   = Marshal.PtrToStringAnsi(pData),
                                DeviceHandle = rid.hDevice,
                                DeviceType   = Win32.GetDeviceType(rid.dwType),
                                Name         = deviceDesc,
                                Source       = keyboardNumber++.ToString(CultureInfo.InvariantCulture)
                            };

                            if (!_deviceList.ContainsKey(rid.hDevice))
                            {
                                numberOfDevices++;
                                _deviceList.Add(rid.hDevice, dInfo);
                            }
                        }

                        Marshal.FreeHGlobal(pData);
                    }

                    Marshal.FreeHGlobal(pRawInputDeviceList);

                    NumberOfKeyboards = numberOfDevices;
                    Debug.WriteLine("EnumerateDevices() found {0} Joystick(s)", NumberOfKeyboards);
                    return;
                }
            }

            throw new Win32Exception(Marshal.GetLastWin32Error());
        }
示例#3
0
文件: Win32.cs 项目: Jamesits/Stickey
        public static void DeviceAudit()
        {
            var file = new FileStream("DeviceAudit.txt", FileMode.Create, FileAccess.Write);
            var sw   = new StreamWriter(file);

            var  keyboardNumber = 0;
            uint deviceCount    = 0;
            var  dwSize         = (Marshal.SizeOf(typeof(Rawinputdevicelist)));

            if (GetRawInputDeviceList(IntPtr.Zero, ref deviceCount, (uint)dwSize) == 0)
            {
                var pRawInputDeviceList = Marshal.AllocHGlobal((int)(dwSize * deviceCount));
                GetRawInputDeviceList(pRawInputDeviceList, ref deviceCount, (uint)dwSize);

                for (var i = 0; i < deviceCount; i++)
                {
                    uint pcbSize = 0;

                    // On Window 8 64bit when compiling against .Net > 3.5 using .ToInt32 you will generate an arithmetic overflow. Leave as it is for 32bit/64bit applications
                    var rid = (Rawinputdevicelist)Marshal.PtrToStructure(new IntPtr((pRawInputDeviceList.ToInt64() + (dwSize * i))), typeof(Rawinputdevicelist));

                    GetRawInputDeviceInfo(rid.hDevice, RawInputDeviceInfo.RIDI_DEVICENAME, IntPtr.Zero, ref pcbSize);

                    if (pcbSize <= 0)
                    {
                        sw.WriteLine("pcbSize: " + pcbSize);
                        sw.WriteLine(Marshal.GetLastWin32Error());
                        return;
                    }

                    var size = (uint)Marshal.SizeOf(typeof(DeviceInfo));
                    var di   = new DeviceInfo {
                        Size = Marshal.SizeOf(typeof(DeviceInfo))
                    };

                    if (GetRawInputDeviceInfo(rid.hDevice, (uint)RawInputDeviceInfo.RIDI_DEVICEINFO, ref di, ref size) <= 0)
                    {
                        sw.WriteLine(Marshal.GetLastWin32Error());
                        return;
                    }

                    var pData = Marshal.AllocHGlobal((int)pcbSize);
                    GetRawInputDeviceInfo(rid.hDevice, RawInputDeviceInfo.RIDI_DEVICENAME, pData, ref pcbSize);
                    var deviceName = Marshal.PtrToStringAnsi(pData);

                    if (rid.dwType == DeviceType.RimTypekeyboard || rid.dwType == DeviceType.RimTypeHid)
                    {
                        var deviceDesc = GetDeviceDescription(deviceName);

                        var dInfo = new JoystickEvent
                        {
                            DeviceName   = Marshal.PtrToStringAnsi(pData),
                            DeviceHandle = rid.hDevice,
                            DeviceType   = GetDeviceType(rid.dwType),
                            Name         = deviceDesc,
                            Source       = keyboardNumber++.ToString(CultureInfo.InvariantCulture)
                        };

                        sw.WriteLine(dInfo.ToString());
                        sw.WriteLine(di.ToString());
                        sw.WriteLine(di.KeyboardInfo.ToString());
                        sw.WriteLine(di.HIDInfo.ToString());
                        //sw.WriteLine(di.MouseInfo.ToString());
                        sw.WriteLine("=========================================================================================================");
                    }

                    Marshal.FreeHGlobal(pData);
                }

                Marshal.FreeHGlobal(pRawInputDeviceList);

                sw.Flush();
                sw.Close();
                file.Close();

                return;
            }

            throw new Win32Exception(Marshal.GetLastWin32Error());
        }
示例#4
0
        public void ProcessRawInput(IntPtr hdevice)
        {
            // Debug.WriteLine(_rawBuffer.data.keyboard.ToString());
            // Debug.WriteLine(_rawBuffer.data.hid.ToString());
            // Debug.WriteLine(_rawBuffer.header.ToString());

            if (_deviceList.Count == 0)
            {
                return;
            }

            var dwSize = 0;

            Win32.GetRawInputData(hdevice, DataCommand.RID_INPUT, IntPtr.Zero, ref dwSize, Marshal.SizeOf(typeof(Rawinputheader)));

            var unmanagedPointer = Marshal.AllocHGlobal(dwSize);

            if (dwSize != Win32.GetRawInputData(hdevice, DataCommand.RID_INPUT, unmanagedPointer, ref dwSize, Marshal.SizeOf(typeof(Rawinputheader))))
            {
                Debug.WriteLine("Error getting the rawinput buffer");
                return;
            }

            var inputData = (InputData)Marshal.PtrToStructure(unmanagedPointer, typeof(InputData));

            var size = (int)(inputData.data.hid.dwCount * inputData.data.hid.dwSizHid);

            if (_deviceList.ContainsKey(_rawBuffer.header.hDevice))
            {
                lock (_padLock)
                {
                    Debug.WriteLine(_deviceList[_rawBuffer.header.hDevice]);
                }

                byte[] managedArray = new byte[dwSize];
                Marshal.Copy(unmanagedPointer, managedArray, 0, dwSize);

                var sb = new StringBuilder();
                sb.AppendFormat("Size: {0}/{1} ", dwSize, size);
                for (int i = Marshal.SizeOf(typeof(Rawinputheader)) + Marshal.SizeOf(typeof(byte)) * 2; i < dwSize; ++i)
                {
                    sb.AppendFormat("{0,3:X}", managedArray[i]);
                }

                Debug.WriteLine(sb);

                JoystickEvent e = new JoystickEvent()
                {
                    MainButtons      = managedArray[managedArray.Length - 4],
                    DirectionButtons = managedArray[managedArray.Length - 3]
                };

                // Direction buttons
                var directionBtn = (byte)(e.DirectionButtons & 0x3c);
                switch (directionBtn)
                {
                case 0x04:
                    e.Up = true;
                    break;

                case 0x0c:
                    e.Right = true;
                    break;

                case 0x14:
                    e.Down = true;
                    break;

                case 0x1c:
                    e.Left = true;
                    break;

                case 0x18:
                    e.Down = true;
                    e.Left = true;
                    break;

                case 0x20:
                    e.Up   = true;
                    e.Left = true;
                    break;

                case 0x10:
                    e.Down  = true;
                    e.Right = true;
                    break;

                case 0x08:
                    e.Up    = true;
                    e.Right = true;
                    break;
                }

                // L3 & R3
                if ((e.DirectionButtons & 0x01) != 0)
                {
                    e.L3 = true;
                }

                if ((e.DirectionButtons & 0x02) != 0)
                {
                    e.R3 = true;
                }

                // normal buttons
                if ((e.MainButtons & (byte)XInputJoystickButtons.A) != 0)
                {
                    e.A = true;
                }
                if ((e.MainButtons & (byte)XInputJoystickButtons.B) != 0)
                {
                    e.B = true;
                }
                if ((e.MainButtons & (byte)XInputJoystickButtons.X) != 0)
                {
                    e.X = true;
                }
                if ((e.MainButtons & (byte)XInputJoystickButtons.Y) != 0)
                {
                    e.Y = true;
                }
                if ((e.MainButtons & (byte)XInputJoystickButtons.LB) != 0)
                {
                    e.LB = true;
                }
                if ((e.MainButtons & (byte)XInputJoystickButtons.RB) != 0)
                {
                    e.RB = true;
                }
                if ((e.MainButtons & (byte)XInputJoystickButtons.View) != 0)
                {
                    e.View = true;
                }
                if ((e.MainButtons & (byte)XInputJoystickButtons.Menu) != 0)
                {
                    e.Menu = true;
                }

                JoystickPressed?.Invoke(this, new RawInputEventArg(e));
            }
            else
            {
                Debug.WriteLine("Handle: {0} was not in the device list.", _rawBuffer.header.hDevice);
                return;
            }

            Marshal.FreeHGlobal(unmanagedPointer);
        }