使用C ++ \ Qt

时间:2018-03-28 22:49:51

标签: c++ windows qt

我正在尝试从我的Qt应用程序中的另一个应用程序的活动窗口中选择文本。 在Linux上,我只需在QClipboard模式下使用Selection。 在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, &copyText, sizeof(INPUT));

// Press the "C" key
copyText.ki.wVk = 'C';
copyText.ki.dwFlags = 0; // 0 for key press
SendInput(1, &copyText, sizeof(INPUT));

Sleep(50);

// Release the "C" key
copyText.ki.wVk = 'C';
copyText.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &copyText, sizeof(INPUT));

// Release the "Ctrl" key
copyText.ki.wVk = VK_CONTROL;
copyText.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &copyText, sizeof(INPUT));
Sleep(50);

但是这个黑客行为不正常 - 有时候我没有选择。我认为它可能是由一个使用此代码调用函数的热键引起的,并且在此代码运行时仍会按下某些键。如何检查QKeySequenceEdit中是否未按下每个键?或者如何检查是否没有按下任何一个键? 或者是否有更简单的方法从Windows上的活动窗口获取所选文本?

1 个答案:

答案 0 :(得分:0)

我获取所选文字的解决方案:

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方法获得本机密钥代码。