处理MVVM中的按键和释放事件

时间:2022-02-21 00:08:30

I am developing a wpf application using MVVM pattern. I need to separately handle Key press & release events, (e.g. in media players fwd/rev happen till user keeps key pressed & stops when he releases). After searching a lot, still I couldn't find any way to do it. Can anybody please help.

我正在使用MVVM模式开发一个wpf应用程序。我需要单独处理按键和释放事件,(例如在媒体播放器中发生fwd / rev直到用户按下按键并在发布时停止)。经过大量搜索,我仍然无法找到任何办法。任何人都可以帮忙。

2 个解决方案

#1


0  

I'm guessing, You will bind the Command to Button. If you want the Commands to fire repeatedly, you can use RepeatButton. It was designed for that purpose.You can bind your command to Command Property. It will fire your methods repeatedly until RepeatButton is released.

我猜,你将命令绑定到Button。如果希望重复触发命令,可以使用RepeatButton。它是为此目的而设计的。您可以将命令绑定到Command Property。它将重复触发您的方法,直到RepeatButton被释放。

#2


0  

Thanks for your suggestions. I found a way to do this by using interactivity triggers & dependency property. Following is the dependency property for Command.

谢谢你的建议。我通过使用交互触发器和依赖属性找到了一种方法。以下是Command的依赖项属性。

public class EventToCommand : TriggerAction<DependencyObject>
{
    public ICommand Command
    {
        get { return (ICommand)GetValue(CommandProperty); }
        set { SetValue(CommandProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Command.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty CommandProperty =
        DependencyProperty.Register("Command", typeof(ICommand), typeof(EventToCommand), new PropertyMetadata(null));

    protected override void Invoke(object parameter)
    {
        if (Command != null
            && Command.CanExecute(parameter))
        {
            Command.Execute(parameter);
        }
    }
}

Then just use it in the xaml as below:

然后在xaml中使用它,如下所示:

    <i:Interaction.Triggers>
    <i:EventTrigger EventName="KeyUp">
        <ap:EventToCommand Command="{Binding KeyReleaseCommand}"></ap:EventToCommand>
    </i:EventTrigger>
    <i:EventTrigger EventName="KeyDown">
        <ap:EventToCommand Command="{Binding KeyDownCommand}"></ap:EventToCommand>
    </i:EventTrigger>
</i:Interaction.Triggers>

Where the KeyReleaseCommand & KeyDownCommand are RelayCommand in your ViewModel.

KeyReleaseCommand和KeyDownCommand是ViewModel中的RelayCommand。

    public MainViewModel()
    {
        KeyDownCommand = new RelayCommand<KeyEventArgs>(OnKeyDown, null);
        KeyReleaseCommand = new RelayCommand<KeyEventArgs>(OnKeyRelease, null);
    }

    private void OnKeyRelease(KeyEventArgs args)
    {
        if (args.KeyboardDevice.Modifiers == ModifierKeys.Alt)
        {
            if (args.SystemKey == Key.Left)
            {
                Trace.WriteLine("ALT+LEFT Released");
            }
        }
    }

    public void OnKeyDown(KeyEventArgs args)
    {
        if (args.IsRepeat)
            return;

        if (args.KeyboardDevice.Modifiers == ModifierKeys.Alt)
        {
            if(args.SystemKey == Key.Left)
            {
                Trace.WriteLine("ALT+LEFT");
            }
        }
    }

#1


0  

I'm guessing, You will bind the Command to Button. If you want the Commands to fire repeatedly, you can use RepeatButton. It was designed for that purpose.You can bind your command to Command Property. It will fire your methods repeatedly until RepeatButton is released.

我猜,你将命令绑定到Button。如果希望重复触发命令,可以使用RepeatButton。它是为此目的而设计的。您可以将命令绑定到Command Property。它将重复触发您的方法,直到RepeatButton被释放。

#2


0  

Thanks for your suggestions. I found a way to do this by using interactivity triggers & dependency property. Following is the dependency property for Command.

谢谢你的建议。我通过使用交互触发器和依赖属性找到了一种方法。以下是Command的依赖项属性。

public class EventToCommand : TriggerAction<DependencyObject>
{
    public ICommand Command
    {
        get { return (ICommand)GetValue(CommandProperty); }
        set { SetValue(CommandProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Command.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty CommandProperty =
        DependencyProperty.Register("Command", typeof(ICommand), typeof(EventToCommand), new PropertyMetadata(null));

    protected override void Invoke(object parameter)
    {
        if (Command != null
            && Command.CanExecute(parameter))
        {
            Command.Execute(parameter);
        }
    }
}

Then just use it in the xaml as below:

然后在xaml中使用它,如下所示:

    <i:Interaction.Triggers>
    <i:EventTrigger EventName="KeyUp">
        <ap:EventToCommand Command="{Binding KeyReleaseCommand}"></ap:EventToCommand>
    </i:EventTrigger>
    <i:EventTrigger EventName="KeyDown">
        <ap:EventToCommand Command="{Binding KeyDownCommand}"></ap:EventToCommand>
    </i:EventTrigger>
</i:Interaction.Triggers>

Where the KeyReleaseCommand & KeyDownCommand are RelayCommand in your ViewModel.

KeyReleaseCommand和KeyDownCommand是ViewModel中的RelayCommand。

    public MainViewModel()
    {
        KeyDownCommand = new RelayCommand<KeyEventArgs>(OnKeyDown, null);
        KeyReleaseCommand = new RelayCommand<KeyEventArgs>(OnKeyRelease, null);
    }

    private void OnKeyRelease(KeyEventArgs args)
    {
        if (args.KeyboardDevice.Modifiers == ModifierKeys.Alt)
        {
            if (args.SystemKey == Key.Left)
            {
                Trace.WriteLine("ALT+LEFT Released");
            }
        }
    }

    public void OnKeyDown(KeyEventArgs args)
    {
        if (args.IsRepeat)
            return;

        if (args.KeyboardDevice.Modifiers == ModifierKeys.Alt)
        {
            if(args.SystemKey == Key.Left)
            {
                Trace.WriteLine("ALT+LEFT");
            }
        }
    }