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. How to press key from keyboard automatically with Qt
Forum Updated to NodeBB v4.3 + New Features

How to press key from keyboard automatically with Qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 3.0k Views 1 Watching
  • 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
    esconda
    wrote on last edited by esconda
    #1

    Hello everyone,

    I have a simple question.How can i able to press key automatically with qt program(for example enter or tab key)

    I investigated several examples that consist of key events.But unfortuantely they are just checking pressed key on the keyboard.

    like this one ;

    bool MyWidget::event(QEvent *event)
    {
         if (event->type() == QEvent::KeyPress) {
             QKeyEvent *ke = static_cast<QKeyEvent *>(event);
             if (ke->key() == Qt::Key_Tab) {
                 // special tab handling here
                 return true;
             }
         } else if (event->type() == MyCustomEventType) {
             MyCustomEvent *myEvent = static_cast<MyCustomEvent *>(event);
             // custom event handling here
             return true;
         }
         return QWidget::event(event);
    

    }

    I think above example checks if the tab key is pressed by user.But I would like to write a code that press a specified key (I will choose )automatically by program from keyboard.

    Simply I don't want to check pressed key, I want to press a key automatically with program

    Could you please write me an example for this operation.

    Thank you in advance.

    jsulmJ 1 Reply Last reply
    0
    • E esconda

      Hello everyone,

      I have a simple question.How can i able to press key automatically with qt program(for example enter or tab key)

      I investigated several examples that consist of key events.But unfortuantely they are just checking pressed key on the keyboard.

      like this one ;

      bool MyWidget::event(QEvent *event)
      {
           if (event->type() == QEvent::KeyPress) {
               QKeyEvent *ke = static_cast<QKeyEvent *>(event);
               if (ke->key() == Qt::Key_Tab) {
                   // special tab handling here
                   return true;
               }
           } else if (event->type() == MyCustomEventType) {
               MyCustomEvent *myEvent = static_cast<MyCustomEvent *>(event);
               // custom event handling here
               return true;
           }
           return QWidget::event(event);
      

      }

      I think above example checks if the tab key is pressed by user.But I would like to write a code that press a specified key (I will choose )automatically by program from keyboard.

      Simply I don't want to check pressed key, I want to press a key automatically with program

      Could you please write me an example for this operation.

      Thank you in advance.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @esconda https://stackoverflow.com/questions/24322602/qt-sending-keypressevent

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • E Offline
        E Offline
        esconda
        wrote on last edited by esconda
        #3
        QKeyEvent * eve1 = new QKeyEvent (QEvent::KeyPress,Qt::Key_CapsLock,Qt::NoModifier,"a");
        QKeyEvent * eve2 = new QKeyEvent (QEvent::KeyRelease,Qt::Key_CapsLock,Qt::NoModifier,"a");
        
        
        if(eve1->key()==Qt::Key_CapsLock){
            if(eve2->key()==Qt::Key_CapsLock){
            QString qstr = QKeySequence(eve2->key()).toString();
            ui->lineEdit->setText(qstr);
            QMessageBox::information(this,"Error","pressed key ");}
        }
        

        I wrote above code according to given example, but unfortunately it does not press to capslock key automatically.It shows capslock string in line edit.Bu unfortunately it does not activate the capslock from keyboard.

        Is there anything wrong?

        jsulmJ 1 Reply Last reply
        0
        • E esconda
          QKeyEvent * eve1 = new QKeyEvent (QEvent::KeyPress,Qt::Key_CapsLock,Qt::NoModifier,"a");
          QKeyEvent * eve2 = new QKeyEvent (QEvent::KeyRelease,Qt::Key_CapsLock,Qt::NoModifier,"a");
          
          
          if(eve1->key()==Qt::Key_CapsLock){
              if(eve2->key()==Qt::Key_CapsLock){
              QString qstr = QKeySequence(eve2->key()).toString();
              ui->lineEdit->setText(qstr);
              QMessageBox::information(this,"Error","pressed key ");}
          }
          

          I wrote above code according to given example, but unfortunately it does not press to capslock key automatically.It shows capslock string in line edit.Bu unfortunately it does not activate the capslock from keyboard.

          Is there anything wrong?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @esconda Well, you cannot do this with Qt. The example shows how to simulate key presses in your app - you can't do "real" key presses this way. Maybe there is something in OS APIs (Windows/Linux/MacOS).

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • Pablo J. RoginaP Offline
            Pablo J. RoginaP Offline
            Pablo J. Rogina
            wrote on last edited by
            #5

            @esconda you may want to rely to some OS specific code like the one proposed here.

            Upvote the answer(s) that helped you solve the issue
            Use "Topic Tools" button to mark your post as Solved
            Add screenshots via postimage.org
            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            1

            • Login

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