コード例 #1
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)));

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

            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;
            }

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

            KeyPressEvent keyPressEvent;

            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.ToString());
                return;
            }

            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 RawInputEventArg(keyPressEvent));
            }
        }
コード例 #2
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)));

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

            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;
            }

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

            KeyPressEvent keyPressEvent;

            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;
            }

            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 && keyPressEvent.Source == "Keyboard_01" && keyPressEvent.Message == 0x0100)
            {
                //WORKED YES
                var pID = 0;
                foreach (var process in Process.GetProcesses())
                {
                    if (process.ProcessName == "Spotify" && !String.IsNullOrEmpty(process.MainWindowTitle))
                    {
                        pID = process.Id;
                    }
                }
                //Console.WriteLine(keyPressEvent.VKeyName);

                if (pID == 0)
                {
                    return;
                }
                float?currentVolume = VolumeMixer.GetApplicationVolume(pID);
                float minVolume     = 0;
                float maxVolume     = 100;
                //Console.WriteLine(currentVolume);
                if (keyPressEvent.VKeyName == "NUMPAD8")
                {
                    if (currentVolume < maxVolume - 5f)
                    {
                        maxVolume = (float)currentVolume + 5f;
                    }
                    VolumeMixer.SetApplicationVolume(pID, maxVolume);
                }
                else if (keyPressEvent.VKeyName == "NUMPAD2")
                {
                    if (currentVolume > minVolume + 5f)
                    {
                        minVolume = (float)currentVolume - 5f;
                    }
                    VolumeMixer.SetApplicationVolume(pID, minVolume);
                }
                else if (keyPressEvent.VKeyName == "NUMPAD5")
                {
                    // Play or Pause music
                    keybd_event(VK_MEDIA_PLAY_PAUSE, 0, KEYEVENTF_EXTENTEDKEY, IntPtr.Zero);
                }
                else if (keyPressEvent.VKeyName == "NUMPAD4")
                {
                    // Jump to previous track
                    keybd_event(VK_MEDIA_PREV_TRACK, 0, KEYEVENTF_EXTENTEDKEY, IntPtr.Zero);
                }
                else if (keyPressEvent.VKeyName == "NUMPAD6")
                {
                    // Jump to next track
                    keybd_event(VK_MEDIA_NEXT_TRACK, 0, KEYEVENTF_EXTENTEDKEY, IntPtr.Zero);
                }
                else if (keyPressEvent.VKeyName == "NUMPAD0")
                {
                    //mute
                    if (VolumeMixer.GetApplicationMute(pID) == false)
                    {
                        VolumeMixer.SetApplicationMute(pID, true);
                    }
                    else
                    {
                        VolumeMixer.SetApplicationMute(pID, false);
                    }
                    // VolumeMixer.GetApplicationMute(pID) == false ? VolumeMixer.SetApplicationMute(pID, true) : VolumeMixer.SetApplicationMute(pID, false);
                }
            }
            else
            {
                KeyPressed(this, new RawInputEventArg(keyPressEvent));
            }
        }