How to get other window's text input cursor position ?
-
wrote on 25 Jul 2018, 03:37 last edited by aha_1980
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 ~ -
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 ~@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 .@Soul Would this Window A be a window of your application or another application (another process)?
-
@Soul I don't think you can do this with Qt. You will need to use OS native APIs.
-
@Soul Windows API, but I'm not expert in this area.
-
wrote on 27 Jul 2018, 23:26 last edited by
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.
1/9