Discussion:
Capture Keystrokes (CTRL+SOMETHING)
(too old to reply)
Johnny Fugazzi
2005-09-19 17:16:18 UTC
Permalink
Doing some research on replacing a current mess of excel workbooks with
embeded macros with assemblies and projects developed using Visual Studio
Tools for Office.

Looking at VSTO 2003 and Excel 2003.

Is there a way to capture keystrokes as events in VSTO code behind? I would
like to handle these events in the code behind and completely get rid of
some VBA macros that are triggered by "ctrl+something" hotkey combinations.

Not really interested in having a command bar button to kick off the
functionality since that require the user to stop doing what they are doing
and use the mouse to click on the button.
Geoff Darst(MSFT)
2005-09-20 16:12:20 UTC
Permalink
Post by Johnny Fugazzi
Looking at VSTO 2003 and Excel 2003.
Is there a way to capture keystrokes as events in VSTO code behind? I would
like to handle these events in the code behind and completely get rid of
some VBA macros that are triggered by "ctrl+something" hotkey combinations.
Hi,

Unfortunately, Excel doesn't source key change events so there isn't a simple way to put code behind them. Probably the approach involving the least
amount of work would be to use System.Windows.Forms.NativeWindow to subclass Excel so you could watch WM_KEYDOWN/WM_KEYUP messages and
make whatever calls you needed in response to the keystrokes. Since Excel doesn't expose its hwnd, you'll need to use the FindWindow or FindWindowEx
apis. Be sure to use the lpWindowName parameter so your code locates the right window in the case where multiple instances of Excel are running.

Sincerely,

Geoff Darst
Microsoft VSTO Tools
--
This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated.
--------------------
Johnny Fugazzi
2005-09-20 19:15:00 UTC
Permalink
Appreaciate the tips.

That is not something I have tried before, can you offer pointers on where
to find out more information of monitoring the Excel Window?

Also, is it possible to assign a key-combo or a hot-key to a
commandbar-button that is created and docked using VSTO code behind?
Post by Geoff Darst(MSFT)
Post by Johnny Fugazzi
Looking at VSTO 2003 and Excel 2003.
Is there a way to capture keystrokes as events in VSTO code behind? I would
like to handle these events in the code behind and completely get rid of
some VBA macros that are triggered by "ctrl+something" hotkey
combinations.
Hi,
Unfortunately, Excel doesn't source key change events so there isn't a
simple way to put code behind them. Probably the approach involving the
least
amount of work would be to use System.Windows.Forms.NativeWindow to
subclass Excel so you could watch WM_KEYDOWN/WM_KEYUP messages and
make whatever calls you needed in response to the keystrokes. Since Excel
doesn't expose its hwnd, you'll need to use the FindWindow or FindWindowEx
apis. Be sure to use the lpWindowName parameter so your code locates the
right window in the case where multiple instances of Excel are running.
Sincerely,
Geoff Darst
Microsoft VSTO Tools
--
This posting is provided "AS IS" with no warranties, and confers no
rights. Use of included script samples are subject to the terms specified
at
http://www.microsoft.com/info/cpyright.htm
Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
--------------------
Geoff Darst(MSFT)
2005-09-21 20:30:33 UTC
Permalink
Post by Johnny Fugazzi
That is not something I have tried before, can you offer pointers on where
to find out more information of monitoring the Excel Window?
Here is a good article that explains subclassing in general principles (which is what NativeWindow does): http://msdn.microsoft.com/library/default.asp?
url=/library/en-us/dnwui/html/msdn_subclas3.asp .

Documentation on NativeWindow can be found here: http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/cpref/html/frlrfsystemwindowsformsnativewindowclasstopic.asp

All of this is going to assume that you have a good understanding of Windows programming. If you don't Charles Petzold's Programming Windows
(whatever his latest version is) is a good place to start. It should be mentioned that if you don't have a good understanding of Windows programming this
will be a somewhat non-trivial undertaking.
Post by Johnny Fugazzi
Also, is it possible to assign a key-combo or a hot-key to a
commandbar-button that is created and docked using VSTO code behind?
Not easily. Normally you would assign keybindngs to macros via the Application.MacroOptions property. However, AFIK this won't work with VSTO. You
would either need to create some Vba wrappers that you would bind to (and which would call back into your VSTO code) or you are back to trying to hook
keyboard messages. If you want to try the former, here is a good article by Paul Stubbs that will show you how to set up your code so that it is callable from
Vba: http://blogs.msdn.com/pstubbs/archive/2004/12/31/344964.aspx . Note that using Vba from within a VSTO project isn't something we want to
encourage since it is far from ideal from a security perspective. However, for limited scenarios such as this one, it may be a necessary solution.

Hope that helps,

Geoff Darst
Microsoft VSTO Tools
--
This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated.
--------------------
Loading...