Click here to Skip to main content
16,012,316 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to copy the word entered by a user as soon as the user presses space(Just like the spell checker in word editors do).
I'm using the following code:

private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            ConsoleKeyInfo info = Console.ReadKey();
            if (info.KeyChar == ' ')
            {
                Word.Range rng = this.Application.ActiveDocument.Words.Last;
                rng.Select();
                rng.Copy();
                String input = Clipboard.GetText(TextDataFormat.Text);
            }
        }


I'm getting the following error:
An exception of type 'System.InvalidOperationException' occurred in mscorlib.dll but was not handled in user code.
for the following line:
ConsoleKeyInfo info = Console.ReadKey();


Please help.
Posted
Comments
Tomas Takac 19-Oct-14 5:43am    
MSDN says ReadKey throws InvalidOperationException in case input has been redirected from some stream other than the console. Is that your case?
Tejas Shastri 19-Oct-14 5:49am    
no no.. As soon as i run the prog the MS word application launches and throws an error. FYI I'm using MS office 2013 and VS 2013 ultimate.
Maciej Los 19-Oct-14 5:44am    
Word does not corresponds with Console application!
Tejas Shastri 19-Oct-14 5:50am    
oh :( Then how will i read the keystroke? :(
Maciej Los 19-Oct-14 6:08am    
Please, see my answer.

You need to know that MS Word addin does not corresponds with Console aplication[^].

If you want to interact with MS Word, you need to use its objects, methods and events.

As per i understand your requirements, you need to use one of MS word/document event (i'm not sure which one, if any). Please, see:
Application Events[^]
Document Events[^]
Using Events with the Document Object[^]
Writing application event procedures[^]

[EDIT]
I think you're looking for KeyPress event[^]
But i need to warn you: there isn't simple way to achieve that. Please see this[^]
 
Share this answer
 
v3
Comments
Tejas Shastri 19-Oct-14 6:19am    
There's no application event or document event which can detect keystrokes :( Please advice.. Can I create an event handler??
Maciej Los 19-Oct-14 6:24am    
See updated question (after EDIT part).
Tejas Shastri 19-Oct-14 8:40am    
I actually found a code.. can you help me modify the code to use for space bar?
Maciej Los 19-Oct-14 9:12am    
I'm sorry, but it doesn't work like this... ;(
You need at least try to do your job. When you get stuck, come back here and ask detailed question.
Tejas Shastri 19-Oct-14 13:42pm    
I'm not even able to understand where the key code is mentioned.:/
Cindy Meister, VSTO/Word MVP, on VSTO Microsoft forum, in 2011 re Word 2010:
More exactly, I said there's nothing in the Word API or VSTO that can pick up keystrokes. There's nothing built-in that you can use in a VSTO project to pick up the keystrokes.

You'd either need to use VBA (which isn't part of VSTO) or you'd need to use the Windows API - both of which are not an integral part of VSTO.

In Word, keystrokes can only be mapped to VBA macros, not to other code.
[^].

I would investigate if you can apply a GlobalHook to the Word Application, but, I have no idea is this is possible within the context of an Add-In.
 
Share this answer
 
Comments
Maciej Los 19-Oct-14 15:26pm    
In other words, there is only one way to catch key events: via API in VBA code.
+5!
Tejas Shastri 20-Oct-14 0:15am    
can you check if this helps?

http://tech.pro/tutorial/760/winforms-accessing-mouse-and-keyboard-state
Tejas Shastri 20-Oct-14 0:16am    
Is there a way to get info about the key state directly from the system?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900