locked
How to get current editor info in TSF

    Question

  • How to get current editor infomation in TSF(Text Service Framework), such as current input box is only for password or number or normal text? And another question is how to get current Capslock state from TFS?
    • Edited by Nick_Zhang Tuesday, August 7, 2012 6:15 AM
    Tuesday, August 7, 2012 6:12 AM

Answers

  • Regarding the question, “How to get current editor infomation in TSF(Text Service Framework), such as current input box is only for password or number or normal text?”

    There is no way to determine if the current input box is only for passwords from TSF.  But you can determine if the text is numeric.  Please see, the  ITfInputScope  interface, where it states

    IS_PASSWORD

    Indicates a password. IS_PASSWORD is not supported and may be altered or unavailable in the future.

    NoteIS_PASSWORD only indicates the password; it doesn't provide any security around the password. All passwords fields should have text services disabled to maintain password secrecy, and therefore it is not valid to have a password field with an IS_PASSWORD input scope.

    IS_NUMBER

    Indicates numbers, including commas, negative sign, and decimal. For United States locations, the following conditions are enforced.

    • The thousand separator is a comma.
    • The decimal separator is a period.
    • Negative numbers are represented with a hyphen without a space, not with parentheses.

    So this shows that    IS_PASSWORD   is not useful since it is unsupported. As the documentation states, All passwords fields should have text services disabled.  This means that both IMM and TSF will be turned off, which is also the case for controls like buttons, so you cannot get anything through TSF in this scenario.  On the other hand   IS_NUMBER looks like it could be useful as long as it is not used in a password.

    Here’s a sketch of how to obtain it (error handling and clean up not included):

                   

                    // this is supposed to be done within an edit session, i.e. the caller have a pointer

                    // to the focused TSF context and an edit cookie for that context      

                    UINT cInputScopeCount = 0;

                    InputScope* pInputScopes = NULL;

                    ITfRange* pRange;

                    ITfReadOnlyProperty* pProp;

                    ITfInputScope* pInputScope;

                    VARIANT var;

                    TF_SELECTION selection;

                    pITfContext->GetSelection(editCookie, TS_DEFAULT_SELECTION, 1 /*ulCount*/, &selection, &cFetched);

                    pITfContext->GetAppProperty(GUID_PROP_INPUTSCOPE, &pProp);

                    pProp->GetValue(editCookie, selection.range, &var);

                    if (var.vt == VT_UNKNOWN) 

                    { 

                        var.punkVal->QueryInterface(IID_ITfInputScope, (void**)&pInputScope); 

                        pInputScope->GetInputScopes(&cInputScopeCount, &pInputScopes);

                    } 

    You can get the Capslock state  through win32

    GetKeyState(VK_CAPITAL);

    I hope this helps.

    • Marked as answer by Jesse Jiang Friday, August 24, 2012 7:00 AM
    Thursday, August 16, 2012 4:42 PM

All replies

  • Looking into this at the moment. We'll get back to you soon.

    David Lamb

    Wednesday, August 8, 2012 1:35 AM
    Moderator
  • As I want to customize keyboard layout according to the current editor information. Many thanks!
    Wednesday, August 8, 2012 2:52 AM
  • Regarding the question, “How to get current editor infomation in TSF(Text Service Framework), such as current input box is only for password or number or normal text?”

    There is no way to determine if the current input box is only for passwords from TSF.  But you can determine if the text is numeric.  Please see, the  ITfInputScope  interface, where it states

    IS_PASSWORD

    Indicates a password. IS_PASSWORD is not supported and may be altered or unavailable in the future.

    NoteIS_PASSWORD only indicates the password; it doesn't provide any security around the password. All passwords fields should have text services disabled to maintain password secrecy, and therefore it is not valid to have a password field with an IS_PASSWORD input scope.

    IS_NUMBER

    Indicates numbers, including commas, negative sign, and decimal. For United States locations, the following conditions are enforced.

    • The thousand separator is a comma.
    • The decimal separator is a period.
    • Negative numbers are represented with a hyphen without a space, not with parentheses.

    So this shows that    IS_PASSWORD   is not useful since it is unsupported. As the documentation states, All passwords fields should have text services disabled.  This means that both IMM and TSF will be turned off, which is also the case for controls like buttons, so you cannot get anything through TSF in this scenario.  On the other hand   IS_NUMBER looks like it could be useful as long as it is not used in a password.

    Here’s a sketch of how to obtain it (error handling and clean up not included):

                   

                    // this is supposed to be done within an edit session, i.e. the caller have a pointer

                    // to the focused TSF context and an edit cookie for that context      

                    UINT cInputScopeCount = 0;

                    InputScope* pInputScopes = NULL;

                    ITfRange* pRange;

                    ITfReadOnlyProperty* pProp;

                    ITfInputScope* pInputScope;

                    VARIANT var;

                    TF_SELECTION selection;

                    pITfContext->GetSelection(editCookie, TS_DEFAULT_SELECTION, 1 /*ulCount*/, &selection, &cFetched);

                    pITfContext->GetAppProperty(GUID_PROP_INPUTSCOPE, &pProp);

                    pProp->GetValue(editCookie, selection.range, &var);

                    if (var.vt == VT_UNKNOWN) 

                    { 

                        var.punkVal->QueryInterface(IID_ITfInputScope, (void**)&pInputScope); 

                        pInputScope->GetInputScopes(&cInputScopeCount, &pInputScopes);

                    } 

    You can get the Capslock state  through win32

    GetKeyState(VK_CAPITAL);

    I hope this helps.

    • Marked as answer by Jesse Jiang Friday, August 24, 2012 7:00 AM
    Thursday, August 16, 2012 4:42 PM