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 ~ -
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 . -
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); QtConcurrent::run([=]() { forever { AttachThreadInput(GetCurrentThreadId(),GetWindowThreadProcessId(GetForegroundWindow(),0), true); POINT caretPos, cCaretPos; GetCaretPos(&caretPos); GetCaretPos(&cCaretPos); ClientToScreen(GetFocus(),&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.