QKeyEvent keyboard event
- Constructor:
- QKeyEvent(QEvent::Type type, int key, Qt::KeyboardModifiers modifiers, const QString &text = QString(), bool autorep = false, ushort count = 1)
- Parameter Description:
- Type -- > type of event (QEvent::KeyPress, QEvent::KeyRelease, QEvent::ShortCutOverride)
- Key -- > key value of the key generating the event (viewed in Qt::Key enumeration, the characters are usually consistent with the corresponding ascll code value)
- modifiers - modifier to judge whether it is a combination key. It has the following values (which can be combined with bits or)
- Qt::NoModifier
- QT:: shiftmodifier -- > shift key is pressed simultaneously
- QT:: controlmodifier -- > Ctrl key is pressed simultaneously, similar to the following
- Qt::AltModifier
- QT:: metamodifier -- > win key
- Qt::KeypadModifier
- Qt::GroupSwitchModifier
- Text -- > character information. When the key is a character input, enter the character information in the corresponding focus component; otherwise, text is invalid
- (for example, if key = Qt::key_0, then the character '0' is input into the focus input component, and key = Qt::Key_backspace, then the text is invalid, but indicates that the backspace key is pressed)
- Is autorep -- > auto repeat key
- The number of keys involved in the count -- > event
- Member function:
- bool matches(QKeySequence::StandardKey key) const
- Standard behavior triggered by matching keys (for example: copy (QKeySequence::Copy))
- If the matching is successful, return true; otherwise, return false
- bool matches(QKeySequence::StandardKey key) const
- Event handler:
- QWidget:: keypressevent (qkeyevent *) -- > a key is pressed
- QWidget:: keyreleaseevent (qkeyevent *) -- > a key has been released
- Soft keyboard production idea:
- Button layout and define slot functions (it is better to use button groups or directly derive QPushButton to define slot functions in custom buttons to avoid defining slot functions for each button)
- setFocusPolicy(Qt::NoFocus) sets the focus policy of the software disk button to Qt::NoFocus to prevent the input box from losing focus and normal input after clicking the soft keyboard key
- In the slot function, create the corresponding QKeyEvent event event object keyeventobj according to the pressed button
- Use the void QCoreApplication::postEvent(QObject *receiver, QEvent *event, int priority = Qt::NormalEventPriority) function to send key events to the focus component
- Parameter Description:
- receiver is the object of event reception (here is the corresponding button)
- Keyboard event keyeventobj created by event (event is the base class of QKeyEvent, so it is compatible)
- Priority event priority (the APPlication object will sort all generated events by priority and dispatch them in order)
- Parameter Description:
- You can get the current focus component (such as TextEidt class object) through QApplication::FocusWidget()
- Custom keyboard mouse combination key (eg.Ctrl + left mouse button) event ideas:
- Customize mouse press events
- Make the following judgment when entering the mouse press event processing function:
- Determine the specific key pressed by the mouse
- Judge the keyboard key modifier (the pressed modifiers can be returned through the function QApplication::keyboardModifiers())
- If it is determined that Ctrl and the left mouse button are pressed, perform the corresponding operation
How to change focus and focus policy
- Change method:
- Tab or Shift+Tab
- Mouse click
- Mouse wheel
- Code setFocus()
- focusPolicy:
- QT:: tabfouc -- > focus can be obtained through Tab
- QT:: clickfocus -- > you can click to get focus
- QT:: strongfocus -- > focus can be obtained in the above two ways
- QT:: nofocus -- > cannot get focus in the above way, but setFocus() is still valid
Production code of simple soft keyboard (some keys):
- effect:
Custom derived PushButton class header file btkey h
#ifndef BTKEY_H #define BTKEY_H #include <QPushButton> #include <QKeyEvent> #include <QApplication> #include <QObject> #include "mykeyboard.h" #include <QDebug> class Btkey : public QPushButton { Q_OBJECT public: // Two bool static members are used to indicate whether the caps and shift keys of the keyboard are pressed static bool capstatus; static bool shiftstatus; Btkey(QWidget *parent = nullptr); ~Btkey(); private slots: void handler_slot(bool k); }; #endif // BTKEY_H
Source file btkey. For custom derived PushButton class cpp
#include "btkey.h" bool Btkey::capstatus = false; bool Btkey::shiftstatus = false; Btkey::Btkey(QWidget *parent) : QPushButton(parent) { // Set the color of the button to translucent blue this->setStyleSheet("background-color: rgba(153, 215, 255, 150);"); // Set focus acquisition policy this->setFocusPolicy(Qt::NoFocus); // Link slot function connect(this, SIGNAL(clicked(bool)), this, SLOT(handler_slot(bool))); } Btkey::~Btkey() { this->destroy(); } void Btkey::handler_slot(bool k) { QApplication::keyboardModifiers(); auto *objfous = QApplication::focusWidget(); bool ok; int key = this->text().toInt(&ok, 16); QString text(this->text()); qDebug() << this->text(); if(this->text() == "CapsLK"){ qDebug() << "Press CapsLk key"; capstatus = k; qobject_cast<MyKeyBoard *>(this->parent())->letterToggle(); text = ""; }else if(this->text() == "Shift"){ qDebug() << "Press Shift key"; shiftstatus = k; qobject_cast<MyKeyBoard *>(this->parent())->letterToggle(); key = Qt::Key_Shift; text = ""; }else if(this->text() == "Enter"){ qDebug() << "Press Enter key"; key = Qt::Key_Enter; }else if(this->text() == "Backspace"){ qDebug() << "Press BackSpace key"; key = Qt::Key_Backspace; }else if(this->text() == "Tab"){ qDebug() << "Press Tab key"; key = Qt::Key_Tab; }else{ qDebug() << "Press a number, letter, or symbol"; } // Create the corresponding key event object QKeyEvent *event = new QKeyEvent(QEvent::KeyPress, key , Qt::NoModifier, text , false, 1); // fousWidget() returns the object where the cursor is located // Dispatch event QCoreApplication::postEvent(objfous, event); // If shift is pressed and another key is pressed, the shift key will pop up if(shiftstatus && key != Qt::Key_Shift){ shiftstatus = false; qobject_cast<MyKeyBoard *>(this->parent())->shift_recover(); qobject_cast<MyKeyBoard *>(this->parent())->letterToggle(); } }
The header file of the soft keyboard class mykeyboard h
#ifndef MYKEYBOARD_H #define MYKEYBOARD_H #include <QWidget> #include "btkey.h" #include <QButtonGroup> #include <QBrush> QT_BEGIN_NAMESPACE namespace Ui { class MyKeyBoard; } QT_END_NAMESPACE class MyKeyBoard : public QWidget { Q_OBJECT public: MyKeyBoard(QWidget *parent = nullptr); ~MyKeyBoard(); void letterToggle(void); void shift_recover(void); private slots: private: Ui::MyKeyBoard *ui; QButtonGroup *Btletter; QList<QPushButton *> *Btlist; }; #endif // MYKEYBOARD_H
The source file of the soft keyboard class mykeyboard cpp
#include "mykeyboard.h" #include "ui_mykeyboard.h" MyKeyBoard::MyKeyBoard(QWidget *parent) : QWidget(parent) , ui(new Ui::MyKeyBoard) , Btletter(new QButtonGroup(this)) , Btlist(new QList<QPushButton *>()) { ui->setupUi(this); /* The layout of the button and some attribute definitions (autorepeat attribute, let the press continue to output) are designed in uidesigner*/ // Put the letter keys into a key group to facilitate case switching { Btletter->addButton(ui->key_a, 0); Btletter->addButton(ui->key_b, 1); Btletter->addButton(ui->key_c, 2); Btletter->addButton(ui->key_d, 3); Btletter->addButton(ui->key_e, 4); Btletter->addButton(ui->key_f, 5); Btletter->addButton(ui->key_g, 6); Btletter->addButton(ui->key_h, 7); Btletter->addButton(ui->key_i, 8); Btletter->addButton(ui->key_j, 9); Btletter->addButton(ui->key_k, 10); Btletter->addButton(ui->key_l, 11); Btletter->addButton(ui->key_n, 12); Btletter->addButton(ui->key_m, 13); Btletter->addButton(ui->key_o, 14); Btletter->addButton(ui->key_p, 15); Btletter->addButton(ui->key_q, 16); Btletter->addButton(ui->key_r, 17); Btletter->addButton(ui->key_s, 18); Btletter->addButton(ui->key_t, 19); Btletter->addButton(ui->key_u, 20); Btletter->addButton(ui->key_v, 21); Btletter->addButton(ui->key_w, 22); Btletter->addButton(ui->key_x, 23); Btletter->addButton(ui->key_y, 24); Btletter->addButton(ui->key_z, 25); } // Set shift and capslk keys to be verifiable (press not to pop up automatically) ui->key_capslk->setCheckable(true); ui->key_shift->setCheckable(true); // Set the background to transparent this->setAutoFillBackground(true); QPalette palette; palette.setBrush(QPalette::Background, QBrush(QColor(0,0,0,0))); this->setPalette(palette); } MyKeyBoard::~MyKeyBoard() { delete ui; } // Flip the case of CapsLk and Shift keys void MyKeyBoard::letterToggle() { for(int i = 0; i < 26; i++){ QString c = Btletter->button(i)->text(); // Equivalent to logical XOR if(Btkey::capstatus ^ Btkey::shiftstatus) Btletter->button(i)->setText(c.toUpper()); else Btletter->button(i)->setText(c.toLower()); } } void MyKeyBoard::shift_recover() { ui->key_shift->setChecked(false); }
- use:
- Add the relevant header files and source files (Btkey and MyKeyBoard) of the keyboard class to the project
- Add Widget component in designer interface
- Promote Widget component to MyKeyBoard class