Button is not disabled when keyboard is hooked RRS feed

  • Question

  • Hi ,Using keyboard hook , I'm handling the press of F3 in a c# word add-in , to call the functionality of a button_click event in the add-in

    The thing is , all the statements in that button-click event is processed except for a line in which i am disabling the button , i tried debugging techniques through the code , it showed that this button.Enabled == false !!

    any clues ?

    Monday, January 5, 2015 8:09 AM

All replies

  • Hi Eng.Sarah,

    Could you please share some code with us? If it contains confidential information, you could just make a sample for us to reproduce the problem.

    Thanks very much!

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, January 6, 2015 9:24 AM
  • Hello Eng,

    Starting from Word 2007 the Ribbon UI is used instead of command bars. To disable a particular Ribbon button at runtime you need to use a callback where you return a Boolean value (disabled or not).

    C#: bool GetEnabled(IRibbonControl control)
    VBA: Sub GetEnabled(control As IRibbonControl, ByRef enabled)
    C++: HRESULT GetEnabled([in] IRibbonControl *pControl, [out, retval] VARIANT_BOOL *pvarfEnabled)
    Visual Basic: Function GetEnabled(control as IRibbonControl) As Boolean

    Then, when you need to refresh your control(s) you can call the Invalidate or InvalidateControl methods of the IRibbonUI interface.

    You can read more about the Ribbon UI in the following series of articles:

    Customizing the 2007 Office Fluent Ribbon for Developers (Part 1 of 3)

    Customizing the 2007 Office Fluent Ribbon for Developers (Part 2 of 3)

    Customizing the 2007 Office Fluent Ribbon for Developers (Part 3 of 3)

    Tuesday, January 6, 2015 9:46 AM
  • Hi Caillen

    No problem , i set the window hook on the ribbon start-up , then in the hooking class i instantiate the ribbon object in order to get the button_click event handler i need :

      private const int WH_KEYBOARD = 2;
      public static void SetHook()
                _hookID = SetWindowsHookEx(WH_KEYBOARD, _proc, IntPtr.Zero, (uint)AppDomain.GetCurrentThreadId());


    then i call the event handler in the hooking class :

                       MyRibbonClass objRibbon = new MyRibbonClass();
                       objRibbon.btnCollectFigures_Click(null, null);
                       actionTime = DateTime.Now;

    here's the event handler :

    public void btnCollectFigures_Click(object sender, RibbonControlEventArgs e)
            //some process object I'm checking on
            if (process != null)
            Thread thread = new Thread(new ThreadStart(PerformCollecting));
            thread.IsBackground = false;
        catch (Exception)
            btnCollectFigures.Enabled = true;


    Tuesday, January 6, 2015 1:48 PM
  • You need to use callbacks and call the Invalidate* method instead.
    Tuesday, January 6, 2015 2:11 PM
  • can you provide some code snippets Eugene ?? it would help a lot!

    Thank you

    Wednesday, January 7, 2015 12:30 PM
  • do you have any clue why would all the statements be executed except for the one that disables the button ??

    this just drives me crazy !!

    i disable it in the PerformCollecting() method i call in the thread shown in the code i shared

    • Edited by Eng.Sarah Wednesday, January 7, 2015 12:40 PM
    Wednesday, January 7, 2015 12:31 PM