Visual Studio扩展KeyProcessor Alt键

时间:2013-08-16 08:06:22

标签: c# visual-studio visual-studio-extensions

无法在 Alt + 上获取事件。示例 Alt + E E Alt 的事件触发,但没有 Alt + E IKeyProcessorProvider中的Mb问题?我有usercontrol,并且想要使用内部控件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); }
    }

1 个答案:

答案 0 :(得分:1)

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

public override void KeyDown(KeyEventArgs args)
{
    if (args.SystemKey == Key.E && (Keyboard.Modifiers & ModifierKeys.Alt) != 0)
    {            
    }           
}