Пример #1
0
        public static bool GetRawInputData(IntPtr hRawInput,
                                           ref RAWINPUTHEADER header,
                                           ref RAWMOUSE mouse,
                                           ref RAWKEYBOARD keyboard,
                                           ref RAWHID hid)
        {
            int iSize = 0;

            GetRawInputData(hRawInput, 0x10000003, IntPtr.Zero, ref iSize, Marshal.SizeOf <RAWINPUTHEADER>()); // RID_INPUT
            if (iSize == 0)
            {
                return(false);
            }

            IntPtr mem = Marshal.AllocHGlobal(iSize);

            if (mem == IntPtr.Zero)
            {
                return(false);
            }

            if (GetRawInputData(hRawInput, 0x10000003, mem, ref iSize, Marshal.SizeOf <RAWINPUTHEADER>()) != iSize)
            {
                Marshal.FreeHGlobal(mem);
                return(false);
            }

            if (Environment.Is64BitProcess)
            {
                var str = Marshal.PtrToStructure <RAWINPUT64>(mem);
                header   = str.header;
                mouse    = str.mouse;
                keyboard = str.keyboard;
                hid      = str.hid;
            }
            else
            {
                var str = Marshal.PtrToStructure <RAWINPUT32>(mem);
                header   = str.header;
                mouse    = str.mouse;
                keyboard = str.keyboard;
                hid      = str.hid;
            }

            Marshal.FreeHGlobal(mem);
            return(true);
        }
Пример #2
0
        public void InputFromKeyboard_Heatmap(RAWINPUTHEADER riHeader, RAWKEYBOARD riKeyboard)
        {
            if (riKeyboard.Flags == 0x0)
            {
                return;
            }
            ;
            if (riKeyboard.Flags == 0x2 && Control.IsKeyLocked(Keys.NumLock))
            {
                return;
            }
            ;

            int currentKey = keys.GetKeyCode(riKeyboard.MakeCode, riKeyboard.VKey, riKeyboard.Flags, Control.IsKeyLocked(Keys.NumLock));

            keyMatrix[currentKey].NewStrike();
        }
Пример #3
0
        /// <summary>
        ///     Retrieves the raw input header from the specified device.
        /// </summary>
        /// <remarks>See <see cref="GetRawInputData" /> for more information.</remarks>
        /// <param name="hRawInput">
        ///     A handle to the <see cref="RAWINPUT" /> structure. This comes from the
        ///     <b>lParam</b> in <see cref="RawHidReading.Win32.WinMessage.WM.INPUT">WM_INPUT</see>.
        /// </param>
        /// <returns>The raw input header as <see cref="RAWINPUTHEADER" />.</returns>
        public static RAWINPUTHEADER GetRawInputDataHeader(HRAWINPUT hRawInput)
        {
            var size = RAWINPUTHEADER.SIZE;

            var ret = new RAWINPUTHEADER();

            ret.dwSize = size;

            var err = GetRawInputDataH(hRawInput, RID.HEADER, ref ret, ref size, RAWINPUTHEADER.SIZE);

            if (err == uint.MaxValue)
            {
                throw new Exception(string.Format("Error getting header of WM_INPUT data. (Error code: 0x{0:X8})", WinKernel.GetLastError()));
            }

            return(ret);
        }
