none
Application.KeyBinding.Count return 0 RRS feed

  • Question

  • hi everybody,when i want to get Application.KeyBindings.Count,it returns 0,please help me

    code:

                object objKey;

                Doc.Application.CustomizationContext = Doc.Application.NormalTemplate;
                string strCommand = "Heading 1";
                Word._Global wrdGlobal = null;

                object wdKeyAlt = Word.WdKey.wdKeyAlt;
                object wdKeyH = WdKey.wdKeyH;

                int keycode = Doc.Application.BuildKeyCode(WdKey.wdKeyControl, ref wdKeyAlt, ref wdKeyH, ref missing);
                Doc.Application.KeyBindings.Add(WdKeyCategory.wdKeyCategoryStyle, strCommand, keycode, ref missing, ref missing);


    amir
    Thursday, January 13, 2011 9:15 AM

Answers

  • Hi amir

    The collection of KeyBindings refers only to customizations made to the default set. The Built-in keybindings are not included in the collection.

    There's a way to do this, but it's fairly complicated and much too long for me to retype here. Best I can do is give you an overview of the logic.

    Word has Application.FindKey object that let's you "look up" any key combination (using the BuildKeyCode parameter). Look at the object model help to see how that works.

    Check the KeyCategory of FindKey for a key combination and if it's not equal to -1 the key is assigned to something. If you then try to use this key combination with KeyBindings and it gives an error / returns null, you know you have a built-in key assignment.

    So, you can loop through all possible key combinations in FindKey to pick these out. BuildKeyCode will accept the numbers 1 through 254 as corresponding to the "normal keyboard". To test these in combination with Shift, Control and Alt, use the Word.WdKey enumerators wdKeyAlt, wdKeyControl and wdKeyShift.

    A built-in key assignment can be turned off using the Disable  method of FindKey. It's Clear method will re-enable the assignment.

    When you do this, you want to be very sure that you don't disable the keyboard commands permanently for the user. That's what will happen if you specify CustomizationContext = NormalTemplate, as your sample code does. Better to specify ActiveDocument for this and just do it for every document.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Bessie Zhao Tuesday, January 18, 2011 2:47 AM
    Thursday, January 13, 2011 11:22 AM
    Moderator

All replies

  • how can i (disable) all shortcut key in word,foe example end user cant cut a selection by pressing ctrl+x

    amir
    Thursday, January 13, 2011 5:39 AM
  • As you seem to have already found Keybindings, I'm going to merge this with that thread...
    Cindy Meister, VSTO/Word MVP
    Thursday, January 13, 2011 10:12 AM
    Moderator
  • Hi amir

    The collection of KeyBindings refers only to customizations made to the default set. The Built-in keybindings are not included in the collection.

    There's a way to do this, but it's fairly complicated and much too long for me to retype here. Best I can do is give you an overview of the logic.

    Word has Application.FindKey object that let's you "look up" any key combination (using the BuildKeyCode parameter). Look at the object model help to see how that works.

    Check the KeyCategory of FindKey for a key combination and if it's not equal to -1 the key is assigned to something. If you then try to use this key combination with KeyBindings and it gives an error / returns null, you know you have a built-in key assignment.

    So, you can loop through all possible key combinations in FindKey to pick these out. BuildKeyCode will accept the numbers 1 through 254 as corresponding to the "normal keyboard". To test these in combination with Shift, Control and Alt, use the Word.WdKey enumerators wdKeyAlt, wdKeyControl and wdKeyShift.

    A built-in key assignment can be turned off using the Disable  method of FindKey. It's Clear method will re-enable the assignment.

    When you do this, you want to be very sure that you don't disable the keyboard commands permanently for the user. That's what will happen if you specify CustomizationContext = NormalTemplate, as your sample code does. Better to specify ActiveDocument for this and just do it for every document.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Bessie Zhao Tuesday, January 18, 2011 2:47 AM
    Thursday, January 13, 2011 11:22 AM
    Moderator