键盘事件处理

Posted Respect@

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了键盘事件处理相关的知识,希望对你有一定的参考价值。

键盘事件

keyevent.h

#ifndef KEYEVENT_H
#define KEYEVENT_H

#include <QWidget>

QT_BEGIN_NAMESPACE
namespace Ui { class KeyEvent; }
QT_END_NAMESPACE

class KeyEvent : public QWidget
{
    Q_OBJECT

public:
    KeyEvent(QWidget *parent = nullptr);
    ~KeyEvent();
    void drawPix();

protected:
    void keyPressEvent(QKeyEvent *event) override;
    void paintEvent(QPaintEvent *event) override;

private:
    Ui::KeyEvent *ui;
    QPixmap *m_pix;
    QImage m_image;
    int m_startX;   //图标顶点位置
    int m_startY;
    int m_width;    //界面的宽度,高度
    int m_height;
    int m_step;     //步长
};
#endif // KEYEVENT_H

keyevent.cpp

#include "keyevent.h"
#include "ui_keyevent.h"
#include <QPainter>
#include <QPen>
#include <QKeyEvent>

KeyEvent::KeyEvent(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::KeyEvent)
{
    ui->setupUi(this);
    setWindowTitle(QString("键盘事件"));
    setAutoFillBackground(true);
    setFixedSize(512,256);
    m_width = size().width();
    m_height = size().height();

    m_pix = new QPixmap(m_width,m_height);
    m_pix->fill(Qt::white);

    m_image.load("tiger.png");

    m_startX = 100;
    m_startY = 100;
    m_step = 20;
    drawPix();
}

KeyEvent::~KeyEvent()
{
    delete ui;
}

void KeyEvent::drawPix()
{
    m_pix->fill(Qt::white);

    QPainter painter(this);
    QPen pen(Qt::DotLine);

    painter.begin(m_pix);//指定m_pix为绘图设备
    painter.setPen(pen);

    //按照步长画纵向网格线
    for(int i = m_step; i < m_width; i += m_step)
    {
        painter.drawLine(QPoint(i,0),QPoint(i,m_height));
    }

    //按照步长画水平网格线
    for(int j = m_step; j < m_height; j += m_step)
    {
        painter.drawLine(QPoint(0,j),QPoint(m_width,j));
    }
    painter.end();

    //画图片
    painter.begin(m_pix);
    painter.drawImage(QPoint(m_startX,m_startY),m_image);
    painter.end();
}

void KeyEvent::keyPressEvent(QKeyEvent *event)
{
    //按下Ctrl键,每次移动为1个像素
    if(event->modifiers() == Qt::ControlModifier)
    {
        if(event->key() == Qt::Key_Left)
            m_startX = (m_startX - 1) < 0 ? m_startX : m_startX - 1;
        if(event->key() == Qt::Key_Right)
            m_startX = (m_startX + 1 + m_image.width()) > m_width ? m_startX : m_startX + 1;
        if(event->key() == Qt::Key_Up)
            m_startY = (m_startY - 1) < 0 ? m_startY : m_startY - 1;
        if(event->key() == Qt::Key_Down)
            m_startY = (m_startY + 1 + m_image.height()) > m_height ? m_startY : m_startY + 1;
    }
    else//没有按下Ctrl键,每次移动为一个步长
    {
        //调整图标左上角位置到网格顶点上
        m_startX = m_startX - m_startX % m_step;
        m_startY = m_startY - m_startY % m_step;

        if(event->key() == Qt::Key_Left)
            m_startX = (m_startX - m_step) < 0 ? m_startX : m_startX - m_step;
        if(event->key() == Qt::Key_Right)
            m_startX = (m_startX + m_step + m_image.width()) > m_width ? m_startX : m_startX + m_step;
        if(event->key() == Qt::Key_Up)
            m_startY = (m_startY - m_step) < 0 ? m_startY : m_startY - m_step;
        if(event->key() == Qt::Key_Down)
            m_startY = (m_startY + m_step + m_image.height()) > m_height ? m_startY : m_startY + m_step;
    }

    drawPix();//根据调整后的图标位置重新在m_pix上绘制图像
    update();//触发窗口重绘
}

void KeyEvent::paintEvent(QPaintEvent *event)
{
    QPainter painter;
    painter.begin(this);
    painter.drawPixmap(QPoint(0,0),*m_pix);
    painter.end();
}


main.cpp

#include "keyevent.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    KeyEvent w;
    w.show();
    return a.exec();
}

以上是关于键盘事件处理的主要内容,如果未能解决你的问题,请参考以下文章

java事件处理4(焦点,键盘

Vue 事件处理 -- 事件的基本使用 & 键盘事件

JavaScript:文本域事件处理

自动处理键盘事件的第三方库 IQKeyboardManager

将数据从片段发送到活动,无需任何事件处理或侦听器

处理 EditText 未聚焦状态时的物理键盘按键事件