Re: Capturing Key Events in Add-In

From: Chad DeMeyer (cjdemeye_at_bechtel.com)
Date: 06/01/04


Date: Tue, 1 Jun 2004 14:35:29 -0700

Mike,

One alternative is to simply replace the built-in commands with your own
code. This can be done by giving a subprocedure the same name as the
built-in command, e.g., "Sub FilePrint", "Sub FilePrintDefault", etc. You
can determine the names of built-in commands by going to Tools|Macro|Macros,
and selecting "Word Commands" in the "Macros in:" dropdown.

If you do this, and click on the Word Command in the list that you want to
replace, you can then select the document or template you are coding in in
the "Macros in:" dropdown and click Create. Word will create a macro in
that VBA project with the default code for the command, which you can
include in your custom code to ensure that no desired functionality is lost.

Regards,
Chad

"Mike Oliver" <michael_s_oliver@yahoo.com> wrote in message
news:3875a72e.0405271848.2b6905a0@posting.google.com...
> I'm trying to capture key events that trigger commands (default or
> custom) in a add-in for Word. I'm not writing a macro, so there is no
> OnKey event that I can tap into.
>
> I've tried to install a low-level keyboard hook, but when I do that,
> Word (Office 2003) crashes as soon as the user hits any key. (See C#
> code below). Am I doing something wrong?
>
> I could inspect any WM_KEYDOWN messages sent to the HWND of the
> document window, but there isn't really an easy way to access the
> underlying HWND (and subclass it) for a document through the object
> model, is there?
>
> What I'm trying to do at a higher level is trap all ways that a user
> can do such commands as Save, Print, Copy, Paste, etc... (I've trapped
> the CommandBar .Click events fine, but the Ctrl+... keyboard
> accelerators are what have me beat currently).
>
> Any help would be appreciated.
>
> Mike Oliver
>
> **code snippet**
>
> [DllImport("kernel32")]
> public static extern int GetCurrentThreadId();
>
> [DllImport(
"user32",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]
> public static extern int SetWindowsHookEx(HookType idHook,HOOKPROC
> lpfn,int hmod,int dwThreadId);
>
> public enum HookType
> {
> WH_KEYBOARD = 2
> }
>
> public delegate int HOOKPROC(int nCode, int wParam, int lParam);
>
> public void SetHook()
> {
> // set the keyboard hook
> SetWindowsHookEx(HookType.WH_KEYBOARD,new
> HOOKPROC(KeyboardProc),0,GetCurrentThreadId());
> }
>
> public int KeyboardProc(int nCode, int wParam, int lParam)
> {
> //Perform your process
> return 0;
> }
>
> public void OnStartupComplete(ref System.Array custom)
> {
> SetHook(); // setting hook will cause crash in Word as soon as user
> hits a key
> }



Relevant Pages

  • Re: [PATCH] Prevent NMI oopser
    ... -static inline void ... * commands. ... -static inline int ... +static inline scb_t * ...
    (Linux-Kernel)
  • Sockets Redirection problem
    ... I am trying to build a simple server that can execute local commands ... executing ls, cd, find and all of the normal unix utility programs. ... int execute; ...
    (comp.os.linux.development.apps)
  • [patch 2/3] s390: add vmcp interface.
    ... Add interface to issue VM control program commands. ... * the caller of __cpcmd has to ensure that the response buffer is below 2 GB ... +static int vmcp_open ...
    (Linux-Kernel)
  • Re: Reintroducing fish, the friendly interactive shell
    ... Kurt Swanson skrev: ... the writers of all the commands to implement this would be infeasible. ... specific switch, in which case val and flag must also be 0. ... int has_arg; ...
    (comp.unix.shell)
  • Re: Re: i686 hang on boot in userspace
    ... afterwards I will try to bisect the revisions you mentioned. ... int i, ddir; ... /* We have room on the queue for more commands. ...
    (Linux-Kernel)