diff --git a/scripting/scripting.cpp b/scripting/scripting.cpp index 7ce6d59fd7..3e3ab5691e 100644 --- a/scripting/scripting.cpp +++ b/scripting/scripting.cpp @@ -587,6 +587,28 @@ void KWin::JSEngineGlobalMethodsWrapper::registerWindow(QQuickWindow *window) }); } +bool KWin::JSEngineGlobalMethodsWrapper::registerShortcut(const QString &name, const QString &text, const QKeySequence& keys, QJSValue function) +{ + if (!function.isCallable()) { + qCDebug(KWIN_SCRIPTING) << "Fourth and final argument must be a javascript function"; + return false; + } + + QAction *a = new QAction(this); + a->setObjectName(name); + a->setText(text); + const QKeySequence shortcut = QKeySequence(keys); + KGlobalAccel::self()->setShortcut(a, QList{shortcut}); + KWin::input()->registerShortcut(shortcut, a); + + connect(a, &QAction::triggered, this, [=]() mutable { + QJSValueList arguments; + arguments << Scripting::self()->qmlEngine()->toScriptValue(a); + function.call(arguments); + }); + return true; +} + KWin::Scripting *KWin::Scripting::s_self = nullptr; KWin::Scripting *KWin::Scripting::create(QObject *parent) diff --git a/scripting/scripting.h b/scripting/scripting.h index 07f5e7346c..d2682e22e0 100644 --- a/scripting/scripting.h +++ b/scripting/scripting.h @@ -28,6 +28,7 @@ along with this program. If not, see . #include #include #include +#include class QQmlComponent; class QQmlContext; @@ -318,6 +319,7 @@ public: public Q_SLOTS: QVariant readConfig(const QString &key, QVariant defaultValue = QVariant()); void registerWindow(QQuickWindow *window); + bool registerShortcut(const QString &name, const QString &text, const QKeySequence& keys, QJSValue function); private: DeclarativeScript *m_script;