I'm trying to disable Alt-Enter in Word 2003 using the following code:
private void DisableWordShortcutKeys()
{
object objKey;
/* ALT + keys */
keys = new List<WdKey>()
{
WdKey.wdKeyReturn,
(WdKey)37, (WdKey)38, (WdKey)39, (WdKey)40 //37=left, 38=up, 39=right, 40=down
};
foreach (WdKey key in keys)
{
objKey = key;
DisableWordShortcutKey(_application.BuildKeyCode(WdKey.wdKeyAlt, ref objKey, ref _missing, ref _missing));
}
}
private void DisableWordShortcutKey(int keyCode)
{
_application.KeyBindings.Add(WdKeyCategory.wdKeyCategoryDisable, "", keyCode, ref _missing, ref _missing);
}
Can anyone confirm that this is the way to do what I'm trying to do? Thanks in advance Claus | | Claus Jessing Thursday, October 15, 2009 1:37 PM | Hi Claus
OK, our messages are staggering...
Disabling a keyboard command from a VSTO project is one thing. Assigning it to do something else is another matter, entirely, and requires either VBA code or keyboard hooking from the Windows API.
the approach suggested in my previous message, using events, could still work but, as I say, you'd need to link to a VBA macro as the keyboard trigger.
However, I would then suggest a keyboard combination other than one standardized in windows. Cindy Meister, VSTO/Word MVP- Marked As Answer byClaus Jessing Friday, October 16, 2009 9:38 AM
-
| | Cindy Meister Friday, October 16, 2009 8:03 AM | Does it work? I do not think ALT+ENTER is a short-cut key - but is rather core functionality of the editing surface ...
Misha | | Misha Shneerson - MSFT Thursday, October 15, 2009 2:52 PM | And even if it does work - I would probably think very hard about disabling ALT+ENTER ... This will affect all documents opened in Word. The question that needs to be asked - how users are supposed to type in a newline w/o creating a new paragraph (that's what ALT+ENTER does). Misha | | Misha Shneerson - MSFT Thursday, October 15, 2009 2:56 PM | Well... according to this: http://support.microsoft.com/kb/290938
Redo or Repeat CTRL+Y or F4 or ALT+ENTER
But the symptoms we are seeing could suggest that it has not been disabled entirely... | | Claus Jessing Thursday, October 15, 2009 3:41 PM | Hi Misha
Actually, Alt+Enter does not do anything, by default, in the Word application. - Enter creates a new paragraph - Shift + Enter creates a new line - Ctrl+Enter creates a new page
Cindy Meister, VSTO/Word MVP | | Cindy Meister Thursday, October 15, 2009 3:57 PM | Hi Claus
If it works, then you're doing it correctly. It's been a while since I worked with this, so I don't have the details memorized, but using KeyBindings is the way to disable a key combination.
A couple of remarks, though:
1. You must be very careful to specify the CustomizationContext. This is the place where the change is to be stored. If you do not specify, Word will save it in whatever document it deems "appropriate", which may not be what you expect or want. Since you don't tell us exactly what your project is and why you'd want to disable these five keyboard combinations, I can't offer any advice or opinion on the appropriate CustomizationContext, except that you're likely to end up making a mess of the user's configuration if you aren't very careful.
2. Alt+Enter (and the other combinations you show) aren't assigned to anything in the Word UI, by default, so I don't know why you want to disable these combinations.
3. Be aware that, if the keyboard combinations are mapped outside of Word (in Windows) that changing the internal Word KeyBinding probably won't affect how these combinations are working. For that, you'd likely need to use the Windows API.
Cindy Meister, VSTO/Word MVP | | Cindy Meister Thursday, October 15, 2009 4:03 PM | Thanks Cindy but I beg to differ... as mentioned above the shortcut list says that Alt-Enter performs a Redo or Repeat. Try the following: Start Word 2003 end enter "Microsoft Word" Place the cursor between Microsoft and Word Press Alt-Enter What I get is a line like this: "Microsoft Microsoft WordWord" Depending on typos and new lines you might get a slightly different result... Claus
| | Claus Jessing Friday, October 16, 2009 5:58 AM | Since you don't tell us exactly what your project is and why you'd want to disable these five keyboard combinations, I can't offer any advice or opinion on the appropriate CustomizationContext, except that you're likely to end up making a mess of the user's configuration if you aren't very careful.
To elaborate a bit: Our client want Alt-Enter to select the current Word paragraph and upon subsequent Alt-Enter continue to select the following Word paragraph. The code to support this behavior is this (called when pressing Alt-Enter):
private void SelectNextParagraph()
{
object missing = Type.Missing;
Range range = _taskPane.LdeDocument.Selection.Range;
//if the current paragraph is selected
if (range.Start == _taskPane.LdeDocument.Selection.Paragraphs[1].Range.Start &&
range.End == _taskPane.LdeDocument.Selection.Paragraphs[_taskPane.LdeDocument.Selection.Paragraphs.Count].Range.End)
{
Paragraph nextParagraph = _taskPane.LdeDocument.Selection.Paragraphs[_taskPane.LdeDocument.Selection.Paragraphs.Count].Next(ref missing);
//next paragraph must be within the same node
if (nextParagraph.Range.XMLParentNode.BaseName == range.XMLParentNode.BaseName)
range.End = nextParagraph.Range.End;
}
else
{
range = _taskPane.LdeDocument.Selection.Paragraphs[1].Range;
}
range.Select();
}
What we are seeing is this: When marking a word Bold followed by Alt-Enter selects the paragraphs as expected but also marks the selected paragraphs Bold - which we are absolutely not expecting. If no formatting (bold, italic etc) is performed everything works just fine. It's the last line (range.Select();) that of cause causes the range to be selected but also performs the format. I'm puzzled... Have a nice weekend! Claus | | Claus Jessing Friday, October 16, 2009 7:12 AM | Hi Claus
Aha, an older, somewhat obscure windows shortcut... I never used that one.
OK, I'm looking at Word 2007, but I'm guessing the behavior is the same in earlier versions, especially since this (probably) comes from Windows.
If I disable the keyboard command, then it's disabled for the entire application, not the current CustomizationContext. But as soon as I (for example) create a new document Word "forgets" the disabling.
From my experience, I think you can probably use some events (such as NewDocument and DocumentChange) to selectively enable/disable the command. Cindy Meister, VSTO/Word MVP | | Cindy Meister Friday, October 16, 2009 7:53 AM | Hi Claus
OK, our messages are staggering...
Disabling a keyboard command from a VSTO project is one thing. Assigning it to do something else is another matter, entirely, and requires either VBA code or keyboard hooking from the Windows API.
the approach suggested in my previous message, using events, could still work but, as I say, you'd need to link to a VBA macro as the keyboard trigger.
However, I would then suggest a keyboard combination other than one standardized in windows. Cindy Meister, VSTO/Word MVP- Marked As Answer byClaus Jessing Friday, October 16, 2009 9:38 AM
-
| | Cindy Meister Friday, October 16, 2009 8:03 AM | ...or keyboard hooking from the Windows API...
...which is exactly what we have done. We then disable all the shortcuts (via KeyBindings) we are assigning new functionality to. We have changed Alt-Enter to Ctrl-M as a solution. Not the preferred solution but it now works. :-/ Thanks for your effort Claus | | Claus Jessing Friday, October 16, 2009 9:05 AM |
|