Пример #1
0
        public DeviceService(IntPtr handle)
        {
            AssignHandle(handle);

            this.Keys            = new HashSet <OpenInput.Keys>();
            this.MouseState      = new MouseState();
            this.devNotifyHandle = RegisterForDeviceNotifications(handle);

            FindDevices();

            // Set the mouse initial position
            MouseState.X = Cursor.Position.X;
            MouseState.Y = Cursor.Position.Y;

            // Register devices
            var rids = new RawInputDevice[]
            {
                new RawInputDevice // Mouse
                {
                    UsagePage = HidUsagePage.GENERIC,
                    Usage     = HidUsage.Mouse,
                    Flags     = RawInputDeviceFlags.INPUTSINK | RawInputDeviceFlags.DEVNOTIFY,
                    Target    = handle
                },
                new RawInputDevice // Keyboard
                {
                    UsagePage = HidUsagePage.GENERIC,
                    Usage     = HidUsage.Keyboard,
                    Flags     = RawInputDeviceFlags.INPUTSINK | RawInputDeviceFlags.DEVNOTIFY,
                    Target    = handle
                },
            };

            if (!WindowsInterop.RegisterRawInputDevices(rids, (uint)rids.Length, (uint)Marshal.SizeOf(rids[0])))
            {
                throw new ApplicationException("RawInput: Failed to register devices.");
            }
        }
Пример #2
0
        public DeviceService(IntPtr handle)
        {
            AssignHandle(handle);

            this.Keys = new HashSet<OpenInput.Keys>();
            this.MouseState = new MouseState();
            this.devNotifyHandle = RegisterForDeviceNotifications(handle);

            FindDevices();

            // Set the mouse initial position
            MouseX = Cursor.Position.X;
            MouseY = Cursor.Position.Y;

            // Register devices
            var rids = new RawInputDevice[]
            {
                new RawInputDevice // Mouse
                {
                    UsagePage = HidUsagePage.GENERIC,
                    Usage = HidUsage.Mouse,
                    Flags = RawInputDeviceFlags.INPUTSINK | RawInputDeviceFlags.DEVNOTIFY,
                    Target = handle
                },
                new RawInputDevice // Keyboard
                {
                    UsagePage = HidUsagePage.GENERIC,
                    Usage = HidUsage.Keyboard,
                    Flags = RawInputDeviceFlags.INPUTSINK | RawInputDeviceFlags.DEVNOTIFY,
                    Target = handle
                },
            };

            if (!WindowsInterop.RegisterRawInputDevices(rids, (uint)rids.Length, (uint)Marshal.SizeOf(rids[0])))
                throw new ApplicationException("RawInput: Failed to register devices.");
        }