3#include <QDesktopServices>
4#include <QLoggingCategory>
9 #include "Windows/HookWindows.h"
10#elif defined(Q_OS_ANDROID)
11#elif defined(Q_OS_MACOS)
12#elif defined(Q_OS_LINUX)
13 #include "Unix/NativeEventFilterUnix.h"
15#include "RabbitCommonTools.h"
17QAbstractNativeEventFilter* g_pNativeEventFilter =
nullptr;
18static Q_LOGGING_CATEGORY(log,
"Plugin.Hook")
22 , m_pParameterPlugin(pPara)
25 qDebug(log) <<
"CHook::~CHook()";
30 qDebug(log) <<
"CHook::~CHook()";
38#elif defined(Q_OS_LINUX)
39 p =
new CHook(pPara, parent);
44int CHook::RegisterKeyboard()
48 if(m_pParameterPlugin->GetDesktopShortcutsScript()
49 && !m_pParameterPlugin->GetRestoreDesktopShortcutsScript().isEmpty()
50 && !m_pParameterPlugin->GetDisableDesktopShortcutsScript().isEmpty()) {
51#if defined(Q_OS_WIN32)
52 QDesktopServices::openUrl(m_pParameterPlugin->GetDisableDesktopShortcutsScript());
54 RunCommand(m_pParameterPlugin->GetDisableDesktopShortcutsScript());
58 OnDisableDesktopShortcuts();
64int CHook::UnRegisterKeyboard()
67#if defined(Q_OS_WIN32)
68 QDesktopServices::openUrl(m_pParameterPlugin->GetRestoreDesktopShortcutsScript());
70 RunCommand(m_pParameterPlugin->GetRestoreDesktopShortcutsScript());
73 OnRestoreDesktopShortcuts();
76 OnUnRegisterKeyboard();
80int CHook::OnRegisterKeyboard()
82#if defined(Q_OS_LINUX) && !defined(Q_OS_MACOS) && !defined(Q_OS_ANDROID)
83 if(!g_pNativeEventFilter)
85 if(g_pNativeEventFilter)
86 qApp->installNativeEventFilter(g_pNativeEventFilter);
88 qApp->installEventFilter(
this);
93int CHook::OnUnRegisterKeyboard()
95 if(g_pNativeEventFilter) {
96 qApp->removeNativeEventFilter(g_pNativeEventFilter);
97 delete g_pNativeEventFilter;
98 g_pNativeEventFilter =
nullptr;
101 if(m_pParameterPlugin)
102 qApp->removeEventFilter(
this);
107int CHook::OnDisableDesktopShortcuts()
109#if defined(Q_OS_LINUX) && !defined(Q_OS_MACOS) && !defined(Q_OS_ANDROID)
110 m_DesktopShortcutsManager.disableAllShortcuts();
115int CHook::OnRestoreDesktopShortcuts()
117#if defined(Q_OS_LINUX) && !defined(Q_OS_MACOS) && !defined(Q_OS_ANDROID)
118 m_DesktopShortcutsManager.restoreAllShortcuts();
123bool CHook::eventFilter(QObject *watched, QEvent *event)
125 if(QEvent::KeyPress == event->type() || QEvent::KeyRelease == event->type())
127 if(m_pParameterPlugin && m_pParameterPlugin->GetCaptureAllKeyboard()) {
129 bool bProcess =
false;
130 QKeyEvent *keyEvent =
static_cast<QKeyEvent *
>(event);
131 int key = keyEvent->key();
135 case Qt::Key_Super_L:
136 case Qt::Key_Super_R:
144 CFrmViewer* focus = qobject_cast<CFrmViewer*>(QApplication::focusWidget());
145 qDebug(log) <<
"eventFilter:" << keyEvent
146 << watched << focus << bProcess;
148 if(focus == watched) {
159 switch(keyEvent->type())
161 case QKeyEvent::KeyPress:
162 emit focus->sigKeyPressEvent(keyEvent);
164 case QKeyEvent::KeyRelease:
165 emit focus->sigKeyReleaseEvent(keyEvent);
177bool CHook::RunCommand(
const QString &program,
const QStringList &args,
int timeout)
180 process.setProgram(program);
182 process.setArguments(args);
188 if (!process.waitForFinished(timeout)) {
189 qWarning(log) <<
"Command timeout:" << program << args;
193 if (process.exitCode() != 0) {
194 QString errorOutput = process.readAllStandardError();
195 qWarning(log) <<
"Command failed:" << program << args
196 <<
"exit code:" << process.exitCode()
197 <<
"failed:" << errorOutput;
201 QString standardOutput = process.readAllStandardOutput();
202 if (!standardOutput.isEmpty()) {
203 qDebug(log) <<
"Command output:" << standardOutput.trimmed();
用于显示从 CConnectDesktop 输出的图像,和向 CConnectDesktop 发送键盘、鼠标事件。
The class is the HOOK abstract class.