Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KeyDown event not raising from a grid

Here I have sample window with a grid. I need to capture event when key is pressed. But it is not raising when I click grid area and then press key. It will work only if Textbox is focused. I know it will work if I capture it from Window. But I have other application with few usercontrols and I need to capture it from distinct ones. I tried to set Focusable.false for Window and true for Grid but it not helps. Any solutions?

<Window x:Class="Beta.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" Closed="Window_Closed_1" Focusable="False">

    <Grid KeyDown="Grid_KeyDown_1" Focusable="True">
    <TextBox x:Name="tbCount" HorizontalAlignment="Left" Height="35" Margin="310,49,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="83"/>

  </Grid>

like image 683
Dork Avatar asked Mar 06 '13 07:03

Dork


People also ask

What is the difference between the Keydown and KeyUp events?

Occur in sequence when a user presses and releases a key. KeyDown occurs when the user presses a key. KeyUp occurs when the user releases a key.

Is Keydown an event?

The keydown event is fired when a key is pressed. Unlike the deprecated keypress event, the keydown event is fired for all keys, regardless of whether they produce a character value. The keydown and keyup events provide a code indicating which key is pressed, while keypress indicates which character was entered.

What is the difference between Onkeypress and Onkeydown?

keydown – fires when any key is pressed down, fires first, and always before the browser processes the key (e.g. inserting text, moving focus, etc). keypress – fires when a key that produces a character value is pressed down, fires after keydown , and before the browser processes the key.

How do you use Keydown function?

The keydown() is an inbuilt method in jQuery which is used to trigger the keydown event whenever User presses a key on the keyboard. If the key is kept pressed, the event is sent every time the operating system repeats the key. So, Using keydown() method we can detect if any key is on its way down.


3 Answers

Right this is weird. This is clearly a focus problem, still I can not understand why the grid do not take Focus, even when we click on it.

Though there is a workaround: create an handler for the loaded event of the grid:

<Grid x:Name="theGrid" KeyDown="Grid_KeyDown_1" Focusable="True" Loaded="TheGrid_OnLoaded">

And then force focus in your code behind:

    private void TheGrid_OnLoaded(object sender, RoutedEventArgs e)
    {
        theGrid.Focus();
    }

Your keydown event will work after that. Hope it helps.

like image 177
Ouarzy Avatar answered Sep 18 '22 12:09

Ouarzy


I tried using the Focus Method too, to no avail, until I set the Focusable property to true ( It was default to False. )

like image 29
joseph taylor Avatar answered Sep 19 '22 12:09

joseph taylor


I had the same issue with a Universal Windows Platform (UWP) app. I attached the event to a grid in XAML but it would only work when the focus was on the TextBox. I found the answer (not just a workaround) on MSDN: https://social.msdn.microsoft.com/Forums/en-US/56272bc6-6085-426a-8939-f48d71ab12ca/page-keydown-event-not-firing?forum=winappswithcsharp

In summary, according to that post, the event won't fire because when focus to the TextBox is lost, it's passed higher up so the Grid won't get it. Window.Current.CoreWindow.KeyDown should be used instead. I've added my event handlers to the page loaded event like this:

private void Page_Loaded(object sender, RoutedEventArgs e)
{
    Window.Current.CoreWindow.KeyDown += coreWindow_KeyDown;
    Window.Current.CoreWindow.KeyUp += CoreWindow_KeyUp;
}

This works as expected for me.

like image 24
Nagev Avatar answered Sep 20 '22 12:09

Nagev