Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why it is impossible to simulate Key hold?
QtWS25 Last Chance

Why it is impossible to simulate Key hold?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Engelard
    wrote on 13 Aug 2020, 15:16 last edited by Engelard
    #1

    For the last few days i'm trying to make my app simulate keypress. First i went this trivial way:

    QKeyEvent *event = new QKeyEvent(QKeyEvent::KeyPress, Qt::Key_G, Qt::NoModifier, "A");
    QApplication::sendEvent(ui->textEdit, event);
    

    Btw i don't get that thing, what is the point specifying Qt::Key, if it'll take last QString parameter as desired "key" for pressing.

    Then i found QTest library, which have a shorter, nicer, but in fact completely the same function:

    QTest::keyPress(ui->textEdit,Qt::Key_T);
    

    And also tried windows.h SendInput function, which is nearly the same, with that difference that it can do keypress to the whole system, not only it's app windows/widgets.

    So none of that C++ functions with keyPress does not actually "press", they tap, click one single time, and that's it, so i came here with hope, that someone actually knows why it is impossible to simulate hold, like:

    QTest::keyPress(ui->textEdit,Qt::Key_T);
    QThread::sleep(4);
    QTest::keyRelease(ui->textEdit,Qt::Key_T);
    

    and get "tttttttttttttttttttttttt" instead of single t.

    And maybe even someone know the way how to simulate key hold.

    P.S. cycle tapping frequently in the loop many times not working since that will "rePress" button over and over, so to say, some LMButton can't press on some icon, and pull that aside, it'll loose that immediately.

    C 1 Reply Last reply 13 Aug 2020, 18:55
    0
    • K Offline
      K Offline
      Kent-Dorfman
      wrote on 13 Aug 2020, 18:21 last edited by
      #2

      it's platform dopendent at that point. look at the X11 events, which do in fact trigger both a press and release of a key. If you're using X11 then you can inject the key events using Xwindows.

      1 Reply Last reply
      1
      • E Engelard
        13 Aug 2020, 15:16

        For the last few days i'm trying to make my app simulate keypress. First i went this trivial way:

        QKeyEvent *event = new QKeyEvent(QKeyEvent::KeyPress, Qt::Key_G, Qt::NoModifier, "A");
        QApplication::sendEvent(ui->textEdit, event);
        

        Btw i don't get that thing, what is the point specifying Qt::Key, if it'll take last QString parameter as desired "key" for pressing.

        Then i found QTest library, which have a shorter, nicer, but in fact completely the same function:

        QTest::keyPress(ui->textEdit,Qt::Key_T);
        

        And also tried windows.h SendInput function, which is nearly the same, with that difference that it can do keypress to the whole system, not only it's app windows/widgets.

        So none of that C++ functions with keyPress does not actually "press", they tap, click one single time, and that's it, so i came here with hope, that someone actually knows why it is impossible to simulate hold, like:

        QTest::keyPress(ui->textEdit,Qt::Key_T);
        QThread::sleep(4);
        QTest::keyRelease(ui->textEdit,Qt::Key_T);
        

        and get "tttttttttttttttttttttttt" instead of single t.

        And maybe even someone know the way how to simulate key hold.

        P.S. cycle tapping frequently in the loop many times not working since that will "rePress" button over and over, so to say, some LMButton can't press on some icon, and pull that aside, it'll loose that immediately.

        C Offline
        C Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on 13 Aug 2020, 18:55 last edited by Chris Kawa
        #3

        @Engelard said:

        Btw i don't get that thing, what is the point specifying Qt::Key, if it'll take last QString parameter as desired "key" for pressing.

        Some letters take couple of keys to be formed. For example Polish letter ć is generated by pressing ALT+C. The first key press is for ALT (Qt::Key_Alt) which has empty text and then another keypress is for C (Qt::Key_C), but because ALT is still pressed the text is now "ć" instead of just "c".

        QThread::sleep(4);

        You need to use a timer to let the event loop going e.g. this works

        QTimer* t = new QTimer(ui->textEdit);
        t->setInterval(100);
        t->start();
        connect(t, &QTimer::timeout, ui->textEdit, [&]()
        {
            QKeyEvent evt(QKeyEvent::KeyPress, Qt::Key_G, Qt::NoModifier, "G");
            QApplication::sendEvent(ui->textEdit, &evt);
        });
        

        Also keep in mind that the next parameter after "G" is the autorepeat. If you're simulating a pressed and held key the first event should have autorepeat set to false and all the next ones to true until next key release event.

        1 Reply Last reply
        4
        • E Offline
          E Offline
          Engelard
          wrote on 13 Aug 2020, 22:45 last edited by
          #4

          Both answers is good, thanks.

          1 Reply Last reply
          1

          3/4

          13 Aug 2020, 18:55

          • Login

          • Login or register to search.
          3 out of 4
          • First post
            3/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved