PDA

View Full Version : Shortcut key events best practices



ducc
17th November 2010, 01:15
Hi all,

I'm completely new to Qt. I've been building small apps, trying to get familiar with how everything works, and I feel like some of the things I'm doing (or want to do) can surely be done better.

The question for this post concerns shortcut keys. I know that QShortcut can be used to attach shortcuts to the class that displays (for example) my window widget.


(void) new QShortcut(Qt::Key_Up, this, SLOT(on_movePositiveButton_pressed()));

And that works nicely. I can also use events, by defining an event filter and then using
movePositiveButton->installEventFilter(this) to make that filter operate on events that occur through that object. That works as expected.

However, there are a few problems with either approach.

1. The QShortcut approach doesn't let you assign an action for when you let go of a key. So if I want the Left arrow key to run the movePlayerLeft() function, I can't run the stopPlayerMotion() after the user has let go of the key. Right? Is there a good way to detect when a key has been let go using this method?

2. On the other hand, I like the QShortcut method because it seems to notice the key press event before it even gets to any given widget. For example, let's say you have several buttons on your interface. By default, the arrow keys are set to move from button to button. If you define a QShortcut for the Up arrow, the Up arrow will no longer select another button. Nice and clean.

3. The Event Filter method is pretty much the ultimate solution for these problems. Build a filter that can handle all the keypresses, and install the filter on all your widgets. But MAN, that's a pain! I don't want to run around, trying to make sure I have the filter installed on every new widget! I don't want to have to keep track of which widgets should have the filter, and which need to keep the default behavior! If I install the filter ONLY in the base class (say, the window widget), then arrow keys (for example) still get caught by the button widgets and never make it to the filter. Which makes arrow keys select widgets instead of firing an event. Annoying.

So how do I design this application? How do you design shortcut events without it becoming a bookkeeping nightmare?

Thanks for the help!

JPNaude
17th November 2010, 07:56
If you want monitor things like how long the user keeps the key in you need to use event filters. QShortcuts won't work for that scenario. I don't exactly understand what your app should do, but you can install an event filter on the QApplication object which will allow you to catch all key events, no matter which widget has focus.

Hope it helps,
Cheers
Jaco

ducc
20th November 2010, 13:45
Ah! That's a good idea. I'm having trouble making the event filter installer actually install it into the QApplication object (it requires QObject?!); can you give me a hint?

ducc
21st November 2010, 20:40
I found a decent solution. I just disabled the focus for the forms that got focus, and installed the eventFilter on the class that showed the window. That works well. It just means that I can't have forms in the window that require focus (like a text input form thing). I can probably live with that.

long2015
21st June 2011, 19:53
Maybe you can use QxtGlobalShortcut ,It's part of libqxt.
http://code.google.com/p/h-qt-exercise/downloads/list