加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
keyeventdispatcher.cpp 2.59 KB
一键复制 编辑 原始数据 按行查看 历史
linbin823 提交于 2016-12-09 15:02 . 测试
#include <QInputMethodEvent>
#include <QCoreApplication>
#include <QKeyEvent>
#include "keyeventdispatcher.h"
KeyEventDispatcher::KeyEventDispatcher(QObject *parent) :
QObject(parent),m_focusItem(0)
{
}
void KeyEventDispatcher::setFocusItem(QObject *focusItem)
{
m_focusItem = focusItem;
}
void KeyEventDispatcher::sendKeyToFocusItem(const QString &keyText)
{
if (!m_focusItem) {
return;
}
if (keyText == QString("\x7F"))
{
QCoreApplication::sendEvent(m_focusItem, new QKeyEvent(QEvent::KeyPress, Qt::Key_Backspace, Qt::NoModifier));
QCoreApplication::sendEvent(m_focusItem, new QKeyEvent(QEvent::KeyRelease, Qt::Key_Backspace, Qt::NoModifier));
}
else if (keyText == QString("\n"))
{
QCoreApplication::sendEvent(m_focusItem, new QKeyEvent(QEvent::KeyPress, Qt::Key_Return, Qt::NoModifier));
QCoreApplication::sendEvent(m_focusItem, new QKeyEvent(QEvent::KeyRelease, Qt::Key_Return, Qt::NoModifier));
}
else if (keyText == QString("&&"))
{
QCoreApplication::sendEvent(m_focusItem, new QKeyEvent(QEvent::KeyPress, 0, Qt::NoModifier, "&"));
QCoreApplication::sendEvent(m_focusItem, new QKeyEvent(QEvent::KeyRelease, 0, Qt::NoModifier, "&"));
}
else if (keyText == QString("&CtrlC&"))
{
QCoreApplication::sendEvent(m_focusItem, new QKeyEvent(QEvent::KeyPress, Qt::Key_C, Qt::ControlModifier));
QCoreApplication::sendEvent(m_focusItem, new QKeyEvent(QEvent::KeyRelease, Qt::Key_C, Qt::ControlModifier));
}
else if (keyText == QString("&CtrlV&"))
{
QCoreApplication::sendEvent(m_focusItem, new QKeyEvent(QEvent::KeyPress, Qt::Key_V, Qt::ControlModifier));
QCoreApplication::sendEvent(m_focusItem, new QKeyEvent(QEvent::KeyRelease, Qt::Key_V, Qt::ControlModifier));
}
else if (keyText == QString("&Left&"))
{
QCoreApplication::sendEvent(m_focusItem, new QKeyEvent(QEvent::KeyPress, Qt::Key_Left, Qt::NoModifier));
QCoreApplication::sendEvent(m_focusItem, new QKeyEvent(QEvent::KeyRelease, Qt::Key_Left, Qt::NoModifier));
}
else if (keyText == QString("&CtrlA&"))
{
QCoreApplication::sendEvent(m_focusItem, new QKeyEvent(QEvent::KeyPress, Qt::Key_A, Qt::ControlModifier));
QCoreApplication::sendEvent(m_focusItem, new QKeyEvent(QEvent::KeyRelease, Qt::Key_A, Qt::ControlModifier));
}
else
{
QCoreApplication::sendEvent(m_focusItem, new QKeyEvent(QEvent::KeyPress, 0, Qt::NoModifier, keyText));
QCoreApplication::sendEvent(m_focusItem, new QKeyEvent(QEvent::KeyRelease, 0, Qt::NoModifier, keyText));
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化