Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Key Interface
keyInterface.png


Using this interface you can react on key events. You first have to register a key (or key combination) with KeyInterface::registerKey(). Note that the log widget will contain a warning if you register a key twice or it is occupied by another plugin.

For each registered key your KeyInterface::slotKeyEvent() will be triggered when the key was pressed and the slot KeyInterface::slotKeyReleaseEvent() when the key was released. Both slots get the QKeyEvent and will contain all the modifiers that were active when the event occurred.

In OpenFlippers options tab you can view all registered keys and reassign them. The events will be mapped, such that the plugins still receive the right combination. Therefore the mapping will be completely invisible to them.

See our tutorial Implementing mouse and keyboard interaction for an example of how to use mouse and keyboard events within a plugin.

To use the KeyInterface:

  • include KeyInterface.hh in your plugins header file
  • derive your plugin from the class KeyInterface
  • add Q_INTERFACES(KeyInterface) to your plugin class
  • Implement the required functions.
// In your plugin initialization register the keys you want to get.
void ExamplePlugin::initializePlugin() {
// Register keys
emit registerKey(Qt::Key_W, Qt::NoModifier, "Rotate object down");
emit registerKey(Qt::Key_A, Qt::NoModifier, "Rotate object left");
emit registerKey(Qt::Key_D, Qt::NoModifier, "Rotate object right");
emit registerKey(Qt::Key_S, Qt::NoModifier, "Rotate object up");
}
// Called when one of the registered Keys is pressed.
void MouseAndKeyPlugin::slotKeyEvent( QKeyEvent* _event ) {
// Check if it's really the right key
if ( _event->modifiers() == Qt::NoModifier ) {
// Switch pressed keys
switch (_event->key()) {
case Qt::Key_W:
// Move forward
break;
case Qt::Key_A:
// Move left
break;
case Qt::Key_D:
// Move right
break;
case Qt::Key_S:
// Move backward
break;
default:
break;
}
}
}