C++ (Qt)quint32 QKeyEvent::nativeVirtualKey () const
#ifndef KEYTRANSLATOR_H#define KEYTRANSLATOR_H#include <QApplication>#include <QtGui>class KeyTranslator : public QObject { protected: bool eventFilter(QObject* pobj, QEvent* pe){ if (pe->type() == QEvent::KeyPress) { // for copy if (((QKeyEvent*)pe)->modifiers() == Qt::CTRL && ((QKeyEvent*)pe)->key() == 1057) { QKeyEvent* pe = new QKeyEvent(QEvent::KeyPress, Qt::Key_C, Qt::ControlModifier, "" ); QApplication::sendEvent(pobj, pe); return true; } // for cut if (((QKeyEvent*)pe)->modifiers() == Qt::CTRL && ((QKeyEvent*)pe)->key() == 1063) { QKeyEvent* pe = new QKeyEvent(QEvent::KeyPress, Qt::Key_X, Qt::ControlModifier, "" ); QApplication::sendEvent(pobj, pe); return true; } // for paste if (((QKeyEvent*)pe)->modifiers() == Qt::CTRL && ((QKeyEvent*)pe)->key() == 1052) { QKeyEvent* pe = new QKeyEvent(QEvent::KeyPress, Qt::Key_V, Qt::ControlModifier, "" ); QApplication::sendEvent(pobj, pe); return true; } // for redo if (((QKeyEvent*)pe)->modifiers() == Qt::CTRL + Qt::SHIFT && ((QKeyEvent*)pe)->key() == 1071) { QKeyEvent* pe = new QKeyEvent(QEvent::KeyPress, Qt::Key_Z, Qt::ControlModifier | Qt::ShiftModifier, "" ); QApplication::sendEvent(pobj, pe); return true; } // for undo if (((QKeyEvent*)pe)->modifiers() == Qt::CTRL && ((QKeyEvent*)pe)->key() == 1071) { QKeyEvent* pe = new QKeyEvent(QEvent::KeyPress, Qt::Key_Z, Qt::ControlModifier, "" ); QApplication::sendEvent(pobj, pe); return true; } // for select all if (((QKeyEvent*)pe)->modifiers() == Qt::CTRL && ((QKeyEvent*)pe)->key() == 1060) { QKeyEvent* pe = new QKeyEvent(QEvent::KeyPress, Qt::Key_A, Qt::ControlModifier, "" ); QApplication::sendEvent(pobj, pe); return true; } } return false; }public: KeyTranslator(QObject* pobj = 0) : QObject(pobj) { }};#endif // KEYTRANSLATOR_H
#include "keytranslator.h"
KeyTranslator* keyTranslator;
keyTranslator = new KeyTranslator(ui->plainTextEdit);ui->plainTextEdit->installEventFilter(keyTranslator);
class KeyTranslator : public QObject { // for copy if (((QKeyEvent*)pe)->modifiers() == Qt::CTRL && ((QKeyEvent*)pe)->key() == 1057) {... // for cut if (((QKeyEvent*)pe)->modifiers() == Qt::CTRL && ((QKeyEvent*)pe)->key() == 1063) {... // for paste
C++ (Qt)QKeyEvent * MyTranslateKeyEvent( const QKeyEvent * ке ){ static QMap <quint32, int> theMap; // virtual key + key if (!theNap.size()) { theMap[VKEY_A] = Qt::Key_A; // VKEY_A найдете в отладчике theMap[VKEY_С] = Qt::Key_С; ... } if (ke->type() != Qt::KeyPress) return 0; if (ke->modifiers() != Qt::CTRL) return 0; int newKey = theMap.value(ke->nativeVirtualKey(); if (!newKey || newKey == ke->key()) return 0; return new QKeyEvent(QEvent::KeyPress, newKey, Qt::ControlModifier);}