WPF: Set Most Recent Input Device


Problem

You want the set the most recent input device for a particular InputManager instance.

Solution

The field that holds a reference to the most recent input device is private to the InputManager class. To change the value from the outside we can use reflection.

using System.Reflection;

namespace System.Windows.Input
{
    /// <summary>
    /// Provides extension methods for setting the most recent input device
    /// for a given input manager instance.
    /// </summary>
    public static class InputManagerExtensions
    {
        /// <summary>
        /// Sets the most recent input device.
        /// </summary>
        /// <param name="inputManager">The <see cref="T:InputManager"/> instance.</param>
        /// <param name="inputDevice">The input device to set as the most recent.</param>
        public static void SetMostRecentInputDevice(this InputManager inputManager, InputDevice inputDevice)
        {
            FieldInfo fi = inputManager.GetMostRecentInputDeviceFieldInfo();
            fi.SetValue(InputManager.Current, inputDevice);
        }

        /// <summary>
        /// Sets the most recent input device to the primary keyboard device.
        /// </summary>
        /// <param name="inputManager">The <see cref="T:InputManager"/> instance.</param>
        public static void SetMostRecentInputDeviceToPrimaryKeyboard(this InputManager inputManager)
        {
            inputManager.SetMostRecentInputDevice(inputManager.PrimaryKeyboardDevice);
        }

        /// <summary>
        /// Sets the most recent input device to the primary mouse device.
        /// </summary>
        /// <param name="inputManager">The <see cref="T:InputManager"/> instance.</param>
        public static void SetMostRecentInputDeviceToPrimaryMouse(this InputManager inputManager)
        {
            inputManager.SetMostRecentInputDevice(inputManager.PrimaryMouseDevice);
        }

        /// <summary>
        /// Gets the field info object for the non-public <see cref="F:InputManager._mostRecentInputDevice"/>
        /// field.
        /// </summary>
        /// <param name="inputManager">The <see cref="T:InputManager"/> instance.</param>
        /// <returns>Returns the field info object for the most recent device.</returns>
        private static FieldInfo GetMostRecentInputDeviceFieldInfo(this InputManager inputManager)
        {
            FieldInfo mostRecentInputDeviceField = typeof(InputManager)
                .GetMember("_mostRecentInputDevice", MemberTypes.Field,
                    BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static)[0] as FieldInfo;
            return mostRecentInputDeviceField;
        }
    }
}

,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: