示例#1
0
 /// <summary>
 /// Performs tasks associated with freeing, releasing, or resetting unmanaged resources.
 /// </summary>
 public void Dispose()
 {
     UnregisterDeviceNotification(deviceNotificationHandle);
     Application.RemoveMessageFilter(messageFilter);
     nativeWindowWrap.Dispose();
     nativeWindowWrap = null;
 }
示例#2
0
        /// <summary>
        /// Initializes an instance of Keyboards.
        /// </summary>
        /// <param name="handle">The handle of the receiver (form) which will process the caught messages.</param>
        public Keyboards(IntPtr handle)
        {
            messageFilter = new MessageFilter();
            Application.AddMessageFilter(messageFilter);
            nativeWindowWrap = new NativeWindowWrap(handle, ProcessKey);

            var broadcastDeviceInterface = new BroadcastDeviceInterface(multiKeyboardDeviceId);

            var mem = IntPtr.Zero;

            try
            {
                mem = Marshal.AllocHGlobal(sizeOfBroadcastDeviceInterface);
                Marshal.StructureToPtr(broadcastDeviceInterface, mem, false);
                deviceNotificationHandle = RegisterDeviceNotification(handle, mem, 0 /*DEVICE_NOTIFY_WINDOW_HANDLE*/);
            }
            finally
            {
                Marshal.FreeHGlobal(mem);
            }

            if (deviceNotificationHandle == IntPtr.Zero)
            {
                throw new InvalidOperationException(string.Format("Registration for device notifications Failed. Error: {0}", Marshal.GetLastWin32Error()));
            }
        }