Пример #4
0
        /// <summary>
        /// Processes WM_INPUT messages to retrieve information about any
        /// keyboard events that occur.
        /// </summary>
        /// <param name="message">The WM_INPUT message to process.</param>
        public void ProcessInputCommand(Message message)
        {
            uint dwSize = 0;

            // First call to GetRawInputData sets the value of dwSize
            // dwSize can then be used to allocate the appropriate amount of memory,
            // storing the pointer in "buffer".
            GetRawInputData(message.LParam,
                            RID_HEADER, IntPtr.Zero,
                            ref dwSize,
                            (uint)Marshal.SizeOf(typeof(RAWINPUTHEADER)));

            IntPtr headerBuffer = Marshal.AllocHGlobal((int)dwSize);

            try
            {
                // Check that buffer points to something, and if so,
                // call GetRawInputData again to fill the allocated memory
                // with information about the input
                if (headerBuffer != IntPtr.Zero &&
                    GetRawInputData(message.LParam,
                                    RID_HEADER,
                                    headerBuffer,
                                    ref dwSize,
                                    (uint)Marshal.SizeOf(typeof(RAWINPUTHEADER))) == dwSize)
                {
                    RAWINPUTHEADER header = (RAWINPUTHEADER)Marshal.PtrToStructure(headerBuffer, typeof(RAWINPUTHEADER));
                    if (header.dwType == RIM_TYPEHID)
                    {
                        DeviceInfo dInfo = null;

                        if (deviceList.Contains(header.hDevice))
                        {
                            dInfo = (DeviceInfo)deviceList[header.hDevice];
                        }
                        else
                        {
                            // Device not in list.  Reenumerate all of them again.  Could warn the code with some sort of Connect/Disconnect event.
                            EnumerateDevices();
                            dInfo = (DeviceInfo)deviceList[header.hDevice];
                        }

                        // The header tells us the size of the actual event
                        IntPtr eventBuffer = Marshal.AllocHGlobal(header.dwSize);

                        uint eventSize = (uint)header.dwSize;
                        if (eventBuffer != IntPtr.Zero &&
                            GetRawInputData(message.LParam,
                                            RID_INPUT,
                                            eventBuffer,
                                            ref eventSize,
                                            (uint)Marshal.SizeOf(typeof(RAWINPUTHEADER))) == header.dwSize)
                        {
                            RAW3DMOUSE_EVENTTYPE eventType = (RAW3DMOUSE_EVENTTYPE)Marshal.PtrToStructure(new IntPtr(eventBuffer.ToInt32() + Marshal.SizeOf(typeof(RAWINPUTHEADER))), typeof(RAW3DMOUSE_EVENTTYPE));
                            switch (eventType.eventType)
                            {
                            case (byte)RAW3DxMouseEventType.TranslationVector:
                                if (header.dwSize == Marshal.SizeOf(typeof(RAWINPUTHEADER)) + SIZEOF_STANDARD_REPORT)      // standard length T report
                                {
                                    RAW3DMOUSEMOTION_T t  = (RAW3DMOUSEMOTION_T)Marshal.PtrToStructure(new IntPtr(eventBuffer.ToInt32() + Marshal.SizeOf(typeof(RAWINPUTHEADER))), typeof(RAW3DMOUSEMOTION_T));
                                    TranslationVector  tv = new TranslationVector(t.X_lb, t.X_hb, t.Y_lb, t.Y_hb, t.Z_lb, t.Z_hb);
                                    // Console.Write("Motion Event = {0} {1} {2}", tv.X, tv.Y, tv.Z);
                                    MotionEvent(this, new MotionEventArgs(dInfo, tv));
                                }
                                else     // "High Speed" firmware version includes both T and R vector in the same report
                                {
                                    RAW3DMOUSEMOTION_TR_COMBINED tr = (RAW3DMOUSEMOTION_TR_COMBINED)Marshal.PtrToStructure(new IntPtr(eventBuffer.ToInt32() + Marshal.SizeOf(typeof(RAWINPUTHEADER))), typeof(RAW3DMOUSEMOTION_TR_COMBINED));
                                    TranslationVector            tv = new TranslationVector(tr.X_lb, tr.X_hb, tr.Y_lb, tr.Y_hb, tr.Z_lb, tr.Z_hb);
                                    RotationVector rv = new RotationVector(tr.RX_lb, tr.RX_hb, tr.RY_lb, tr.RY_hb, tr.RZ_lb, tr.RZ_hb);
                                    // Console.WriteLine("6DOF Motion Event = {0} {1} {2} {3} {4} {5}", tv.X, tv.Y, tv.Z, rv.X, rv.Y, rv.Z);
                                    MotionEvent(this, new MotionEventArgs(dInfo, tv, rv));
                                }
                                break;

                            case (byte)RAW3DxMouseEventType.RotationVector:
                            {
                                RAW3DMOUSEMOTION_R r  = (RAW3DMOUSEMOTION_R)Marshal.PtrToStructure(new IntPtr(eventBuffer.ToInt32() + Marshal.SizeOf(typeof(RAWINPUTHEADER))), typeof(RAW3DMOUSEMOTION_R));
                                RotationVector     rv = new RotationVector(r.X_lb, r.X_hb, r.Y_lb, r.Y_hb, r.Z_lb, r.Z_hb);
                                // Console.WriteLine(" {0} {1} {2}", rv.X, rv.Y, rv.Z);
                                MotionEvent(this, new MotionEventArgs(dInfo, rv));
                            }
                            break;

                            case (byte)RAW3DxMouseEventType.ButtonReport:
                                RAW3DMOUSEBUTTONS b  = (RAW3DMOUSEBUTTONS)Marshal.PtrToStructure(new IntPtr(eventBuffer.ToInt32() + Marshal.SizeOf(typeof(RAWINPUTHEADER))), typeof(RAW3DMOUSEBUTTONS));
                                ButtonMask        bm = new ButtonMask(b.b1, b.b2, b.b3, b.b4);
                                //Console.WriteLine("raw.buttons = {0:X}", bm.Pressed);
                                ButtonEvent(this, new ButtonEventArgs(dInfo, bm));
                                break;
                            }
                        }
                    }
                }
            }
            finally
            {
                Marshal.FreeHGlobal(headerBuffer);
            }
        }
