0

Alt无法在+上获得事件Key。示例Alt+ EE为and触发事件Alt,但在 ? 中没有Alt+ E Mb 问题IKeyProcessorProvider?我有用户控件,并且想要使用内部控件ButtonKeyProc.KeyDownEvent+=

[Export(typeof(IKeyProcessorProvider))]
[TextViewRole(PredefinedTextViewRoles.Document)]
[ContentType("any")]
[Name("ButtonProvider")]
[Order(Before = "default")]
internal class ButtonProvider : IKeyProcessorProvider
{
    [ImportingConstructor]
    public ButtonProvider()
    {
    }

    public KeyProcessor GetAssociatedProcessor(IWpfTextView wpfTextView)
    {
        return new ButtonKeyProc(wpfTextView);
    }
}


internal class ButtonKeyProc : KeyProcessor
{
    internal static event KeyEventHandler KeyDownEvent;

    public ButtonKeyProc(ITextView textView)
    {
    }

    public override void KeyDown(KeyEventArgs args)
    {
        if (args.Key == Key.E && IsAlt)
        {
            if (KeyDownEvent != null)
            {
                KeyDownEvent(this, args);
            }
        }           
    }

    public bool IsAlt
    {
        get { return Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt); }
    }
4

1 回答 1

2

正确的代码。需要用到 args.SystemKey 和 Keyboard.Modifiers。

public override void KeyDown(KeyEventArgs args)
{
    if (args.SystemKey == Key.E && (Keyboard.Modifiers & ModifierKeys.Alt) != 0)
    {            
    }           
}
于 2013-08-21T06:01:10.193 回答