Keyboard navigation on form does not work in Word XP

A

Alex

Hello,

I have a COM add-in for Word written in C#.
The add-in is built against the Word XP PIAs and works in both Word XP and Word 2003.

The add-in displays some .NET Windows Forms to the user.

However, when the add-in is running under Word XP, keyboard navigation between controls on the forms (TAB, arrow keys) does not work.
Same keyboard navigation works as usual then the add-in runs under Word 2003.

Does Word XP filter out the keystrokes?
How can I make it work?


Best wishes,
Alex.
 
P

Peter Huang [MSFT]

Hi,

Currently I am researching the issue and we will reply here with more
information as soon as possible.
If you have any more concerns on it, please feel free to post here.

Thanks for your understanding!

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Peter Huang [MSFT]

Hi Alex,

Sorry for delay response.
I can reproduce the problem at my side. So far I am contacting the related
team and I will reply to you ASAP.
Thanks!

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Peter Huang [MSFT]

Hi

Sorry for the delay response.

According to my test, the Form in the MS Word Add-in does not respond to
any Shortcuts or Navigational keys such as Tab, arrow, escape. To fix the
problem, there are two ways to try:

1. Use Model Winform like below:
fm.ShowDialog()

NOTE: I did not recommend you use the Hook approach because that will hook
into the whole Office Application's message loop which may cause the whole
application to crash, if you did not have much concern about the modal
form, I prefer your choosing the first workaround.
Thanks!
2. Use use a WH_GETMESSAGE hook to capture the keystroke messages and call
the IsDialogMessage API. If IsDialogMessage returns TRUE, then do not pass
the message on to the message pump.

#region Windows Hook
// Win32: SetWindowsHookEx()
[DllImport("user32.dll")]
protected static extern IntPtr SetWindowsHookEx(HookType code, HOOKPROC
func,IntPtr hInstance,int threadID);
[DllImport("user32.dll")]
protected static extern int CallNextHookEx(IntPtr hhook, int code,
IntPtr wParam, IntPtr lParam);
[DllImport("user32")]
static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam,
IntPtr lParam);
[DllImport("user32.dll")]
static extern bool IsDialogMessage(IntPtr hDlg, [In] ref Message lpMsg);

protected IntPtr m_hhook = IntPtr.Zero;

public enum HookType: int
{
WH_GETMESSAGE = 3,
}

public delegate int HOOKPROC(int nCode, IntPtr wParam, IntPtr lParam);
public void SetHook()
{
// set the keyboard hook
m_hhook = SetWindowsHookEx(HookType.WH_GETMESSAGE, new
HOOKPROC(this.MyMessageProc), IntPtr.Zero, (int)
AppDomain.GetCurrentThreadId());
}
public int MyMessageProc(int nCode, IntPtr wParam, IntPtr lParam)
{
const int WM_KEYFIRST = 0x0100;
const int WM_KEYLAST = 0x0108;
const int PM_REMOVE = 1;
Message msg =
(Message)System.Runtime.InteropServices.Marshal.PtrToStructure(lParam,
typeof(Message));
if (nCode >= 0 && PM_REMOVE == (int)wParam)
{
if ((msg.Msg >= WM_KEYFIRST) && (msg.Msg <= WM_KEYLAST))
{
bool i = IsDialogMessage(this.Handle, ref msg);
if (i)
{
msg.Msg = 0;
msg.LParam = IntPtr.Zero;
msg.WParam = IntPtr.Zero;
System.Runtime.InteropServices.Marshal.StructureToPtr(msg, lParam,
true);
}
else
{
}
}
}
return CallNextHookEx(m_hhook, nCode, wParam, lParam);
}
#endregion

Both work well. Let me know if you have any other question.


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

Alex

Hello Peter,

"Peter Huang" said:
According to my test, the Form in the MS Word Add-in does not respond to
any Shortcuts or Navigational keys such as Tab, arrow, escape. To fix the
problem, there are two ways to try:

1. Use Model Winform like below:
fm.ShowDialog()

No can do.
The dialog *must* be modeless.
2. Use use a WH_GETMESSAGE hook to capture the keystroke messages and call
the IsDialogMessage API. If IsDialogMessage returns TRUE, then do not pass
the message on to the message pump.

Sounds dangerous.
Are there no other alternatives?


Best wishes,
Alex.
 
P

Peter Huang [MSFT]

Hi

I think so far we have no other alternative.
I will report the problem to our product team, but because that may need to
change the behavior that Word XP handle the whole message loop which may
break the existing program. I am not sure if and when that will be fixed.

If you still have any concern, please feel free to post here.


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top