How to catch enter key

From Qt Wiki
Revision as of 15:41, 14 January 2015 by Maintenance script (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

English Deutsch

How to catch enter key events

Overview

There are many different situations where you can use the enter key, e.g. to start a search action. But implementing something like this is not that easy – Qt catches enter keys before you even get the event.

Solution

Fortunately, Qt allows to reimplement the general event catching method. You need a new class with a method like this:

That’s everything:

Now, we have to implement the method:

That was quiet fast – so here is a detailled explanation:

Key pressed?

First, we check if any key was pressed. If not, it is a event that has nothing to do with keys – and Qt should handle it:

Convertion

We got a QEvent as a parameter. To read out which key was pressed, we need to convert the QEvent to a QKeyEvent:

Enter/Return or another key?

That’s it. Now we only have to check whether it was “our” enter key or another key we are not interested in:

Finally, we can install our event handler:

Categories: