Beispiel #1
0
        /// <summary>
        /// Gets the key filtering info of the active device.
        /// </summary>
        /// <returns></returns>
        public KeyFiltering KeyboardGetKeyFiltering()
        {
            KeyFiltering filterRequest;

            int outputSize = 2 * sizeof(ushort);

            byte[] outputBuffer = new byte[outputSize];
            if (!DeviceIoControl(_driverHandle, IOCTL_KEYBOARD_GET_FILTER, IntPtr.Zero, 0, outputBuffer, (uint)outputSize, out uint bytesReturned, IntPtr.Zero))
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            if (bytesReturned < 2 * sizeof(ushort))
            {
                throw new Exception("Invalid query result from the driver.");
            }

            filterRequest = KeyFiltering.FromBuffer(outputBuffer);

            if (filterRequest.FilterMode == FilterMode.KEY_FLAG_AND_SCANCODE && filterRequest.FlagOrCount > 0)
            {
                outputSize  += filterRequest.FlagOrCount * Marshal.SizeOf(typeof(KeyFilterData));
                outputBuffer = new byte[outputSize];
                if (!DeviceIoControl(_driverHandle, IOCTL_KEYBOARD_GET_FILTER, IntPtr.Zero, 0, outputBuffer, (uint)outputSize, out bytesReturned, IntPtr.Zero))
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }
                filterRequest = KeyFiltering.FromBuffer(outputBuffer);
            }


            return(filterRequest);
        }
Beispiel #2
0
        internal static KeyFiltering FromBuffer(byte[] buffer)
        {
            KeyFiltering filterRequest = new KeyFiltering();
            int          bytesRead     = 0;
            int          bytesRemained = buffer.Length;

            if (bytesRemained >= sizeof(ushort))
            {
                filterRequest.FilterMode = (FilterMode)BitConverter.ToUInt16(buffer, bytesRead);
                bytesRead     += sizeof(ushort);
                bytesRemained -= sizeof(ushort);
                if (bytesRemained >= sizeof(ushort))
                {
                    filterRequest.FlagOrCount = BitConverter.ToUInt16(buffer, bytesRead);
                    bytesRead     += sizeof(ushort);
                    bytesRemained -= sizeof(ushort);
                    if (filterRequest.FilterMode == FilterMode.KEY_FLAG_AND_SCANCODE && bytesRemained >= filterRequest.FlagOrCount * 2 * sizeof(ushort))
                    {
                        filterRequest.FilterData = new KeyFilterData[filterRequest.FlagOrCount];
                        for (int i = 0; i < filterRequest.FlagOrCount; i++)
                        {
                            filterRequest.FilterData[i].KeyFlagPredicates = (KeyboardKeyFlag)BitConverter.ToUInt16(buffer, bytesRead);
                            bytesRead += sizeof(ushort);
                            filterRequest.FilterData[i].ScanCode = BitConverter.ToUInt16(buffer, bytesRead);
                            bytesRead += sizeof(ushort);
                        }
                    }
                }
            }

            return(filterRequest);
        }
Beispiel #3
0
 /// <summary>
 /// Sets the key filtering for the currently active device.
 /// </summary>
 /// <param name="filterRequest">Structure that contains the key filtering data.</param>
 public void KeyboardSetFiltering(KeyFiltering filterRequest)
 {
     if (filterRequest.FilterMode == FilterMode.KEY_NONE || filterRequest.FilterMode == FilterMode.KEY_ALL)
     {
         ushort filterMode = (ushort)filterRequest.FilterMode;
         if (!DeviceIoControl(_driverHandle, IOCTL_KEYBOARD_SET_FILTER, ref filterMode, sizeof(ushort), IntPtr.Zero, 0, out uint bytesReturned, IntPtr.Zero))
         {
             throw new Win32Exception(Marshal.GetLastWin32Error());
         }
     }
     else
     {
         filterRequest.RemoveRedundant();
         var request = filterRequest.GetBytes();
         if (!DeviceIoControl(_driverHandle, IOCTL_KEYBOARD_SET_FILTER, request, (uint)request.Length, IntPtr.Zero, 0, out uint bytesReturned, IntPtr.Zero))
         {
             throw new Win32Exception(Marshal.GetLastWin32Error());
         }
     }
 }