PreviewKeyDown Event Fires Multiple Times Before KeyUp Event

4 posts, 0 answers
  1. Robert
    Robert avatar
    271 posts
    Member since:
    Mar 2011

    Posted 09 Dec 2013 Link to this post

    Hi,

    I'm having trouble with the logic in my code where the PreviewKeyDown Event is firing multiple times before the KeyUp Event is triggered.

    For example:
    I press two keys very quickly but NOT at the exact same time. However, my KeyUp event has some logic that needs to be completed in time before the next PreviewKeyDown should be fired.

    Is there a way that I can prevent a PreviewKeyDown event from firing again until the respective KeyUp event has finished?
    I suppose one solution would be to programmatically Add and Remove the events. I just wondered if there was a flag I can use to check instead.


    You can see from my Debug output below that the PreviewKeyDown is firing twice before the KeyUp events have finished:
    key press: N
    Update Modification - PreviewKeyDown Event (Is Selection Empty: False)
    key press: A
    Update Modification - PreviewKeyDown Event (Is Selection Empty: False)
    KeyUp Event Fired
    KeyUp Event Fired


    Many thanks,

    Rob
  2. Petya
    Admin
    Petya avatar
    975 posts

    Posted 11 Dec 2013 Link to this post

    Hi Rob,

    RadRichTextBox has no additional logic for the mentioned events, they are inherited from UIElement and the result is the same if you subscribe to the events of a TextBox, for example. That said we are not sure what causes the behavior or how you can prevent it.

    If you share some additional details on what you are trying to achieve maybe we will be able to suggest an approach which does not require subscribing to these events.

    I'm looking forward to your reply.

    Regards,
    Petya
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  3. UI for WPF is Visual Studio 2017 Ready
  4. Robert
    Robert avatar
    271 posts
    Member since:
    Mar 2011

    Posted 11 Dec 2013 Link to this post

    Thanks Petya,

    I worked around my problem by subscribing to the CommandExecuting event where e.Command = InsertTextCommand.

    However,  the CommandExecuting event fires twice per key press so I created a simple counter variable and only allowed my code to execute on Count == 1. So far in my tests it seems to work.
  5. Petya
    Admin
    Petya avatar
    975 posts

    Posted 16 Dec 2013 Link to this post

    Hi Rob,

    As far as our observations go the event is only fired once when typing in the control, so I am not sure what causes this in your application. If you are experiencing other difficulties in implementing the desired result please share additional details on your requirements and we will do our best to assist you further.

    Let me know how it goes.

    Regards,
    Petya
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
Back to Top