none
Ignore KeyDown event when TextBox has focus

    Question

  • Hi,

     

    I am doing a keyboard shortcut system for my application. I allow my users to set shortcuts such as a single key (like 'P' for example) or use keys such as Enter, Backspace, etc. However, this will clash when the user is entering text into a TextBox or such.

    So I want to, inside my two functions that captures KeyDown and KeyUp event for the main window, detect if the user is entering text in TextBox. In psuedo code:

    if (CurrentlyFocusedControl == TextBox)

    It would make it easier for me in future coding since I would be able to just add more TextBoxes without having to manually, for each entry, add logic to "turn off" keyboard shortcuts while in edit mode.

     

    Thanks!

    Tuesday, October 26, 2010 4:36 PM

Answers

  • Hi Ephracis,

    you can test using the FocusManager's GetFocusedElement method:

    bool fFocusedControlIsTextBox = FocusManager.GetFocusedElement(this).GetType().Equals(typeof(TextBox));
    


    Cheers,
    Olaf
    http://blogs.intuidev.com
    • Marked as answer by Ephracis Friday, October 29, 2010 12:20 AM
    Tuesday, October 26, 2010 4:47 PM

All replies

  • Hi Ephracis,

    you can test using the FocusManager's GetFocusedElement method:

    bool fFocusedControlIsTextBox = FocusManager.GetFocusedElement(this).GetType().Equals(typeof(TextBox));
    


    Cheers,
    Olaf
    http://blogs.intuidev.com
    • Marked as answer by Ephracis Friday, October 29, 2010 12:20 AM
    Tuesday, October 26, 2010 4:47 PM
  • Oh, or - as an alternative:

    bool fFocusedControlIsTextBox = Keyboard.FocusedElement.GetType().Equals(typeof(TextBox));
    


    Cheers,
    Olaf
    http://blogs.intuidev.com
    Tuesday, October 26, 2010 4:49 PM
  • In WPF keyboard shortcut system is implemented by "CommandsBinding".
    You can register your own command or you can use any of existing commands classes:
    • ApplicationCommand
    • NavigationCommand
    • EditingCommand
    • ComponentCommand
    • MediaCommand
    More about commands you can find here
    http://msdn.microsoft.com/en-us/library/ms752308.aspx


    Tuesday, October 26, 2010 5:28 PM
  • Thanks for the replies. They both worked. :D

     

    In WPF keyboard shortcut system is implemented by "CommandsBinding".
    You can register your own command or you can use any of existing commands classes:
    • ApplicationCommand
    • NavigationCommand
    • EditingCommand
    • ComponentCommand
    • MediaCommand
    More about commands you can find here
    http://msdn.microsoft.com/en-us/library/ms752308.aspx


    Too bad I am almost finished with my own custom implementation then. However, does CommandsBinding only work in MenuItems or can I assign anything? Like change focus to that control, or change text on control a and set visibility to hidden on control b?

    Also, is it possible to present an interface for changing these shortcuts inside the app with different profiles?

     

    Anyway, as I said, the implementation is almost done now. All I have left is to convert strange keys such as OemTilde and OemComma to normal characters. It would be sooo easy if everyone just used the same keyboard layout. :P

    Tuesday, October 26, 2010 7:05 PM
  • U can use CommandBindings even for a button.
    CommandBinding was made to have less code. Many of application have a lot of functions, most of this function can be executed from few places... For example: Lets have a look on InternetExplorer.
    We can open new tab on 3 ways:
    1. We can press on menu File -> New tab
    2. We can press Ctrl+T
    3. We can press a extra button which is next to last tab.

    In WinForms we had to have 2 events. One for the menu and one for the button. Each event raised a function.

    Command is something like the event is already the function. Se we can directyly bind menuitem to the same function what a button or something else :D

    Tuesday, November 23, 2010 12:42 AM