#include "inputmethodeventcatch.h"
#include <QInputMethodEvent>
static int key2code(const QChar &key)
{
switch (key.toLatin1())
{
case 'q': case 'Q': return Qt::Key_Q;
case 'w': case 'W': return Qt::Key_W;
case 'e': case 'E': return Qt::Key_E;
case 'r': case 'R': return Qt::Key_R;
case 't': case 'T': return Qt::Key_T;
case 'y': case 'Y': return Qt::Key_Y;
case 'u': case 'U': return Qt::Key_U;
case 'i': case 'I': return Qt::Key_I;
case 'o': case 'O': return Qt::Key_O;
case 'p': case 'P': return Qt::Key_P;
case 'a': case 'A': return Qt::Key_A;
case 's': case 'S': return Qt::Key_S;
case 'd': case 'D': return Qt::Key_D;
case 'f': case 'F': return Qt::Key_F;
case 'g': case 'G': return Qt::Key_G;
case 'h': case 'H': return Qt::Key_H;
case 'j': case 'J': return Qt::Key_J;
case 'k': case 'K': return Qt::Key_K;
case 'l': case 'L': return Qt::Key_L;
case 'z': case 'Z': return Qt::Key_Z;
case 'x': case 'X': return Qt::Key_X;
case 'c': case 'C': return Qt::Key_C;
case 'v': case 'V': return Qt::Key_V;
case 'b': case 'B': return Qt::Key_B;
case 'n': case 'N': return Qt::Key_N;
case 'm': case 'M': return Qt::Key_M;
}
return Qt::Key_unknown;
}
InputMethodEventCatch::InputMethodEventCatch(QObject *parent)
: QObject{parent}
{
}
InputMethodEventCatch::~InputMethodEventCatch()
{
if (m_target) m_target->removeEventFilter(this);
}
QObject *InputMethodEventCatch::target()
{
return m_target;
}
void InputMethodEventCatch::setTarget(QObject *target)
{
if (!target) return;
if (m_target != target) {
if (m_target) m_target->removeEventFilter(this);
target->installEventFilter(this);
m_target = target;
emit targetChanged();
}
}
bool InputMethodEventCatch::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::InputMethod) {
QInputMethodEvent *input = static_cast<QInputMethodEvent *>(event);
if (input->preeditString().isEmpty()) {
emit inputMethodEventCommitted(input->commitString());
} else {
QString key;
for (const auto &attr: input->attributes()) {
if (attr.type == QInputMethodEvent::AttributeType::Cursor && attr.start > 0) {
key = input->preeditString().mid(attr.start - 1, attr.length);
break;
}
}
if (key.size() == 1)
emit inputMethodEventPressed(key2code(*key.begin()), key);
}
}
return QObject::eventFilter(obj, event);
}

梦起丶
- 粉丝: 2w+
最新资源
- 全业务运营时代的网络外包服务.ppt
- 酒店客房计算机设备网络防盗报警系统方案介绍.doc
- 公司科研项目管理办法.doc
- 事业单位会计信息化建设有效途径【精品发布】.doc
- 综合布线工程预算表.pptx
- 最新3月全国计算机等级考试二级C语言上机题库100套最新光碟拷贝版.doc
- 中小型企业网络规划设计方案(完整版).doc
- 高等教育数学建模贪心算法.pptx
- 法律规定网络财产被侵权怎么办.doc
- 智能环境助手-硬件开发资源
- 报刊订阅管理系统的设计与实现数据库课程设计.doc
- 网络安全技术与实践第二版课后答案.doc
- 信息系统工程监理18信息网络系统验收阶段的监理.pptx
- 英语知识结构网络图市公开课一等奖百校联赛优质课金奖名师赛课获奖课件.ppt
- 享受健康网络交往.pptx
- 企业网络工程设计方案.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


