using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Windows.Input;
namespace Livet
{
public static class LivetCommandManager
{
static LivetCommandManager()
{
InputManager.Current.PostProcessInput += new ProcessInputEventHandler(RaisedPostProcessInput);
var primaryCommandDevice = typeof(InputManager)
.GetProperty("PrimaryCommandDevice", BindingFlags.Instance | BindingFlags.NonPublic)
.GetValue(InputManager.Current, null);
var preProcessInput = primaryCommandDevice.GetType()
.GetMethod("PreProcessInput", BindingFlags.Instance | BindingFlags.NonPublic);
var postProcessInput = primaryCommandDevice.GetType()
.GetMethod("PostProcessInput", BindingFlags.Instance | BindingFlags.NonPublic);
var preProcessInputDelegate = Delegate.CreateDelegate(typeof(PreProcessInputEventHandler), primaryCommandDevice, preProcessInput);
var postProcessInputDelegate = Delegate.CreateDelegate(typeof(ProcessInputEventHandler), primaryCommandDevice, postProcessInput);
typeof(InputManager).GetEvent("PreProcessInput").RemoveEventHandler(InputManager.Current, preProcessInputDelegate);
typeof(InputManager).GetEvent("PostProcessInput").RemoveEventHandler(InputManager.Current, postProcessInputDelegate);
Mode = InvalidateRequerySuggestedMode.Default;
}
private static void RaisedPostProcessInput(object sender, ProcessInputEventArgs e)
{
if (Mode == InvalidateRequerySuggestedMode.Default || Mode == InvalidateRequerySuggestedMode.Full)
{
var routedEvent = e.StagingItem.Input.RoutedEvent;
if (routedEvent == Keyboard.KeyUpEvent ||
routedEvent == Mouse.MouseUpEvent ||
routedEvent == Keyboard.GotKeyboardFocusEvent ||
routedEvent == Keyboard.LostKeyboardFocusEvent)
{
CommandManager.InvalidateRequerySuggested();
}
}
}
public static InvalidateRequerySuggestedMode Mode
{
get;
set;
}
}
public enum InvalidateRequerySuggestedMode
{
None,
Default,
Full
}
}