ADTF  3.18.2
Source Code for Qt5 Key Event Runner
Location
./src/examples/src/adtf/active_runner/qt_key_event_runner/
Namespace for entire ADTF SDK.
Build Environment
To see how to set up the build environment have a look at ADTF CMake Environment
This example shows:
Implementation
/*
* This file depends on Qt which is licensed under LGPLv3.
* See ADTF_DIR/3rdparty/qt5 and doc/license for detailed information.
*/
#include <QApplication>
#include <QWindow>
#include <QKeyEvent>
#include <QMainWindow>
#include <string>
using namespace adtf::ucom;
using namespace adtf::base;
using namespace adtf::streaming;
// this is used to describe the key property values
std::vector<std::pair<Qt::Key, std::string>> s_oQtKeys
{
{Qt::Key_Escape, "Escape" },
{Qt::Key_Tab, "Tab" },
{Qt::Key_Backtab, "Backtab" },
{Qt::Key_Backspace, "Backspace" },
{Qt::Key_Return, "Return" },
{Qt::Key_Enter, "Enter" },
{Qt::Key_Insert, "Insert" },
{Qt::Key_Delete, "Delete" },
{Qt::Key_Pause, "Pause" },
{Qt::Key_Print, "Print" },
{Qt::Key_SysReq, "SysReq" },
{Qt::Key_Clear, "Clear" },
{Qt::Key_Home, "Home" },
{Qt::Key_End, "End" },
{Qt::Key_Left, "Left" },
{Qt::Key_Up, "Up" },
{Qt::Key_Right, "Right" },
{Qt::Key_Down, "Down" },
{Qt::Key_PageUp, "PageUp" },
{Qt::Key_PageDown, "PageDown" },
{Qt::Key_Shift, "Shift" },
{Qt::Key_Control, "Control" },
{Qt::Key_Meta, "Meta" },
{Qt::Key_Alt, "Alt" },
{Qt::Key_CapsLock, "CapsLock" },
{Qt::Key_NumLock, "NumLock" },
{Qt::Key_ScrollLock, "ScrollLock" },
{Qt::Key_F1, "F1" },
{Qt::Key_F2, "F2" },
{Qt::Key_F3, "F3" },
{Qt::Key_F4, "F4" },
{Qt::Key_F5, "F5" },
{Qt::Key_F6, "F6" },
{Qt::Key_F7, "F7" },
{Qt::Key_F8, "F8" },
{Qt::Key_F9, "F9" },
{Qt::Key_F10, "F10" },
{Qt::Key_F11, "F11" },
{Qt::Key_F12, "F12" },
{Qt::Key_F13, "F13" },
{Qt::Key_F14, "F14" },
{Qt::Key_F15, "F15" },
{Qt::Key_F16, "F16" },
{Qt::Key_F17, "F17" },
{Qt::Key_F18, "F18" },
{Qt::Key_F19, "F19" },
{Qt::Key_F20, "F20" },
{Qt::Key_F21, "F21" },
{Qt::Key_F22, "F22" },
{Qt::Key_F23, "F23" },
{Qt::Key_F24, "F24" },
{Qt::Key_F25, "F25" },
{Qt::Key_F26, "F26" },
{Qt::Key_F27, "F27" },
{Qt::Key_F28, "F28" },
{Qt::Key_F29, "F29" },
{Qt::Key_F30, "F30" },
{Qt::Key_F31, "F31" },
{Qt::Key_F32, "F32" },
{Qt::Key_F33, "F33" },
{Qt::Key_F34, "F34" },
{Qt::Key_F35, "F35" },
{Qt::Key_Super_L, "Super_L" },
{Qt::Key_Super_R, "Super_R" },
{Qt::Key_Menu, "Menu" },
{Qt::Key_Hyper_L, "Hyper_L" },
{Qt::Key_Hyper_R, "Hyper_R" },
{Qt::Key_Help, "Help" },
{Qt::Key_Direction_L, "Direction_L" },
{Qt::Key_Direction_R, "Direction_R" },
{Qt::Key_Space, "Space" },
{Qt::Key_Exclam, "Exclam" },
{Qt::Key_QuoteDbl, "QuoteDbl" },
{Qt::Key_NumberSign, "NumberSign" },
{Qt::Key_Dollar, "Dollar" },
{Qt::Key_Percent, "Percent" },
{Qt::Key_Ampersand, "Ampersand" },
{Qt::Key_Apostrophe, "Apostrophe" },
{Qt::Key_ParenLeft, "ParenLeft" },
{Qt::Key_ParenRight, "ParenRight" },
{Qt::Key_Asterisk, "Asterisk" },
{Qt::Key_Plus, "Plus" },
{Qt::Key_Comma, "Comma" },
{Qt::Key_Minus, "Minus" },
{Qt::Key_Period, "Period" },
{Qt::Key_Slash, "Slash" },
{Qt::Key_0, "0" },
{Qt::Key_1, "1" },
{Qt::Key_2, "2" },
{Qt::Key_3, "3" },
{Qt::Key_4, "4" },
{Qt::Key_5, "5" },
{Qt::Key_6, "6" },
{Qt::Key_7, "7" },
{Qt::Key_8, "8" },
{Qt::Key_9, "9" },
{Qt::Key_Colon, "Colon" },
{Qt::Key_Semicolon, "Semicolon" },
{Qt::Key_Less, "Less" },
{Qt::Key_Equal, "Equal" },
{Qt::Key_Greater, "Greater" },
{Qt::Key_Question, "Question" },
{Qt::Key_At, "At" },
{Qt::Key_A, "A" },
{Qt::Key_B, "B" },
{Qt::Key_C, "C" },
{Qt::Key_D, "D" },
{Qt::Key_E, "E" },
{Qt::Key_F, "F" },
{Qt::Key_G, "G" },
{Qt::Key_H, "H" },
{Qt::Key_I, "I" },
{Qt::Key_J, "J" },
{Qt::Key_K, "K" },
{Qt::Key_L, "L" },
{Qt::Key_M, "M" },
{Qt::Key_N, "N" },
{Qt::Key_O, "O" },
{Qt::Key_P, "P" },
{Qt::Key_Q, "Q" },
{Qt::Key_R, "R" },
{Qt::Key_S, "S" },
{Qt::Key_T, "T" },
{Qt::Key_U, "U" },
{Qt::Key_V, "V" },
{Qt::Key_W, "W" },
{Qt::Key_X, "X" },
{Qt::Key_Y, "Y" },
{Qt::Key_Z, "Z" },
{Qt::Key_BracketLeft, "BracketLeft" },
{Qt::Key_Backslash, "Backslash" },
{Qt::Key_BracketRight, "BracketRight" },
{Qt::Key_AsciiCircum, "AsciiCircum" },
{Qt::Key_Underscore, "Underscore" },
{Qt::Key_QuoteLeft, "QuoteLeft" },
{Qt::Key_BraceLeft, "BraceLeft" },
{Qt::Key_Bar, "Bar" },
{Qt::Key_BraceRight, "BraceRight" },
{Qt::Key_AsciiTilde, "AsciiTilde" }
};
// we derive from cActiveRunner which handles all the required interface implementations for us
class cQtKeyEventRunner:
public QObject,
public cActiveRunner
{
Q_OBJECT
public:
ADTF_CLASS_ID_NAME(cQtKeyEventRunner,
"qt_key_event_runner.streaming.adtf.cid",
"Qt5 Key Event Runner");
cQtKeyEventRunner()
{
set_description(*this, "Use this Active Runner to send a trigger to Runner by pressing a defined keyboard shortcut.");
m_eKey.SetValueList(s_oQtKeys);
m_eKey.SetDescription("Key to choose for shortcut combination.");
RegisterPropertyVariable("key", m_eKey);
m_bCtrl.SetDescription("If enabled - 'CTRL' key will be set for shortcut combination.");
RegisterPropertyVariable("ctrl", m_bCtrl);
m_bAlt.SetDescription("If enabled - 'ALT' key will be set for shortcut combination.");
RegisterPropertyVariable("alt", m_bAlt);
m_bShift.SetDescription("If enabled - 'SHIFT' key will be set for shortcut combination.");
RegisterPropertyVariable("shift", m_bShift);
set_help_link(*this, "$(ADTF_DIR)/doc/adtf_html/page_qt_key_event_runner.html");
}
// we override this method to install our event filter
// when the runner should be active.
tResult Activate() override
{
// The main window should always be the only instance of QMainWindow and has been created by the Qt5 ADTF
// XSystem UI Service.
auto oAllWindows = qApp->topLevelWidgets();
m_pMainWindow = nullptr;
for (const auto pWindow : oAllWindows)
{
if (auto pMainWindow = qobject_cast<QMainWindow*>(pWindow))
{
m_pMainWindow = pMainWindow;
break;
}
}
if (!m_pMainWindow)
{
RETURN_ERROR_DESC(ERR_NOT_FOUND, "Unable to find main window.");
}
// we want to intercept key events that are not handled by any widget contained within the main window.
m_pMainWindow->installEventFilter(this);
}
// we override this method to stop intercepting key events.
tResult Deactivate() override
{
if (m_pMainWindow)
{
m_pMainWindow->removeEventFilter(this);
m_pMainWindow = nullptr;
}
}
bool IsActivated() const override
{
return m_pMainWindow != nullptr;
}
// this method will recieve all events that the main window recieves
bool eventFilter(QObject* pObject, QEvent* pEvent) override
{
if (pObject == m_pMainWindow && pEvent->type() == QEvent::KeyPress)
{
QKeyEvent* pKeyEvent = static_cast<QKeyEvent*>(pEvent);
if (pKeyEvent->key() == m_eKey &&
(!m_bCtrl || pKeyEvent->modifiers() & Qt::ControlModifier) &&
(!m_bAlt || pKeyEvent->modifiers() & Qt::AltModifier) &&
(!m_bShift || pKeyEvent->modifiers() & Qt::ShiftModifier))
{
// if the condition is met, we trigger all items connected to this runner.
Trigger();
}
}
// let Qt handle the event as well.
return false;
}
private:
QMainWindow* m_pMainWindow = nullptr;
property_variable<Qt::Key> m_eKey = Qt::Key_0;
property_variable<bool> m_bCtrl = false;
property_variable<bool> m_bAlt = false;
property_variable<bool> m_bShift = false;
};
// this creates the plugin entry functions
ADTF_PLUGIN("Qt5 Key Event Runner Plugin", cQtKeyEventRunner)
#include <qt_key_event_runner.moc>
#define ADTF_PLUGIN(__plugin_identifier,...)
The ADTF Plugin Macro will add the code of a adtf::ucom::ant::IPlugin implementation.
Definition: adtf_plugin.h:22
#define RETURN_ERROR_DESC(_code,...)
Same as RETURN_ERROR(_error) using a printf like parameter list for detailed error description.
#define RETURN_NOERROR
Return status ERR_NOERROR, which requires the calling function's return type to be tResult.
#define REQUIRE_INTERFACE(_interface)
Macro usable with ADTF_CLASS_DEPENDENCIES() to require mandatory interfaces.
#define ADTF_CLASS_DEPENDENCIES(...)
Add interface ids (string literals,.
#define ADTF_CLASS_ID_NAME(_class, _strcid, _strclabel)
Common macro to enable correct treatment of class identifier AND Class Name by IClassInfo.
Definition: class_id.h:33
The IReferenceClock interface provides the reference time source for the filter graph.
Interface definition for the ADTF XSystem based on Qt.
Copyright © Audi Electronics Venture GmbH.
Namespace for the ADTF Base SDK.
void set_description(base::ant::IConfiguration &oConfig, const char *strDescription)
Sets description information.
void set_help_link(base::ant::IConfiguration &oConfig, const char *strUrl)
Set the link to the corresponding help/documentation.
Namespace for the ADTF Streaming SDK.
Namespace for the ADTF uCOM3 SDK.
Copyright © Audi Electronics Venture GmbH.
Copyright © Audi Electronics Venture GmbH.