Exemple #1
0
        /// <summary>
        /// Registers the devices that supply the raw input data.
        /// </summary>
        /// <param name="usagePage">The usage page.</param>
        /// <param name="usageId">The usage id.</param>
        /// <param name="flags">The flags.</param>
        /// <param name="target">The target.</param>
        /// <param name="options">The options.</param>
        public static void RegisterDevice(UsagePage usagePage, UsageId usageId, DeviceFlags flags, IntPtr target, RegisterDeviceOptions options = RegisterDeviceOptions.Default)
        {
            var rawInputDevices = new RawInputDevice[1];

            rawInputDevices[0].UsagePage = (short)usagePage;
            rawInputDevices[0].Usage     = (short)usageId;
            rawInputDevices[0].Flags     = (int)flags;
            rawInputDevices[0].Target    = target;

            // Register this device
            RawInputFunctions.RegisterRawInputDevices(rawInputDevices, 1, Utilities.SizeOf <RawInputDevice>());

            if (options != RegisterDeviceOptions.NoFiltering && rawInputMessageFilter == null)
            {
                rawInputMessageFilter = new RawInputMessageFilter();
                if (options == RegisterDeviceOptions.Default)
                {
                    Application.AddMessageFilter(rawInputMessageFilter);
                }
                else
                {
                    MessageFilterHook.AddMessageFilter(target, rawInputMessageFilter);
                }
            }
        }
Exemple #2
0
 public ProxyNativeWindow(Control form)
 {
     this._form           = form;
     this._windowHandle   = form.Handle;
     this._form.Disposed += new EventHandler(this._form_Disposed);
     MessageFilterHook.AddMessageFilter(this._windowHandle, (IMessageFilter)this);
     this._isAlive = true;
 }
Exemple #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ProxyNativeWindow"/> class.
 /// </summary>
 public ProxyNativeWindow(Control form)
 {
     _form           = form;
     _windowHandle   = form.Handle;
     _form.Disposed += _form_Disposed;
     MessageFilterHook.AddMessageFilter(_windowHandle, this);
     _isAlive = true;
 }
 protected override void OnLoad(EventArgs e)
 {
     m_isLoaded = true;
     if (EnableMessageBypass)
     {
         MessageFilterHook.AddMessageFilter(this.Handle, this);
     }
     base.OnLoad(e);
 }
Exemple #5
0
 // This allocates, however it should be used only when launching app
 public static void RegisterDevice(UsagePage usagePage, UsageId usageId, DeviceFlags flags, IntPtr target)
 {
     Device.RegisterDevice(usagePage, usageId, flags, target, false);
     if (m_filter == null)
     {
         m_filterWindow = target;
         m_filter       = new RawInputMessageFilter();
         MessageFilterHook.AddMessageFilter(m_filterWindow, m_filter);
     }
 }
        public static void FixKeyboard(Game game)
        {
            IntPtr hWnd = game.Window.Handle;

            Device.RegisterDevice(UsagePage.Generic, UsageId.GenericKeyboard, DeviceFlags.None, hWnd, RegisterDeviceOptions.Default);
            var filter = new RawInputMessageFilter();

            filterCache[hWnd] = filter;
            MessageFilterHook.AddMessageFilter(hWnd, filter);
        }
Exemple #7
0
        /// <summary>
        /// Calls this method on each frame.
        /// </summary>
        /// <returns><c>true</c> if if the control is still active, <c>false</c> otherwise.</returns>
        /// <exception cref="System.InvalidOperationException">An error occured </exception>
        public bool NextFrame()
        {
            // Setup new control
            // TODO this is not completely thread-safe. We should use a lock to handle this correctly
            if (switchControl && control != null)
            {
                controlHandle     = control.Handle;
                control.Disposed += ControlDisposed;
                MessageFilterHook.AddMessageFilter(control.Handle, this);
                isControlAlive = true;
                switchControl  = false;
            }

            if (isControlAlive)
            {
                if (UseCustomDoEvents)
                {
                    var localHandle = controlHandle;
                    if (localHandle != IntPtr.Zero)
                    {
                        // Previous code not compatible with Application.AddMessageFilter but faster then DoEvents
                        Win32Native.NativeMessage msg;
                        while (Win32Native.PeekMessage(out msg, localHandle, 0, 0, 0) != 0)
                        {
                            if (Win32Native.GetMessage(out msg, localHandle, 0, 0) == -1)
                            {
                                throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture,
                                                                                  "An error happened in rendering loop while processing windows messages. Error: {0}",
                                                                                  Marshal.GetLastWin32Error()));
                            }

                            Win32Native.TranslateMessage(ref msg);
                            Win32Native.DispatchMessage(ref msg);
                        }
                    }
                }
                else
                {
                    // Revert back to Application.DoEvents in order to support Application.AddMessageFilter
                    // Seems that DoEvents is compatible with Mono unlike Application.Run that was not running
                    // correctly.
                    Application.DoEvents();
                }
            }

            return(isControlAlive || switchControl);
        }
        /// <summary>
        ///   Starts processing the raw input messages received by the application.
        /// </summary>
        /// <param name="windowHandle">Handle to the target window that will receive the messages.</param>
        /// <param name="options">The options for the processing and filtering of messages.</param>
        /// <remarks>
        ///   Call this function to start filtering and processing raw input messages through the message pump of a target window.
        ///   <para/>
        ///   Using this function, the raw input data is processed one message at a time. In contrast, you can use buffered processing
        ///   by calling <see cref="ProcessMessages"/> to process the queued raw input messages in bulk.
        ///   <para/>
        ///   This function installs a <see cref="IMessageFilter"/> that intercepts and processes the raw input messages received by
        ///   the application. See <see cref="MessageProcessingOptions"/> enumeration for more information.
        /// </remarks>
        public static void StartProcessingMessages(IntPtr windowHandle = default, MessageProcessingOptions options = MessageProcessingOptions.Default)
        {
            if (rawInputMessageFilter != null)
            {
                return;
            }

            targetWindowHandle    = windowHandle;
            rawInputMessageFilter = new RawInputMessageFilter();

            if (options == MessageProcessingOptions.Default)
            {
                Application.AddMessageFilter(rawInputMessageFilter);
            }
            else
            {
                MessageFilterHook.AddMessageFilter(windowHandle, rawInputMessageFilter);
            }
        }
Exemple #9
0
 protected override void OnLoad(EventArgs e)
 {
     MessageFilterHook.AddMessageFilter(this.Handle, this);
     base.OnLoad(e);
 }
Exemple #10
0
 public static void SetWindow(IntPtr windowHandle)
 {
     m_windowHandle = windowHandle;
     MessageFilterHook.AddMessageFilter(windowHandle, new MouseMessageFilter());
 }