Пример #5
0
 //TODO: keystate and modifier keys.
 private void ProcessRawKeyboardInput(RAWKEYBOARD keyboard, RAWINPUTHEADER rh)
 {
     KeyboardInput?.Invoke(this, new RawKeyboardInputEventArgs(rh, keyboard));
 }
Пример #6
0
 private void ProcessRawMouseInput(RAWMOUSE mouse, RAWINPUTHEADER rh)
 {
     MouseInput?.Invoke(this, new RawMouseInputEventArgs(rh, mouse));
 }
 public RawKeyboardInputEventArgs(RAWINPUTHEADER header, RAWKEYBOARD data)
 {
     Header = header;
     Data   = data;
 }
Пример #8
0
 private static extern uint GetRawInputDataH(HRAWINPUT hRawInput, RID uiCommand, ref RAWINPUTHEADER pData, ref uint pcbSize, uint cbSizeHeader);
Пример #9
0
        /// <summary>
        /// Processes a <see cref="WM.INPUT">WM_INPUT</see> messages and raises the proper events.
        /// </summary>
        /// <param name="lParam">The handle to the raw input dataset.</param>
        /// <param name="size">The size, in bytes, of the raw input dataset.</param>
        public void ProcessInput(IntPtr lParam, uint size)
        {
            IntPtr buffer = Marshal.AllocHGlobal((int)size);
            uint   dwSize = size;

            try
            {
                uint err = RawInput.GetRawInputData(lParam, RID.INPUT, buffer, ref dwSize, RAWINPUTHEADER.SIZE);
                if (err == uint.MaxValue)
                {
                    throw new Exception(string.Format("Error getting WM_INPUT data. (Error code: 0x{0:X8})", WinKernel.GetLastError()));
                }

                RAWINPUTHEADER        inputHeader = (RAWINPUTHEADER)Marshal.PtrToStructure(buffer, typeof(RAWINPUTHEADER));
                SpaceMouseEventHeader eventHeader = (SpaceMouseEventHeader)Marshal.PtrToStructure(buffer + (int)RAWINPUTHEADER.SIZE, typeof(SpaceMouseEventHeader));

                int eventHeaderSize = SpaceMouseEventHeader.SIZE + (int)RAWINPUTHEADER.SIZE;

                switch (eventHeader.eventType)
                {
                case SpaceMouseEventType.Translation:
                    if (eventHeader.dwSizeHid == 7)                          // check report size for safety
                    {
                        EventHandler <SpaceMouseTranslationEventArgs> tEvt = Translation;
                        if (tEvt != null)                              // no need to handle if nobody is listing
                        {
                            for (int i = 0; i < eventHeader.dwCount; i++)
                            {
                                SpaceMouseTranslationEventData translation = (SpaceMouseTranslationEventData)Marshal.PtrToStructure(buffer + eventHeaderSize + i * (int)eventHeader.dwSizeHid, typeof(SpaceMouseTranslationEventData));
                                tEvt(this, new SpaceMouseTranslationEventArgs(inputHeader.hDevice, translation.X, translation.Y, translation.Z));
                            }
                        }
                    }
                    else if (eventHeader.dwSizeHid == 13)                          // "High Speed" firmware version includes both T and R vector in the same report
                    {
                        EventHandler <SpaceMouseTranslationEventArgs> tEvt = Translation;
                        EventHandler <SpaceMouseRotationEventArgs>    rEvt = Rotation;
                        if (tEvt != null || rEvt != null)                          // no need to handle if nobody is listing
                        {
                            for (int i = 0; i < eventHeader.dwCount; i++)
                            {
                                SpaceMouseTranslationRotationEventData transRot = (SpaceMouseTranslationRotationEventData)Marshal.PtrToStructure(buffer + eventHeaderSize + i * (int)eventHeader.dwSizeHid, typeof(SpaceMouseTranslationRotationEventData));
                                if (tEvt != null)
                                {
                                    tEvt(this, new SpaceMouseTranslationEventArgs(inputHeader.hDevice, transRot.X, transRot.Y, transRot.Z));
                                }
                                if (rEvt != null)
                                {
                                    rEvt(this, new SpaceMouseRotationEventArgs(inputHeader.hDevice, transRot.RX, transRot.RY, transRot.RZ));
                                }
                            }
                        }
                    }
                    break;

                case SpaceMouseEventType.Rotation:
                    if (eventHeader.dwSizeHid == 7)                          // check report size for safety
                    {
                        EventHandler <SpaceMouseRotationEventArgs> rEvt = Rotation;
                        if (rEvt != null)                              // no need to handle if nobody is listing
                        {
                            for (int i = 0; i < eventHeader.dwCount; i++)
                            {
                                SpaceMouseRotationEventData rotation = (SpaceMouseRotationEventData)Marshal.PtrToStructure(buffer + eventHeaderSize + i * (int)eventHeader.dwSizeHid, typeof(SpaceMouseRotationEventData));
                                rEvt(this, new SpaceMouseRotationEventArgs(inputHeader.hDevice, rotation.RX, rotation.RY, rotation.RZ));
                            }
                        }
                    }
                    break;

                case SpaceMouseEventType.Buttons:
                    if (eventHeader.dwSizeHid >= 5)                          // check report size for safety
                    {
                        EventHandler <SpaceMouseButtonsEventArgs> bEvt = Buttons;
                        if (bEvt != null)                              // no need to handle if nobody is listing
                        {
                            if (!DeviceToProduct.ContainsKey(inputHeader.hDevice))
                            {
                                var di = RawInput.GetRawInputDeviceInfo(inputHeader.hDevice);
                                DeviceToProduct.Add(inputHeader.hDevice, di.hid.dwProductId);
                            }

                            uint product = DeviceToProduct[inputHeader.hDevice];

                            for (int i = 0; i < eventHeader.dwCount; i++)
                            {
                                SpaceMouseButtonsEventData tmpButtons = (SpaceMouseButtonsEventData)Marshal.PtrToStructure(buffer + eventHeaderSize + i * (int)eventHeader.dwSizeHid, typeof(SpaceMouseButtonsEventData));

                                SpaceMouseButtons buttons = SpaceMouseButtons.NONE;
                                switch (product)
                                {
                                case ProductID.SpacePilot: buttons = FromSpacePilot(tmpButtons.ButtonState); break;

                                case ProductID.SpaceExplorer: buttons = FromSpaceExplorer(tmpButtons.ButtonState); break;

                                case ProductID.SpaceNavigator:
                                case ProductID.SpaceNavigatorforNotebooks:
                                case ProductID.SpaceMouse:
                                case ProductID.SpaceMouseWireless: buttons = FromSpaceMouse(tmpButtons.ButtonState); break;

                                case ProductID.SpacePilotPro: buttons = FromSpacePilotPro(tmpButtons.ButtonState); break;

                                case ProductID.SpaceMousePro:
                                case ProductID.SpaceMouseProWireless: buttons = FromSpaceMousePro(tmpButtons.ButtonState); break;

                                case ProductID.SpaceMouseTouch: buttons = FromSpaceMouseTouch(tmpButtons.ButtonState); break;
                                }

                                bEvt(this, new SpaceMouseButtonsEventArgs(inputHeader.hDevice, buttons));
                            }
                        }
                    }
                    break;
                }
            }
            finally
            {
                Marshal.FreeHGlobal(buffer);
            }
        }
