Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to raise an event on MS word Keypress

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.

like image 362
yohannist Avatar asked Oct 24 '12 15:10

yohannist


People also ask

How do you key press an event?

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.

Is event a keypress?

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 .

Which event is called when key is pressed?

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.

How do you keypress an event in C#?

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.


1 Answers

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.

like image 81
Soeholm Avatar answered Oct 13 '22 06:10

Soeholm