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 5 Jan 2016, 20:24 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 5 Jan 2016, 20:43
    0
    • N Nouriemm
      5 Jan 2016, 20:24

      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 5 Jan 2016, 20:43 last edited by A Former User 1 May 2016, 20:44
      #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 5 Jan 2016, 22:24
      1
      • ? A Former User
        5 Jan 2016, 20:43

        @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 5 Jan 2016, 22:24 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 6 Jan 2016, 03:52
        0
        • N Nouriemm
          5 Jan 2016, 22:24

          @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 6 Jan 2016, 03:52 last edited by Devopia53 1 Jun 2016, 04:05
          #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 6 Jan 2016, 08:39 last edited by Nouriemm 1 Jun 2016, 08:41
            #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

            V 1 Reply Last reply 6 Jan 2016, 10:07
            0
            • N Nouriemm
              6 Jan 2016, 08:39

              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

              V Offline
              V Offline
              ValentinMichelet
              wrote on 6 Jan 2016, 10:07 last edited by ValentinMichelet 1 Jun 2016, 10:07
              #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 6 Jan 2016, 13:59 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

                1/7

                5 Jan 2016, 20:24

                • Login

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