locked
KeyDown or KeyUp not Working RRS feed

  • Question

  • I have the following in XAML:
    <Page x:Class="IUS.PaginaInicial" Title="Jurisprudencia y Tesis Aisladas" 
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
          xmlns:jc="clr-namespace:JaStDev.ControlFramework.Controls;assembly=JaStDev.ControlFramework" 
          WindowTitle="Jurisprudencia y Tesis Aisladas" 
          ShowsNavigationUI="False" MinWidth="600" 
          MinHeight="500" jc:Help.Topic="Inicio" 
          KeyDown ="Page_KeyDown" 
          jc:Help.HelpFile="ius.chm" FontFamily="Arial"

    And it will never enter to my event, no matter if is KeyDown or KeyUp event the one i'm trying to capture.
    The code in .cs:
            private void Page_KeyDown(object sender, KeyEventArgs e) 
            { 
                help.DefaultHelpFile = Constants.ARCHIVO_AYUDA; 
                if (e.Key == Key.F1) 
                { 
                    help.ShowHelp(); 
                } 
            } 

    I tried it with MouseLeftButtonDown (changing the type of EventArgs) and it worked fine, but i need the help to be shown with F1 not with a click.
    Greetings


    Wednesday, November 19, 2008 5:40 PM

Answers

  • for the KeyDown event, the sender is the Window and Page is hosted inside the window. So it does not tunnel  down to the page, somehow.

    Alternative?

    Inside Page Load event handler (Page_Loaded, in my case) attach event handler as shown.

    private void Page_Loaded(object sender, RoutedEventArgs e)  
            {  
                Application.Current.MainWindow.KeyDown += new KeyEventHandler(MainWindow_KeyDown);  
            } 
    But in a multipage scenario, you might have to remove the handler manually, if you are going to another page.
    Research and Development Imageright Inc. http://krishnabhargav.blogspot.com
    Wednesday, November 19, 2008 9:13 PM

All replies

  • I was having a similar problem earlier involving buttons.  In my case, I was trying to use MouseLeftButtonDown/Up with a button, but the Click event kept getting in the way.  What I had to do was use PreviewMouseLeftButtonDown/Up as my event, insert what I wanted to happen in the C# code for the event, and as the last statement in that event function, I set e.Handled = true.  This stopped the event from actually tunneling (I think Preview events are tunneling) all the way to the Click event so that the button behaved the way I wanted it to.

    I'm not sure what events work with Page best...for all I know, you are facing a case similar to mine and have an event similar to the button Click default getting in your way.  If that's the case, I'd suggest setting your event to e.Handled = true within your Page_KeyDown function (and KeyUp).  If that doesn't work, try changing the event you are calling to PreviewKeyDown (and PreviewKeyUp), also making sure to set the event as handled in your C# function.  Hopefully one of those will work.
    Wednesday, November 19, 2008 8:33 PM
  • for the KeyDown event, the sender is the Window and Page is hosted inside the window. So it does not tunnel  down to the page, somehow.

    Alternative?

    Inside Page Load event handler (Page_Loaded, in my case) attach event handler as shown.

    private void Page_Loaded(object sender, RoutedEventArgs e)  
            {  
                Application.Current.MainWindow.KeyDown += new KeyEventHandler(MainWindow_KeyDown);  
            } 
    But in a multipage scenario, you might have to remove the handler manually, if you are going to another page.
    Research and Development Imageright Inc. http://krishnabhargav.blogspot.com
    Wednesday, November 19, 2008 9:13 PM
  • It worked like great... now, like the Fool's Garden used to sing... I wonder how... i wonder why... Greetings
    Wednesday, November 19, 2008 10:05 PM
  •  When using listening to routed events, it is possible to catch  events even if they are marked as handled (the button clicked sample), but thats not the case here. I think that some element in the page needs to be focused as a startingpoint for the event to travel from, then you can catch it from within the page. For example, it would work if you have a textbox that you click on before hitting F1, or if you set it in XAML like 

    FocusManager.FocusedElement="{Binding ElementName=myTextBox}"


    I dont know the main reason why a Window catches all keyhandling even without having focus while a page doesnt, i guess it has something to do with security.
    Hope it helps you some.

    Edit 2009-03-12: I think its the XAML App that catches the event, as ean example, if you open a XAML window from a winform app without setting a focused element, it will not catch the keydown event.
    /Henrik
    Wednesday, November 19, 2008 10:10 PM