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. Get string value from modified key press
Forum Updated to NodeBB v4.3 + New Features

Get string value from modified key press

Scheduled Pinned Locked Moved General and Desktop
10 Posts 3 Posters 11.8k 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.
  • U Offline
    U Offline
    uranusjr
    wrote on 7 Oct 2011, 09:08 last edited by
    #1

    Hi,

    I'm working on a key event related function. I use keyPressEvent to read in the keyboard inputs. The key() and string() values work fine on single press key events, but there is a problem.

    When I press a key combination (control + c, for example), I receive two events. One has key = Qt::Key_Control, which is good. But the other has key = Qt::Key_C and string = QString(). Is there a way to convert the Qt::Key_* values to QString, QChar, or something similar?

    A simple search in the docs yielded nothing, but I really wish I could find something here before diving into mapping all keys by myself (and there's the shift key modifier to consider...Aaaarrrrhh).

    Thanks in advance.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on 7 Oct 2011, 09:24 last edited by
      #2

      The int key code values from Qt::Key_Space (0x20) until Qt::Key_AsciiTilde (0x7f) map directly to the respective ASCII codes of the characters. So it should be safe to do this:

      @
      int key = event->key();
      QString keyString;
      if(key >= Qt::Key_Space && key <= Qt::Key_AsciiTilde) {
      // handle ASCII char like keys
      keyString = QString( QChar(key) );
      } else {
      // handle the other keys here...
      }
      @

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • U Offline
        U Offline
        uranusjr
        wrote on 7 Oct 2011, 09:54 last edited by
        #3

        Thanks, that worked...a bit. :-(

        Seems that key() doesn't handle shift key at all (not just the upper/lower cases), so I need to manually extinguish "!" and "1", "{" and "[", etc.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on 7 Oct 2011, 10:08 last edited by
          #4

          Yes. It handles the raw keys from the keyboard and does not interpret it into the (possible) character that is composed of the key and the modifiers. For that purpose the text() method exists.

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • U Offline
            U Offline
            uranusjr
            wrote on 7 Oct 2011, 11:49 last edited by
            #5

            But text() is not available when modifiers present. :-( Well, I guess I'll just manually mapped all non-alphanumeric keys on my keyboard. But how about keyboard layouts? £ instead of $ above 4 on british keyboard, ¥ instead of \ on Japanese keyboards, etc. Is there a way for me to detect this on run-time?

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on 7 Oct 2011, 11:53 last edited by
              #6

              Is there any way you could just use QKeySequence instead of trying to handle the raw events yourself?

              1 Reply Last reply
              0
              • U Offline
                U Offline
                uranusjr
                wrote on 7 Oct 2011, 12:16 last edited by
                #7

                I am building something similar to a terminal simulator that sends things like ctrl + ! to the server. The format the server takes (for the above example) is a CTRL, and then an ASCII '!', so I need to get the result of the keyboard output to send (through a TCP socket).

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  goetz
                  wrote on 7 Oct 2011, 12:42 last edited by
                  #8

                  That depends on the modifiers. What text do you expect for Ctrl-C?

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  1 Reply Last reply
                  0
                  • U Offline
                    U Offline
                    uranusjr
                    wrote on 7 Oct 2011, 13:03 last edited by
                    #9

                    For alphabets the cases don't matter, so for ctrl + c both CTRL + 'C' and CTRL + 'c' are fine. I don't participate in the server side programming, so I'm not sure what they intend to do with non-ASCII keys. (Most likely they don't care -- the system we are building is not for public use, and we use mostly US-layout keyboards. My plan now is to ignore the problem and leave it for the testing team to worry about. :-p)

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      goetz
                      wrote on 7 Oct 2011, 13:51 last edited by
                      #10

                      Programming by delegation ... good idea for the weekend approaching ;-P

                      http://www.catb.org/~esr/faqs/smart-questions.html

                      1 Reply Last reply
                      0

                      1/10

                      7 Oct 2011, 09:08

                      • Login

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