键盘事件:如何捕捉和关闭键? [英] Keydown event: how to capture down and up key rows?

查看:149
本文介绍了键盘事件:如何捕捉和关闭键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试捕获向下和向上的键(方向行),但是当我按这个键时,它不会引发事件锁定。

I am trying to capture the down and up keys (the direction rows) but when I press this keys, it is not raised the event keydown.

但是,如果我按任何其他键,则会引发事件。例如numlock被捕获。行键是特殊键?

However, if I press any other key, the event is raised. For example numlock is catched. The row keys are special keys?

我正在使用MVVMLight将事件转换为命令,并传递KeyEventArgs。

I am using MVVMLight to convert the events to command, and pass the KeyEventArgs.

谢谢。

编辑:添加一些代码

真的我有一个comboBox,并且是可编辑的,所以我可以在comboBox内写文本。搜索选项如何启用,当我在写作时,选择被更改。

Well. really I have a comboBox, and is editable, so I can write text inside the comboBox. How the search option is enabled, while I am writing, the selecition is changed.

所以选择可以改变很多原因:我写和comboBox更改选择,因为的搜索选项,我可以用鼠标更改选择,我可以用箭头键更改选择。

So the selection can change for many reasons: I write and the comboBox change the selection because of the search option, I can change the selection with the mouse and I can change the selection with the arrow keys.

我想知道是什么原因选择变化。所以我需要知道什么时候在我的comboBox我按下或向上箭头键。

I would like to know which is the reason of the selection change. So I need to know when in my comboBox I press down or up arrow keys.

我有这个代码:

AXML

<ComboBox DisplayMemberPath="Type" Height="23" HorizontalAlignment="Left" IsSynchronizedWithCurrentItem="True" Margin="0,16,0,0" Name="cmbType" VerticalAlignment="Top" Width="238"
            ItemsSource="{Binding Path=Types}"
            SelectedIndex="{Binding Path=TypesIndex}" IsEditable="True"
            Text="{Binding TypesText}">

            <i:Interaction.Triggers>
                <i:EventTrigger EventName="PreviewKeyDown">
                    <cmd:EventToCommand Command="{Binding TypesPreviewKeyDownCommand, Mode=OneWay}" PassEventArgsToCommand="True" />
                </i:EventTrigger>    
                <i:EventTrigger EventName="SelectionChanged">
                    <cmd:EventToCommand Command="{Binding TypesSelectionChangedCommand, Mode=OneWay}" CommandParameter="{Binding ElementName=cmbTypes, Path=SelectedItems}" />
                </i:EventTrigger>    
            </i:Interaction.Triggers>
        </ComboBox>

在我的viewModel中:

In my viewModel:

private RelayCommand<KeyEventArgs> _typesPreviewKeyDownCommand = null;
        public RelayCommand<KeyEventArgs> typesPreviewKeyDownCommand
        {
            get
            {
                if (_typesPreviewKeyDownCommand == null)
                {
                    _typesPreviewKeyDownCommand = new RelayCommand<KeyEventArgs>(typesPreviewKeyDownCommand);
                }
                return _typesPreviewKeyDownCommand;
            }
        }





private void typesPreviewKeyDownCommand(KeyEventArgs e)
        {
            if (e.Key == Key.Down || e.Key == Key.Up)
            {
                //my code
            }
            else
            {
                //more code
            }
        }


推荐答案

不知道如果相关了,但是这里有一篇关于CodeProject的文章,讨论了一个非常相似的问题/行为
DatePicker上的Up / Down行为

Not sure if relevant anymore, but here's an article on CodeProject which discusses a very similar issue/behaviour Up/Down behavior on a DatePicker

这很简单在代码后面处理就像文章所示,但是如果你想要做MVVM风格,你需要与 i:交互化InputBindings
我喜欢交互,因为加上mvvm-light,似乎对我来说更好,而使用 InputBindings 我发现上/下键没有没有任何工作,而像ALT这样的修饰符就可以正常工作。

It's very simple to handle in the Code Behind like the article suggests, but if you want to do it MVVM style, you need to go with the i:Interaction or InputBindings. I prefer the Interaction, since coupled with mvvm-light it seems to work better for me, while with the InputBindings I've found that up/down keys didn't work, while having a modifier like ALT would work.

正如评论所说,它可能正在被处理的地方,在它得到你之前。 (这就是为什么你想使用 PreviewKeyDown 而不是 KeyDown )。

As the comments said, it's probably being handled somewhere on the way before it gets to you. (this is why you'd like to use the PreviewKeyDown and not KeyDown).

这篇关于键盘事件:如何捕捉和关闭键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