Qt World Summit: Submit your Presentation

How to get other window's text input cursor position ?

  • I want to make an input tool with QT, but I don't know how to get other application's text input cursor posion.
    Any idea ? Please help ~

  • Lifetime Qt Champion

    @Soul Can you please explain better what you want to do?
    Do you want to implement a virtual keyboard?
    There is already one implemented with Qt.

  • Hi @jsulm
    For example: Window A has an textEdit control . When my mouse clicked on the textEdit , there where be a cursor indicate I can input text. My Qt-aplication job is to show a window fllow the cursor. Just like the IME .

  • Lifetime Qt Champion

    @Soul Would this Window A be a window of your application or another application (another process)?

  • @jsulm The window A is another process.

  • Lifetime Qt Champion

    @Soul I don't think you can do this with Qt. You will need to use OS native APIs.

  • @jsulm Do you have any suggestions ? The application only runs on windows.

  • Lifetime Qt Champion

    @Soul Windows API, but I'm not expert in this area.

  • Hi @Soul,
    To get the textCursorPos or the caretPos you'll have to use native OS apis as @jsulm had already mentioned. I can help you to achieve that on Windows only, let have a look at this sample :

    • First open a new Qt Application project, choose a title, and then let everything as default.
    • Then go to the .pro file and add this line : QT += concurrent widgets,
      this line doesn't have any relation with our goal, it's just needed for the sample that I will describe above. If you're not familiar with QtConcurrent or Qt&Multithreading, I recommend you to learn about that, especially QtConcurrent.
    • And finally make the main.cpp look like that, and hit RUN:
    #include "mainwindow.h"
    #include <QApplication>
    #include <QDebug>
    #include <QtConcurrent>
    #include <windows.h>
    int main(int argc, char *argv[])
        QApplication a(argc, argv);
             AttachThreadInput(GetCurrentThreadId(),GetWindowThreadProcessId(GetForegroundWindow(),0), true);
             POINT caretPos, cCaretPos;
             qDebug() << caretPos.x << "," << caretPos.y << " - " << cCaretPos.x << "," << cCaretPos.y;
        return a.exec();

    If you want a further explanation of all of that, replay YES.

Log in to reply