How can I skip a keypress after auto-inserting a character?

SyntaxEditor for WPF Forum

Posted 8 years ago by Craig - Varigence, Inc.
Version: 11.1.0545
Avatar
When using a tool like Resharper in Visual Studio, it has the ability to auto insert delimiters. Furthermore, if you insert a closing delimiter but then type it, Resharper doesn't add the second one.

To be clear, let's say I type: (
An end parenthesis is inserted: () with the caret between them.
If I then type ), it's ignored since the matching end parenthesis is already present.

While I have the auto-insert delimiter behavior implemented locally, I also want to skip an end delimiter when it's typed immediately after an auto-insert. As I don't see that implemented in a sample in the Sample Browser, I was wondering if you have a recommendation for the best way to implement this.

One option would be to delete the end parenthesis and replace it with the typed character. But that will create an extra undo item on the stack. I'd like to simply listen for the appropriate text change and ignore it (so the extra keypress isn't inserted at all). Can that be done? Is there a better approach I'm missing?

Thanks,

-Craig

Comments (3)

Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Craig,

It's probably best to maintain some sort of state to indicate when this scenario occurs. You might be able to store the state object in the view's Properties. Then watch the text changing events for Typing text changes. Actually you could just look at the text changing event arg's TypedText to see if it's ")" and is being inserted at the offset at which the last one was auto-inserted, and if so, cancel the event and reset your state object.


Actipro Software Support

Posted 8 years ago by Craig - Varigence, Inc.
Avatar
When you say cancel the event, do you mean listen to the SyntaxEditor's Document's DocumentTextChanging event and set its e.Cancel property to true? That'll stop the typed keys from be appended to the document's text?

-Craig
Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Yes, exactly.


Actipro Software Support

The latest build of this product (v2019.1 build 0683) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.