locked
Problem with Focus and KeyDown, KeyUp Events RRS feed

  • Question

  • Hi,

    Im having a problem obtaining focus on my object, and thus my key up and key down events arent working properly.

    The structure i have:

    Main Page, creates a data grid, which then creates and instance of a usercontrol i created, and sends in the data grid instance to which i apply key down, up, got focus, lost focus events with in the usercontrol.

    I tested other events such as mouse enter, leave, mouseleftbuttondown/up and they all work perfectly, the above 4 dont.

     

    Any help or suggestions is appreciated. Thank you.

    Saturday, May 31, 2008 1:38 AM

Answers

  • I have seen the similar problem before. Could you test this by putting a Textbox in the area( a Canvas or Grid?) you are trying to get focus. Click the Textbox will have the focus to that area. Then key event should work.  I think there is a bug for Panel to get focus if there is no clickable control in it., and I still don't know how to workaround this problem without putting a control in the Panel.


     

     

    Saturday, May 31, 2008 4:32 PM
  • Thats exacly what i ended up doing, it works for me for now, maybe if later they change it, ill change my code. So thank you.

    Saturday, May 31, 2008 6:25 PM
  • There's another way around this, I just blogged on it here:

    http://jedi-hacker.spaces.live.com/blog/cns!1F5D68261F592B63!132.entry 

    Sunday, June 1, 2008 9:02 PM
  • Eloff,

    I just tried your solution and it works. Thanks for post this solution. It's certainly a better workaround than putting an empty textbox or button just for the purpose of getting focus to the Panel.

    By the way, Just wrap the panel with <ContentControl IsTabStop="True"> is enough. I did not need to change any code or move the Key event handler to the ContentControl. The only thing I found is I need to do Focus() call in the Page_Loaded event handler instead in the Page Consturctor.

    void Page_Loaded(object sender, RoutedEventArgs e)
    {
          this.Focus();
    }

     


     

    Sunday, June 1, 2008 9:56 PM

All replies

  • I have seen the similar problem before. Could you test this by putting a Textbox in the area( a Canvas or Grid?) you are trying to get focus. Click the Textbox will have the focus to that area. Then key event should work.  I think there is a bug for Panel to get focus if there is no clickable control in it., and I still don't know how to workaround this problem without putting a control in the Panel.


     

     

    Saturday, May 31, 2008 4:32 PM
  • Thats exacly what i ended up doing, it works for me for now, maybe if later they change it, ill change my code. So thank you.

    Saturday, May 31, 2008 6:25 PM
  • There's another way around this, I just blogged on it here:

    http://jedi-hacker.spaces.live.com/blog/cns!1F5D68261F592B63!132.entry 

    Sunday, June 1, 2008 9:02 PM
  • Eloff,

    I just tried your solution and it works. Thanks for post this solution. It's certainly a better workaround than putting an empty textbox or button just for the purpose of getting focus to the Panel.

    By the way, Just wrap the panel with <ContentControl IsTabStop="True"> is enough. I did not need to change any code or move the Key event handler to the ContentControl. The only thing I found is I need to do Focus() call in the Page_Loaded event handler instead in the Page Consturctor.

    void Page_Loaded(object sender, RoutedEventArgs e)
    {
          this.Focus();
    }

     


     

    Sunday, June 1, 2008 9:56 PM
  • Thank you

    Saturday, June 7, 2008 12:01 PM
  • Nice workaround.  Thanks for the tip.  It seems to have equivalent performance to using a hidden control inside the panel. 

    Friday, June 13, 2008 5:52 AM
  •  I can't get this to work anymore with Beta 2. Anyone know?

    Tuesday, June 24, 2008 3:16 PM
  • It's still working in beta 2. I just tried.

    Just make sure you set the focus in the Page Loaded event, putting it in the constructor won't work.

    Page_Loaded(object sender, RoutedEventArgs e)

    {

        this.Focus();

    }

    Tuesday, June 24, 2008 3:40 PM
  •  Ok, I got it working. Thanks.

    Tuesday, June 24, 2008 4:06 PM
  • I still can't get this to work in beta 2.  Here's what I've done:

    • Created new Silverlight app
    • In Page.xaml, surrounded the default Grid control with a ContentControl and set IsTabStop="True"
    • In Page.xml.cs constructor, wired up Page_Loaded and Page_KeyDown events.
    • In Page_Loaded, added "this.Focus();"
    • Put a break point on Page_KeyDown
    • Debug the app, and the Page_KeyDown event doesn't fire unless I click somewhere in the blank area of the screen.

    What am I doing wrong here?  If I wire up a Page_MouseMove event, it works just fine without me clicking to give focus first.  Thanks for your help!

    Andy

    Monday, July 14, 2008 11:39 AM
  • Try this:

     void Page_Loaded(object sender, RoutedEventArgs e)
            {
                System.Windows.Browser.HtmlPage.Plugin.Focus();    

                this.Focus();
            }

    If your page is the only User Control in the Silverlight control. Then you need to make sure the Siverlight Obejct Tag is in focus first.

     


    Monday, July 14, 2008 11:54 AM
  •  That fixed it -  thank you!!

    Monday, July 14, 2008 12:23 PM