Answered TextBox.OnKeyDown Event

  • Tuesday, November 18, 2008 3:58 AM
     
     
    Hi all,

    I am currently using .Net Framework 3.5 and designing my application in WPF. I noticed that for a textbox, the OnKeyDown event no longer detects spacebar and backspace keyboard button clicks. I checked back with the .Net Framework 2.0 WinForm textbox OnKeyDown event and it detects that spacebar and backspace keyboard button clicks.

    I checked the OnKeyUp event for the WPF and noticed that it does detect the spacebar and backspace keyboard button clicks, but not the OnKeyDown event. Is this a bug?

    Thanks!

    Best Regards
    • Moved by nobugzMVP, Moderator Tuesday, November 18, 2008 4:33 AM wpf q (.NET Base Class Library から Windows Presentation Foundation (WPF) へ移動)
    •  

All Replies

  • Wednesday, November 19, 2008 1:53 AM
     
     
    Hi,

    I noticed that the PreviewKeyDown event does detect the spacebar keyboard input but not the KeyDown event for the WPF application. I am currently using it now and it works fine for me but I was just wondering why the KeyDown event does not detect it.

    Thanks.
  • Wednesday, November 19, 2008 3:58 AM
     
     Answered
    PreviewKeyDown sets the Handled to true (Window converts the spacebar keydown to click event, as far as i know) on the event if it encounters keys such as "space bar".

    You can ovveride that by attaching a KeyDown Event Handler in code as shown.

    tx.AddHandler(TextBox.KeyDownEvent, new KeyEventHandler(TextBox_KeyDown),true);

    The third argument tells that you wish to invoke the event handler even for the handled events. May not be a good idea, but it works in your case. You might want to read more about handledEventsToo.

    Research and Development Imageright Inc. http://krishnabhargav.blogspot.com
    • Proposed As Answer by Krishna Vangapandu Wednesday, November 19, 2008 11:43 PM
    • Marked As Answer by Marco Zhou Monday, November 24, 2008 10:25 AM
    •