This is a migrated thread and some comments may be shown as answers.

PreviewKeyDown Event Fires Multiple Times Before KeyUp Event

3 Answers 124 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Robert
Top achievements
Rank 1
Robert asked on 09 Dec 2013, 05:17 PM
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

3 Answers, 1 is accepted

Sort by
0
Petya
Telerik team
answered on 11 Dec 2013, 04:30 PM
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 >>
0
Robert
Top achievements
Rank 1
answered on 11 Dec 2013, 04:48 PM
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.
0
Petya
Telerik team
answered on 16 Dec 2013, 09:31 AM
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 >>
Tags
RichTextBox
Asked by
Robert
Top achievements
Rank 1
Answers by
Petya
Telerik team
Robert
Top achievements
Rank 1
Share this question
or