Пример #10
0
 public RawMouseInputEventArgs(RAWINPUTHEADER header, RAWMOUSE data)
 {
     Header = header;
     Data   = data;
 }
Пример #11
0
        public void InputFromKeyboard_SingleLight(RAWINPUTHEADER riHeader, RAWKEYBOARD riKeyboard)
        {
            if (riKeyboard.Flags == 0x0)
            {
                return;
            }
            ;
            if (riKeyboard.Flags == 0x2 && Control.IsKeyLocked(Keys.NumLock))
            {
                return;
            }
            ;


            string s = "";

            s = s + "0x" + riKeyboard.MakeCode.ToString("X4");
            s = s + "  0x" + riKeyboard.VKey.ToString("X4");
            //            s = s + "  Mssg: 0x" + riKeyboard.Message.ToString("X4");
            s = s + "  0x" + riKeyboard.Flags.ToString("X4");
            //            s = s + "   Rsrvd: 0x" + riKeyboard.Reserved.ToString("X4");
            //            s = s + "   ExInf: 0x" + riKeyboard.ExtraInformation.ToString("X4");
            //            s = s + "  Devc: 0x" + riHeader.hDevice.ToString("X4");
            //            s = s + "   .Size: 0x" + riHeader.dwSize.ToString("X4");
            //            s = s + "   .Type: 0x" + riHeader.dwType.ToString("X4");
            //            s = s + "   wPara: 0x" + riHeader.wParam.ToString("X4");
            System.Diagnostics.Debug.Write(s);
            UpdateStatusMessage.ShowStatusMessage(5, s);


            int currentKey = keys.GetKeyCode(riKeyboard.MakeCode, riKeyboard.VKey, riKeyboard.Flags, Control.IsKeyLocked(Keys.NumLock));
            int sR = 0; int sG = 0; int sB = 0;
            int eR = 0; int eG = 0; int eB = 0;

            switch (Program.ReactTypeStart)
            {
            case 0:
                sR = Program.ReactColors.StartR;
                sG = Program.ReactColors.StartG;
                sB = Program.ReactColors.StartB;
                break;

            case 1:
                //Figure out how to make a rainbow here, probably going to need to rewrite SingleKeyFade classs
                break;

            case 2:
                sR = rnd.Next(Program.ReactColors.SRandRLow, Program.ReactColors.SRandRHigh);
                sG = rnd.Next(Program.ReactColors.SRandGLow, Program.ReactColors.SRandGHigh);
                sB = rnd.Next(Program.ReactColors.SRandBLow, Program.ReactColors.SRandBHigh);
                break;

            default:
                break;
            }

            switch (Program.ReactTypeEnd)
            {
            case 0:
                eR = Program.ReactColors.EndR;
                eG = Program.ReactColors.EndG;
                eB = Program.ReactColors.EndB;
                break;

            case 1:
                if (Program.StaticKeyColors[currentKey] != Color.Transparent)
                {
                    eR = Program.StaticKeyColors[currentKey].R;
                    eG = Program.StaticKeyColors[currentKey].G;
                    eB = Program.StaticKeyColors[currentKey].B;
                }
                break;

            case 2:
                eR = rnd.Next(Program.ReactColors.ERandRLow, Program.ReactColors.ERandRHigh);
                eG = rnd.Next(Program.ReactColors.ERandGLow, Program.ReactColors.ERandGHigh);
                eB = rnd.Next(Program.ReactColors.ERandBLow, Program.ReactColors.ERandBHigh);
                break;

            default:
                break;
            }

            keyMatrix[currentKey] = new SingleKeyFade(
                Program.ReactSettings.Duration,
                (byte)sR,
                (byte)sG,
                (byte)sB,
                (byte)eR,
                (byte)eG,
                (byte)eB);
        }