Force keyboard in digits mode

charles edouard vidoine 96 Reputation points
2020-08-17T13:28:28.69+00:00

Hello,
with my team we are developing a C++ Windows application which is based on Qt library for the UI part
and on Win32 API for the rest.

The minimum Windows version supported is Windows 8.1.

In the application there are some text fields which accept numbers only. This is done with a Qt Validator.
I'd like to know if it's possible (with Win32 API) to force the Virtual Keyboard to be displayed in "digits" mode when focus is set on one of those text fields?
It would be helpful for users which spend time to always change the keyboard mode.

Regards,
M Vidoine

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,498 questions
{count} votes

Accepted answer
  1. charles edouard vidoine 96 Reputation points
    2020-08-21T13:05:07.87+00:00
     if(lKeyboardElement != NULL)
    {
    VARIANT lVarProp;
    lVarProp.vt = VT_BSTR;
    lVarProp.bstrVal = SysAllocString(L"switchSymbol");
    IUIAutomationCondition* lPCondition;
    HRESULT lResult = mKeyboardDisplayModeManager->CreatePropertyCondition(UIA_AutomationIdPropertyId, lVarProp, &lPCondition);
    if(SUCCEEDED(lResult))
    {
    IUIAutomationElement* lSwithButton = NULL;
    lResult = lKeyboardElement->FindFirst(TreeScope_Subtree, lPCondition, &lSwithButton);
    if(SUCCEEDED(lResult) && lSwithButton)
    {
    IUnknown* lActions = NULL;
    lResult = lSwithButton->GetCurrentPattern(UIA_LegacyIAccessiblePatternId, &lActions);
    if(SUCCEEDED(lResult) && lActions)
    {
    ((IUIAutomationLegacyIAccessiblePattern*)lActions)->DoDefaultAction();
    lActions->Release();
    }
    lSwithButton->Release();
    }
    if (lPCondition != NULL)
    lPCondition->Release();
    }
    VariantClear(&lVarProp);
    lKeyboardElement->Release();
    }
          }
    
    }
    
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. charles edouard vidoine 96 Reputation points
    2020-08-21T08:38:23.997+00:00
    void GTabletInfo::initialize()
     {
         CoInitialize(NULL);
             HRESULT lResult = CoCreateInstance(
                     __uuidof(CUIAutomation), NULL, CLSCTX_INPROC_SERVER,
                     __uuidof(IUIAutomation), 
                     (void**)&mKeyboardDisplayModeManager);
     }
    
    0 comments No comments

  2. charles edouard vidoine 96 Reputation points
    2020-08-21T09:28:26.147+00:00
    IUIAutomationElement* GTabletInfo::findElementByClassName(const wchar_t* aWindowName, IUIAutomationElement* aParent)
     {
         if (mKeyboardDisplayModeManager == NULL || aWindowName == NULL)
             return NULL;
         VARIANT lVarProp;
         lVarProp.vt = VT_BSTR;
         lVarProp.bstrVal = SysAllocString(aWindowName);
         IUIAutomationCondition* lPCondition;
         HRESULT lResult = 
            mKeyboardDisplayModeManager->CreatePropertyCondition(
                             UIA_ClassNamePropertyId, lVarProp, &lPCondition);
         if(SUCCEEDED(lResult))
         {
             IUIAutomationElement* lRootElement = aParent;
             if(lRootElement == NULL)
                 lResult = mKeyboardDisplayModeManager->GetRootElement(
                                            &lRootElement);
          if(SUCCEEDED(lResult) && lRootElement)
             {
                 IUIAutomationElement* lFoundElement = NULL;
                 lResult = lRootElement->FindFirst(
                             TreeScope_Children, lPCondition, &lFoundElement);
                 if(SUCCEEDED(lResult))
                 {
                     VariantClear(&lVarProp);
                     if (lPCondition != NULL)
                         lPCondition->Release();
                     if(aParent == NULL)
                         lRootElement->Release();
                     return lFoundElement;
                 }
                 if(lFoundElement != NULL)
                     lFoundElement->Release();
                 if(aParent == NULL)
                     lRootElement->Release();
             }
             if (lPCondition != NULL)
                 lPCondition->Release();
         }
         VariantClear(&lVarProp);
         return NULL;
     }
    
    0 comments No comments

  3. charles edouard vidoine 96 Reputation points
    2020-08-21T13:03:03.84+00:00
    void GTabletInfo::displayKeyboardDigitsMode()
    {
    if(mKeyboardVisible && mKeyboardDisplayModeManager != NULL)
    {
    IUIAutomationElement* lKeyboardElement = findElementByClassName(L"IPTip_Main_Window");
    if(lKeyboardElement == NULL)
    lKeyboardElement = findElementByClassName(L"IPTip_Main_Window",findElementByClassName(L"Windows.UI.Core.CoreWindow",findElementByClassName(L"ApplicationFrameWindow")));
    
    0 comments No comments