I am developing a MS-Word addon in which my code has to get access to the letters the user is entering through the keyboard.
private void ThisDocument_Startup(object sender, System.EventArgs e)
{
this.SelectionChange += new SelectionEventHandler(ThisDocument_SelectionChange);
}
void ThisDocument_SelectionChange(object sender, SelectionEventArgs e)
{
MessageBox.Show(e.Selection.Text);
}
I think the SelectionChange event can give me the text but the event is not raised at keypress, Is there any way to trigger the event at keypress? Also if there is a more straightforward way to do it or an open source project that give the functionality, it would be welcome.
If you press and hold down an ANSI key, the KeyDown and KeyPress events alternate repeatedly (KeyDown, KeyPress, KeyDown, KeyPress, and so on) until you release the key, and then the KeyUp event occurs.
The keypress event is fired when a key that produces a character value is pressed down. Examples of keys that produce a character value are alphabetic, numeric, and punctuation keys. Examples of keys that don't produce a character value are modifier keys such as Alt , Shift , Ctrl , or Meta .
The keydown event is fired when a key is pressed. Unlike the deprecated keypress event, the keydown event is fired for all keys, regardless of whether they produce a character value. The keydown and keyup events provide a code indicating which key is pressed, while keypress indicates which character was entered.
Select your TextBox control on your Form and go to Properties window. Select Event icon on the properties window and scroll down and find the KeyDown event from the list and double click the Keydown Event. The you will get the KeyDown event in your source code editor.
Microsoft doesn't expose a key down event natively, but there's a workaround.
I implemented keyboard checking with help from the article linked below:
http://www.switchonthecode.com/tutorials/winforms-accessing-mouse-and-keyboard-state
This gives you a static method called IsKeyDown, implementing and invoking a delegate you can subscribe to should be fairly straight forward.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With