Search code examples
c#keyboard-shortcutskeyboard-eventskeyboard-hook

C# backspace simulator deletes entire word instead of just a letter


I am writing a simple C# program in which when I press Ctrl+G I want my program to automatically delete the character directly left of the cursor in any program (ex: chrome browser, word document, powerpoint document, etc...).

I have installed a global hook for Ctrl+G and it works fine.
I am using a keyboard simulator that I found on Code Project.

My issue is that when I simulate a backspace like:

KeyboardSimulator.KeyPress(Keys.Back);

the entire word is deleted instead of just the character to the left of the cursor.

For example, if I am in a Microsoft Word document with the following line of text:

"Happy new year"

and my cursor is at the end of "year", then when I press Ctrl+G, my program deletes "year" and puts the cursor just to the right of "new" — instead of just deleting the letter "r" in "year".

I have tried other simulators as well with the same result.

Does anyone have a solution or know what I am doing incorrectly?


Solution

  • Apparently, Ctrl+Backspace commonly means delete the entire word in Windows.

    So when you press Ctrl+G, and your hook simulates the Backspace key, the program instead sees Ctrl+Backspace because the Ctrl key is down.