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 change keyboard's key behavior?

How to change keyboard's key behavior?

Scheduled Pinned Locked Moved Solved General and Desktop
keyboardkeyboard mappinkeypresseventqmap
7 Posts 4 Posters 4.4k 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.
  • N Offline
    N Offline
    Nouriemm
    wrote on last edited by
    #1

    Experts,
    My question is pretty simple:
    I want to press a keyboard's key (on a physical keyboard of-course) and have a custom output.
    For example I want to press R on my keyboard and I want to have ® instead.

    1. Is it possible to do such thing generally all over a Qt program?
    2. If not, is it possible to do it for a widget (something like QLineEdit)?

    Thanks in advance

    ? 1 Reply Last reply
    0
    • N Nouriemm

      Experts,
      My question is pretty simple:
      I want to press a keyboard's key (on a physical keyboard of-course) and have a custom output.
      For example I want to press R on my keyboard and I want to have ® instead.

      1. Is it possible to do such thing generally all over a Qt program?
      2. If not, is it possible to do it for a widget (something like QLineEdit)?

      Thanks in advance

      ? Offline
      ? Offline
      A Former User
      wrote on last edited by A Former User
      #2

      @Nouriemm Hi, you can use an event filter for this. Look at this: http://doc.qt.io/qt-5/qobject.html and search for "KeyPressEater". That's an example that's already pretty close to what you want to do. Should be easy to adopt it to your needs.

      N 1 Reply Last reply
      1
      • ? A Former User

        @Nouriemm Hi, you can use an event filter for this. Look at this: http://doc.qt.io/qt-5/qobject.html and search for "KeyPressEater". That's an example that's already pretty close to what you want to do. Should be easy to adopt it to your needs.

        N Offline
        N Offline
        Nouriemm
        wrote on last edited by
        #3

        @Wieland
        Thanks for the answer.
        let us assume that we have done the part of key grabbing like this:

            bool MainWindow::eventFilter(QObject *target, QEvent *event)
           {
                if (event->type() == QEvent::KeyPress) {
                    QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
                    if (keyEvent->key() == Qt::Key_R) {
                        /******what are we suppose to do here ******/
                        return true;
                    }
                }
        
            return false; }
        

        But, the question is still remained: How to map Qt::Key_R to ® instantly as a general output of the physical keyboard?

        D 1 Reply Last reply
        0
        • N Nouriemm

          @Wieland
          Thanks for the answer.
          let us assume that we have done the part of key grabbing like this:

              bool MainWindow::eventFilter(QObject *target, QEvent *event)
             {
                  if (event->type() == QEvent::KeyPress) {
                      QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
                      if (keyEvent->key() == Qt::Key_R) {
                          /******what are we suppose to do here ******/
                          return true;
                      }
                  }
          
              return false; }
          

          But, the question is still remained: How to map Qt::Key_R to ® instantly as a general output of the physical keyboard?

          D Offline
          D Offline
          Devopia53
          wrote on last edited by Devopia53
          #4
          You can use a sendEvent() simplify. See my sample code:
          
          
          bool MainWindow::eventFilter(QObject *o, QEvent *e)
          {
              if (o == ui->lineEdit) {
                  if (e->type() == QEvent::KeyPress) {
                      QKeyEvent   *k = static_cast<QKeyEvent*>(e);
          
                      if (k->key() == Qt::Key_R) {
                          if (k->text().compare("®") != 0) {
                              QKeyEvent   ke(k->type(), k->key(), k->modifiers(), QString("®"), k->isAutoRepeat(), k->count());
          
                              QApplication::sendEvent(ui->lineEdit, &ke);
                              return true;
                          }
                      }
                      return false;
                  }
                  else {
                      return false;
                  }
              }
              else {
                  return MainWindow::eventFilter(o, e);
              }
          }
          1 Reply Last reply
          1
          • N Offline
            N Offline
            Nouriemm
            wrote on last edited by Nouriemm
            #5

            Thanks to @Devopia53 and @Wieland the problem has solved.
            I have polished the @Devopia53 code to something like this to make it more generic:

              bool MainWindow::eventFilter(QObject *o, QEvent *e){
            
              if (e->type() == QEvent::KeyPress) {
                QKeyEvent   *k = static_cast<QKeyEvent*>(e);
            
                if (k->key() == Qt::Key_R  &&  k->text() != "®") {
                        QKeyEvent   ke(k->type(), k->key(), k->modifiers(), QString("®"), k->isAutoRepeat(), k->count());
                        QApplication::sendEvent(o, &ke);
                        return true;
                    }
            }
            return false;
            

            }

            Just wondering if my code breaks any rule of programming; Specially by not using
            ** return MainWindow::eventFilter(o, e);**

            Cheers

            ValentinMicheletV 1 Reply Last reply
            0
            • N Nouriemm

              Thanks to @Devopia53 and @Wieland the problem has solved.
              I have polished the @Devopia53 code to something like this to make it more generic:

                bool MainWindow::eventFilter(QObject *o, QEvent *e){
              
                if (e->type() == QEvent::KeyPress) {
                  QKeyEvent   *k = static_cast<QKeyEvent*>(e);
              
                  if (k->key() == Qt::Key_R  &&  k->text() != "®") {
                          QKeyEvent   ke(k->type(), k->key(), k->modifiers(), QString("®"), k->isAutoRepeat(), k->count());
                          QApplication::sendEvent(o, &ke);
                          return true;
                      }
              }
              return false;
              

              }

              Just wondering if my code breaks any rule of programming; Specially by not using
              ** return MainWindow::eventFilter(o, e);**

              Cheers

              ValentinMicheletV Offline
              ValentinMicheletV Offline
              ValentinMichelet
              wrote on last edited by ValentinMichelet
              #6

              @Nouriemm said:

              Just wondering if my code breaks any rule of programming; Specially by not using
              ** return MainWindow::eventFilter(o, e);**

              When you override event handling, I think calling the parent's method at the end is a good practice, since Qt works on the event and treats special cases (according to the OS for instance) that you may have forgotten.

              1 Reply Last reply
              0
              • N Offline
                N Offline
                Nouriemm
                wrote on last edited by
                #7

                Thank you all,
                I am going to mark this discussion as solved;
                However, Any more comments would be much appreciated;

                1 Reply Last reply
                0

                • Login

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