Ejemplo n.º 1
0
 public InputEventArg(KeyPressEvent arg)
 {
     KeyPressEvent = arg;
 }
Ejemplo n.º 2
0
        public bool ProcessRawInput(IntPtr hdevice, out KeyPressEvent keyPressEvent)
        {
            //Debug.WriteLine(_rawBuffer.data.keyboard.ToString());
            //Debug.WriteLine(_rawBuffer.data.hid.ToString());
            //Debug.WriteLine(_rawBuffer.header.ToString());

            keyPressEvent = new KeyPressEvent();

            keyPressEvent.Filter = false;

            if (_deviceList.Count == 0)
            {
                return(false);
            }
            if (CaptureOnlyIfTopMostWindow && !Win32.InputInForeground(_rawBuffer.header.wParam))
            {
                return(false);
            }

            var dwSize = 0;

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

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

            int virtualKey = _rawBuffer.data.keyboard.VKey;
            int makeCode   = _rawBuffer.data.keyboard.Makecode;
            int flags      = _rawBuffer.data.keyboard.Flags;

            if (virtualKey == Win32.KEYBOARD_OVERRUN_MAKE_CODE)
            {
                return(false);
            }

            var isE0BitSet = ((flags & Win32.RI_KEY_E0) != 0);

            if (_deviceList.ContainsKey(_rawBuffer.header.hDevice))
            {
                lock (_padLock)
                {
                    keyPressEvent = _deviceList[_rawBuffer.header.hDevice];
                }
            }
            else
            {
                Debug.WriteLine("Handle: {0} was not in the device list.", _rawBuffer.header.hDevice);
                return(false);
            }

            var isBreakBitSet = ((flags & Win32.RI_KEY_BREAK) != 0);

            keyPressEvent.KeyPressState = isBreakBitSet ? "BREAK" : "MAKE";
            keyPressEvent.Message       = _rawBuffer.data.keyboard.Message;
            keyPressEvent.VKeyName      = KeyMapper.GetKeyName(VirtualKeyCorrection(virtualKey, isE0BitSet, makeCode)).ToUpper();
            keyPressEvent.VKey          = virtualKey;

            if (KeyPressed != null)
            {
                KeyPressed(this, new InputEventArg(keyPressEvent));
            }

            return(true);
        }
Ejemplo n.º 3
0
        public void EnumerateDevices()
        {
            lock (_padLock)
            {
                _deviceList.Clear();

                var keyboardNumber = 0;

                var globalDevice = new KeyPressEvent
                {
                    DeviceName   = "Global Keyboard",
                    DeviceHandle = IntPtr.Zero,
                    DeviceType   = Win32.GetDeviceType(DeviceType.RimTypekeyboard),
                    Name         = "Fake Keyboard. Some keys (ZOOM, MUTE, VOLUMEUP, VOLUMEDOWN) are sent to rawinput with a handle of zero.",
                    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);

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

                            var dInfo = new KeyPressEvent
                            {
                                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} Keyboard(s)", NumberOfKeyboards);
                    return;
                }
            }

            throw new Win32Exception(Marshal.GetLastWin32Error());
        }
