none
Find whether focus is in task pane RRS feed

  • Question

  • I have a VBA template that captures keystrokes using a windows hook.The purpose is to fomat characters automatically when the user is typing within certain paragraph styles.

    The problem I have is to determine whether the user is really typing into the document, or into a task pane - for example the navigation pane in Word 2010. Is there a way to tell where the focus is?

    If the focus is in the navigation pane then what happens is as follows. My code looks at the style of the selection (actually the style of selection.range). Unfortunately, doing this seems to set the focus back into the document from the navigation pane. So if the user tries to search for "ab" then the "a" goes into the navigation pane but the "b" goes into the document.

    A simplified version of the relevant part of the code that processes each keystroke is as follows:

    With Selection
        If .Active And Not Application.FocusInMailHeader And .Type = wdSelectionIP Then
            With .Range
                If isInZTextStyles(.Style) Then...

    At this point the focus moves back to the document.

    So I want to add a further guard, in addition to the application.focusinmailheader. But I can't find a corresponding test for whether the focus is in a task pane. Can it be done?

    Thanks

    Anthony

    Tuesday, February 14, 2012 4:13 PM

All replies

  • Hi Anthony,

    Thank you for posting.

    I think that you can try to use an global variable and register the WindowSelectionChange event to set the focus information for the variable. Then, if the textbox on the custom task pane get the focus, then change the varible. Here is some code snippet for you to reference:

        public partial class ThisAddIn
        {
            public string flag = "In the document";
            Microsoft.Office.Tools.CustomTaskPane ctp = null;
    
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                MyUserControl myUc = new MyUserControl();
                ctp = this.CustomTaskPanes.Add(myUc, "MyTaskPane");
                ctp.Visible = true;
    
                this.Application.WindowSelectionChange += new Word.ApplicationEvents4_WindowSelectionChangeEventHandler(Application_WindowSelectionChange);
            }
    
            void Application_WindowSelectionChange(Word.Selection Sel)
            {
                flag = "In the document";
            }       
    
            private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
            {
            }
    
            #region VSTO generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InternalStartup()
            {
                this.Startup += new System.EventHandler(ThisAddIn_Startup);
                this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
            }
            
            #endregion
        }
        public partial class MyUserControl : UserControl
        {
            public MyUserControl()
            {
                InitializeComponent();
            }
    
            private void btnTest_Click(object sender, EventArgs e)
            {
                MessageBox.Show(Globals.ThisAddIn.flag);
                //Selection s = null;
                // s = Globals.ThisAddIn.Application.Selection;
                // if (null != s)
                // {
                //     this.textBox1.Text = "In the custom task pane";
                // }
            }
    
            private void textBox1_Enter(object sender, EventArgs e)
            {
                Globals.ThisAddIn.flag = "In the custom task pane";
            }
        }

    Hope this can help you to apply to your project and just feel free to follow up after you have tried.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, February 16, 2012 10:10 AM
  • Bruce

    Thank you for this suggestion.

    I'm afraid, though, that it isn't really relevant to my problem. What I am looking for is a way of telling whether focus is in any task pane. I don't have a custom task pane (as I said I'm working in VBA) and I need to know when focus is in, for example, the navigation pane. (Actually even that is not the whole story - really I also need to know if it's in a control on the ribbon or a command bar, but that is less likely)

    Following your post I did look at the window selection change event, but as far as I can see it only fires when focus is in the document itself. It would be nice if there were an event that fired when focus left the document, or a test similar to selection.active that would tell me when the focus was elsewhere.

    Thanks

    Anthony


    Anthony

    Sunday, February 19, 2012 5:32 PM
  • Anthony, far as I know, there doesn't exist such event that fired when the focus leaves the document.

    You mentioned the task pane, I thought that you used the vsto custom task pane.In my sample, I use the textbox's enter event to get the information about the focus leaving the document. 

    I will help you involve other experts to help you. Please wait to see whether they can give you some suggestions or hints. There might be some delay about the response. Appreciate your patience.
    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us


    • Edited by Bruce Song Tuesday, February 21, 2012 7:38 AM
    Tuesday, February 21, 2012 7:36 AM
  • Bruce

    I think I have found an answer to my own question, but I would appreciate some advice about it.

    I think I can solve the problem using Win32 API calls. GetFocus() will tell me what window has focus. Now as far as I can see, if the focus is in the document itself the window always has class _WwG. And almost every other window that the focus can be in has a different class such as RICHEDIT60W. The only exception I have found, though perhaps there are others, is the Reviewing pane, which also has class _WwG. On the examples I have looked at the document window always has caption "Microsoft Word Document" and the reviewing pane has caption "Reviewing Pane"

    So my question is this: assuming that you agree that testing the window class of the window with focus is a reasonable approach, can I rely on the window captions? Does a document window always have the caption "Microsoft Word Document", regardless of the language of the operating system and Office? If so that is a complete solution to my problem.

    I'd appreciate any advice

    Thank you

    Anthony


    Anthony

    Saturday, February 25, 2012 9:28 AM