Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi people,
I have a problem about arrow key binding to command. Other keys works fine. For example, the xaml code in below, works fine

XML
<Window.InputBindings>
    <KeyBinding Command="{Binding Path=DenemeCommand}" Key="A" />
</Window.InputBindings>



But following xaml code doest not

XML
<Window.InputBindings>
    <KeyBinding Command="{Binding Path=DenemeCommand}" Key="Left" />
</Window.InputBindings>



There is same stuation in codebehind.

Code in below works fine

C#
var viewmodel = (ViewModels.ViewModelLocator)FindResource("Locator");
var gesture = new KeyGesture(Key.Delete);
var inputBinding = new InputBinding(viewmodel.MainViewModel.DenemeCommand, gesture);
InputBindings.Add(inputBinding);


this code does not.

C#
var viewmodel = (ViewModels.ViewModelLocator)FindResource("Locator");
 var gesture = new KeyGesture(Key.Left);
 var inputBinding = new InputBinding(viewmodel.MainViewModel.DenemeCommand, gesture);
 InputBindings.Add(inputBinding);


I can not understand why this happen. Not only left key. All arrow key(Right,Down etc.) does not trigger the command execution.

Thanks in advance.
Posted
Updated 9-Jan-20 23:03pm

1 solution

For those who would still be wondering, Left,Right,Down,Up do not trigger key down event (I guess the event is "swallowed" by some other components), you'll have to do attache an event on KeyUpEvent or PreviewKeyUpEvent like so :

EventManager.RegisterClassHandler(typeof(MyControl), TextBox.PreviewKeyUpEvent, new KeyEventHandler(MyControl_KeyDown));

and then

private void MyControl_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Left)
    {

    }
    if (e.Key == Key.Right)
    {

    }
    // And so on
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900