Ejemplo n.º 4
0
        //private void initTouch()
        //{

        //    bool res = false;
        //    Rawinputdevicelist[] pRawInputDeviceList = null;
        //    uint puiNumDevices = 0;
        //    uint returnCode = Win32.GetRawInputDeviceList(pRawInputDeviceList, ref puiNumDevices, (uint)Marshal.SizeOf(new Rawinputdevicelist()));
        //    res = (0xFFFFFFFF != returnCode);
        //    if (res)
        //    {
        //        //alloc array
        //        pRawInputDeviceList = new Rawinputdevicelist[puiNumDevices];

        //        //get devices
        //        returnCode = Win32.GetRawInputDeviceList(pRawInputDeviceList, ref puiNumDevices, (uint)Marshal.SizeOf((typeof(Rawinputdevicelist))));
        //        res = (0xFFFFFFFF != returnCode);
        //        if (res)
        //        {
        //            //look for the touchscreen.
        //            bool foundTouchScreen = false;
        //            foreach (Rawinputdevicelist rawInputDevice in pRawInputDeviceList)
        //            {


        //                uint structsize = (uint)Marshal.SizeOf(typeof(DeviceInfo));
        //                DeviceInfo di = new DeviceInfo();
        //                di.Size = (int)structsize;
        //                IntPtr pData = Marshal.AllocHGlobal((int)structsize);
        //                returnCode = Win32.GetRawInputDeviceInfo(rawInputDevice.hDevice, RawInputDeviceInfoType.RIDI_DEVICEINFO, pData, ref structsize);
        //                if (0xFFFFFFF != returnCode && 0 != returnCode)
        //                {
        //                    di = (DeviceInfo)Marshal.PtrToStructure(pData, typeof(DeviceInfo));
        //                    //Console.WriteLine("di.dwType = " + Enum.GetName(typeof(RawInputDeviceType), di.dwType));

        //                    uint comparor = (uint)di.Type;

        //                    if (comparor == (uint)RawInputDeviceType.RIM_TYPEHID)
        //                    {
        //                        /* Console.WriteLine("di.hid.dwVendorId = " + di.hid.dwVendorId);
        //                           Console.WriteLine("di.hid.dwProductId = " + di.hid.dwProductId);
        //                           Console.WriteLine("di.hid.dwVersionNumber = " + di.hid.dwVersionNumber);
        //                           Console.WriteLine("di.hid.usUsagePage = " + di.hid.usUsagePage);
        //                           Console.WriteLine("di.hid.usUsage = " + di.hid.usUsage);*/
        //                        if (0x0D == di.HIDInfo.UsagePage && 0x04 == di.HIDInfo.Usage)
        //                        {
        //                            VendorID = di.HIDInfo.VendorID;
        //                            ProductID = di.HIDInfo.ProductID;
        //                            foundTouchScreen = true;
        //                        }
        //                    }
        //                }
        //            }
        //        }
        //    }
        //}
                    //        if (foundTouchScreen)
                    //        {
                    //            RAWINPUTDEVICE[] rawInputDevicesToMonitor = new RAWINPUTDEVICE[1];
                    //            RAWINPUTDEVICE device = new RAWINPUTDEVICE();
                    //            device.dwFlags = INPUTSINK | DEVNOTIFY;

                    //            //RIDEV_INPUTSINK;
                    //            device.hwndTarget = Process.GetCurrentProcess().MainWindowHandle;
                    //            device.usUsage = di.hid.usUsage;
                    //            device.usUsagePage = di.hid.usUsagePage;
                    //            rawInputDevicesToMonitor[0] = device;

                    //            if (!RegisterRawInputDevices(rawInputDevicesToMonitor, (uint)1, (uint)Marshal.SizeOf(new RAWINPUTDEVICE())))
                    //            {
                    //                Console.WriteLine("Registration of device --> NOK (error: " + Marshal.GetLastWin32Error() + ")");
                    //                RAWINPUTDEVICE[] pRegisteredRawInputDeviceList = null;
                    //                uint puiNumRegDevices = 0;
                    //                returnCode = GetRegisteredRawInputDevices(pRegisteredRawInputDeviceList, ref puiNumRegDevices, (uint)Marshal.SizeOf(new RAWINPUTDEVICE()));
                    //                res = (0xFFFFFFFF != returnCode);
                    //                if (res)
                    //                {
                    //                    //alloc array
                    //                    pRegisteredRawInputDeviceList = new RAWINPUTDEVICE[puiNumRegDevices];

                    //                    //get devices
                    //                    returnCode = GetRegisteredRawInputDevices(pRegisteredRawInputDeviceList, ref puiNumRegDevices, (uint)Marshal.SizeOf((typeof(RAWINPUTDEVICE))));

                    //                    Console.WriteLine("Registered devices nb : " + returnCode);
                    //                    //}
                    //                }
                    //                break;
                    //            }
                    //        }
                    //    }
                    //}


        public void EnumerateDevices()
		{
			lock (_padLock)
			{
				_deviceList.Clear();

				var keyboardNumber = 0;

				var globalDevice = new KeyPressEvent
				{
					DeviceName = "Global Keyboard",
					DeviceHandle = IntPtr.Zero,
					DeviceType = Win32.GetDeviceType(DeviceType.RimTypekeyboard),
					Name = "Fake Keyboard. Some keys (ZOOM, MUTE, VOLUMEUP, VOLUMEDOWN) are sent to rawinput with a handle of zero.",
					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);

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

                            var dInfo = new KeyPressEvent
                            {
                                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} Keyboard(s)", NumberOfKeyboards);
                    return;
                }
                return;
			}

			throw new Win32Exception(Marshal.GetLastWin32Error());
		}
Ejemplo n.º 5
0
        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 KeyPressEvent
                        {
                            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());
        }
Ejemplo n.º 6
0
        public bool ProcessRawInput(IntPtr hdevice, out KeyPressEvent keyPressEvent)
        {
            //Debug.WriteLine(_rawBuffer.data.keyboard.ToString());
            //Debug.WriteLine(_rawBuffer.data.hid.ToString());
            //Debug.WriteLine(_rawBuffer.header.ToString());

            keyPressEvent = new KeyPressEvent();

            keyPressEvent.Filter = false;

            if (_deviceList.Count == 0) return false;
            if (CaptureOnlyIfTopMostWindow && !Win32.InputInForeground(_rawBuffer.header.wParam)) return false;

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

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

            int virtualKey = _rawBuffer.data.keyboard.VKey;
            int makeCode = _rawBuffer.data.keyboard.Makecode;
            int flags = _rawBuffer.data.keyboard.Flags;

            if (virtualKey == Win32.KEYBOARD_OVERRUN_MAKE_CODE) return false;

            var isE0BitSet = ((flags & Win32.RI_KEY_E0) != 0);

            if (_deviceList.ContainsKey(_rawBuffer.header.hDevice))
            {
                lock (_padLock)
                {
                    keyPressEvent = _deviceList[_rawBuffer.header.hDevice];
                }
            }
            else
            {
                Debug.WriteLine("Handle: {0} was not in the device list.", _rawBuffer.header.hDevice);
                return false;
            }

            var isBreakBitSet = ((flags & Win32.RI_KEY_BREAK) != 0);

            keyPressEvent.KeyPressState = isBreakBitSet ? "BREAK" : "MAKE";
            keyPressEvent.Message = _rawBuffer.data.keyboard.Message;
            keyPressEvent.VKeyName = KeyMapper.GetKeyName(VirtualKeyCorrection(virtualKey, isE0BitSet, makeCode)).ToUpper();
            keyPressEvent.VKey = virtualKey;

            if (KeyPressed != null)
            {
                KeyPressed(this, new InputEventArg(keyPressEvent));
            }

            return true;
        }
Ejemplo n.º 7
0
 public RawInputEventArg(KeyPressEvent arg)
 {
     KeyPressEvent = arg;
 }
Ejemplo n.º 8
0
        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);

                        int ierror = Marshal.GetLastWin32Error();

                        sw.WriteLine(ierror.ToString());

                        sw.Flush();
                        sw.Close();
                        file.Close();
                        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());
                        sw.Flush();
                        sw.Close();
                        file.Close();
                        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 KeyPressEvent
                        {
                            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());
        }