internal static KeyModification FromBuffer(byte[] buffer)
        {
            KeyModification modifyRequest = new KeyModification();
            int             bytesRead     = 0;
            int             bytesRemained = buffer.Length;

            if (bytesRemained >= sizeof(ushort))
            {
                modifyRequest.ModifyCount = BitConverter.ToUInt16(buffer, bytesRead);
                bytesRead     += sizeof(ushort);
                bytesRemained -= sizeof(ushort);
                if (bytesRemained >= modifyRequest.ModifyCount * 3 * sizeof(ushort))
                {
                    modifyRequest.ModifyData = new KeyModifyData[modifyRequest.ModifyCount];
                    for (int i = 0; i < modifyRequest.ModifyCount; i++)
                    {
                        modifyRequest.ModifyData[i].KeyStatePredicates = (KeyboardKeyFlag)BitConverter.ToUInt16(buffer, bytesRead);
                        bytesRead += sizeof(ushort);
                        modifyRequest.ModifyData[i].FromScanCode = BitConverter.ToUInt16(buffer, bytesRead);
                        bytesRead += sizeof(ushort);
                        modifyRequest.ModifyData[i].ToScanCode = BitConverter.ToUInt16(buffer, bytesRead);
                        bytesRead += sizeof(ushort);
                    }
                }
            }

            return(modifyRequest);
        }
Beispiel #2
0
        /// <summary>
        /// Sets the key modifications for the currently active device.
        /// </summary>
        /// <param name="modifyRequest">Structure that contains the key modification data.</param>
        public void KeyboardSetModification(KeyModification modifyRequest)
        {
            modifyRequest.RemoveRedundant();

            if (modifyRequest.ModifyData == null || modifyRequest.ModifyData.Length == 0)
            {
                ushort modifyCount = 0;
                if (!DeviceIoControl(_driverHandle, IOCTL_KEYBOARD_SET_MODIFY, ref modifyCount, sizeof(ushort), IntPtr.Zero, 0, out uint bytesReturned, IntPtr.Zero))
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }
            }
            else
            {
                var request = modifyRequest.GetBytes();
                if (!DeviceIoControl(_driverHandle, IOCTL_KEYBOARD_SET_MODIFY, request, (uint)request.Length, IntPtr.Zero, 0, out uint bytesReturned, IntPtr.Zero))
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Gets the key modification info of the currently active device.
        /// </summary>
        /// <returns></returns>
        public KeyModification KeyboardGetKeyModifying()
        {
            KeyModification modifyRequest = new KeyModification();

            if (!DeviceIoControl(_driverHandle, IOCTL_KEYBOARD_GET_MODIFY, IntPtr.Zero, 0, out ushort modifyCount, sizeof(ushort), out uint bytesReturned, IntPtr.Zero))
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            modifyRequest.ModifyCount = modifyCount;
            if (modifyRequest.ModifyCount > 0)
            {
                int    outputSize   = sizeof(ushort) + modifyRequest.ModifyCount * Marshal.SizeOf(typeof(KeyModifyData));
                byte[] outputBuffer = new byte[outputSize];
                if (!DeviceIoControl(_driverHandle, IOCTL_KEYBOARD_GET_MODIFY, IntPtr.Zero, 0, outputBuffer, (uint)outputSize, out bytesReturned, IntPtr.Zero))
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }
                modifyRequest = KeyModification.FromBuffer(outputBuffer);
            }
            return(modifyRequest);
        }