[英]Get selected text from current window with C++ \ Qt
我正在尝试从Qt应用程序中另一个应用程序的活动窗口中获取选定的文本。 在Linux上,我仅在“ Selection
模式下使用QClipboard
。 在Windows上,我尝试将Ctrl + C发送到系统:
INPUT copyText;
copyText.type = INPUT_KEYBOARD;
copyText.ki.wScan = 0;
copyText.ki.time = 0;
copyText.ki.dwExtraInfo = 0;
Sleep(200);
// Press the "Ctrl" key
copyText.ki.wVk = VK_CONTROL;
copyText.ki.dwFlags = 0; // 0 for key press
SendInput(1, ©Text, sizeof(INPUT));
// Press the "C" key
copyText.ki.wVk = 'C';
copyText.ki.dwFlags = 0; // 0 for key press
SendInput(1, ©Text, sizeof(INPUT));
Sleep(50);
// Release the "C" key
copyText.ki.wVk = 'C';
copyText.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, ©Text, sizeof(INPUT));
// Release the "Ctrl" key
copyText.ki.wVk = VK_CONTROL;
copyText.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, ©Text, sizeof(INPUT));
Sleep(50);
但是,这种技巧无法正常运行-有时我没有选择。 我认为这可能是由使用此代码调用函数的热键引起的,并且在运行此代码时仍会按下某些键。 如何检查是否未从QKeySequenceEdit中按下每个键? 或者如何检查是否没有一个按键被按下? 还是有一种更简单的方法来从Windows的活动窗口中获取所选文本?
我获得选定文本的解决方案:
QString MainWindow::selectedText()
{
QString selectedText;
#if defined(Q_OS_LINUX)
selectedText = QApplication::clipboard()->text(QClipboard::Selection);
#elif defined(Q_OS_WIN) // Send Ctrl + C to get selected text
// Save original clipboard data
QVariant originalClipboard;
if (QApplication::clipboard()->mimeData()->hasImage())
originalClipboard = QApplication::clipboard()->image();
else
originalClipboard = QApplication::clipboard()->text();
// Wait until the hot key is pressed
while (GetAsyncKeyState(translateSelectedHotkey->currentNativeShortcut().key) || GetAsyncKeyState(VK_CONTROL)
|| GetAsyncKeyState(VK_MENU) || GetAsyncKeyState(VK_SHIFT))
Sleep(50);
// Generate key sequence
INPUT copyText[4];
// Set the press of the "Ctrl" key
copyText[0].ki.wVk = VK_CONTROL;
copyText[0].ki.dwFlags = 0; // 0 for key press
copyText[0].type = INPUT_KEYBOARD;
// Set the press of the "C" key
copyText[1].ki.wVk = 'C';
copyText[1].ki.dwFlags = 0;
copyText[1].type = INPUT_KEYBOARD;
// Set the release of the "C" key
copyText[2].ki.wVk = 'C';
copyText[2].ki.dwFlags = KEYEVENTF_KEYUP;
copyText[2].type = INPUT_KEYBOARD;
// Set the release of the "Ctrl" key
copyText[3].ki.wVk = VK_CONTROL;
copyText[3].ki.dwFlags = KEYEVENTF_KEYUP;
copyText[3].type = INPUT_KEYBOARD;
// Send key sequence to system
SendInput(4, copyText, sizeof(INPUT));
// Wait for the clipboard to change
QEventLoop loop;
QTimer timer; // Add a timer for the case where the text is not selected
loop.connect(QApplication::clipboard(), &QClipboard::changed, &loop, &QEventLoop::quit);
loop.connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
timer.start(1000);
loop.exec();
// Translate the text from the clipboard if the selected text was not copied
if (timer.isActive())
return QApplication::clipboard()->text();
else
timer.stop();
// Get clipboard data
selectedText = QApplication::clipboard()->text();
// Return original clipboard
if (originalClipboard.type() == QVariant::Image)
QApplication::clipboard()->setImage(originalClipboard.value<QImage>());
else
QApplication::clipboard()->setText(originalClipboard.toString());
#endif
return selectedText;
}
要设置全局快捷方式,我使用了QHotkey 。 从QHotkey中,我使用currentNativeShortcut().key
方法获取本机currentNativeShortcut().key
代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.