Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Windows 10 on-screen keyboard in QT app

I am trying to bring up the Windows on-screen keyboard for an application developed with QT/C++. I currently have a custom on-screen keyboard, but it does not look very nice at all screen sizes, so I want to use the Windows native one. I want to bring the keyboard up automatically when the input focus is on a text box. I am new to QT, but not to C++. I have checked a few other similar questions, but it seems those solutions have not been much help.

Edit: The below solutions do not bring the keyboard up. They compile with no errors, but they do not actually bring up the osk.

void MainWindow::on_Button_released()
{
    ui->Button->setChecked(true);

    //Attempt 1
    //ShellExecute( NULL, NULL, L"osk.exe", NULL, NULL, SW_SHOW );

    //Attempt 2
    /* QObject *parent;
         QString program = "./osk.exe";
         QStringList arguments;
         //arguments << "-b" << "-t" << "input.txt";

         QProcess *myProcess = new QProcess(parent);
         myProcess->start(program);//, arguments);
    */

    //Attempt 3
    /* QProcess *process = new QProcess(this);
    QString file = QDir::homepath + "/tabtip.exe";
    process->start(file);
    */

    //Attempt 4
    /* 
    QProcess::execute ("start C:\\Windows\\System32\\osk.exe");
    */


    //Attempt 5
    system ("start C:\\Windows\\System32\\osk.exe");

}

**Attempt 5 specifically gives an error detailing that the file cannot be found, and it suggests checking that the correct path has been specified. I already verified the path, and osk.exe runs just fine -just not from within my application.

like image 499
Kristina Gauthier Avatar asked May 24 '18 12:05

Kristina Gauthier


1 Answers

To display it, just launch osk.exe.

To get rid of it (*) (warning - undocumented behaviour):

HANDLE hWnd = FindWindowW (L"OSKMainClass", NULL);
if (hWnd)
{
     PostMessage (hWnd, WM_SYSCOMMAND, SC_CLOSE);
  // Or if you prefer:
  // PostMessage (hWnd, WM_SYSCOMMAND, SC_MINIMIZE, 0);
}

I know this works on Windows 10, and probably back as far as Windows 7.

(*) I am indebted to @zett42 for this information (but I'm claiming the credit :)


Edit: It has come to my attention that launching OSK requires a little finesse. You can do it like this:

#include <shellapi.h>

void *was;
Wow64DisableWow64FsRedirection (&was);
ShellExecuteA (NULL, "open", "osk.exe", NULL, NULL, SW_SHOWNORMAL);
Wow64RevertWow64FsRedirection (was);

The Wow64... calls are only needed in a 32 bit app, and you need to link with shell32.lib.

like image 100
Paul Sanders Avatar answered Oct 23 '22 03:10

Paul Sanders