以跨平台的方式C++处理键盘

Handle keyboard in C++ in cross-platform way

本文关键字:处理 键盘 C++ 方式 跨平台      更新时间:2023-10-16

我使用 Qt 在 C++ 中创建了一个应用程序。我希望如果用户单击名为btnA的按钮,那么程序应该模拟它,就好像按下了A一样。

我在谷歌上搜索并提出了一个依赖于平台的解决方案。我希望它是跨平台的。有没有接口?我更喜欢跨平台(或 linux(解决方案。

编辑:-

这是我的代码,它不会扭曲:-

#include "calculator.h"
#include "ui_calculator.h"
#include <QKeyEvent>
Calculator::Calculator(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Calculator),
DisplayString("")
{
ui->setupUi(this);
setFixedSize(this->size());
//Connected all buttons to one slot 
connect(ui->Button1,SIGNAL(clicked()),this,SLOT(CalButtonPressed()));
connect(ui->Button2,SIGNAL(clicked()),this,SLOT(CalButtonPressed()));
connect(ui->Button3,SIGNAL(clicked()),this,SLOT(CalButtonPressed()));
connect(ui->Button4,SIGNAL(clicked()),this,SLOT(CalButtonPressed()));
connect(ui->Button5,SIGNAL(clicked()),this,SLOT(CalButtonPressed()));
connect(ui->Button6,SIGNAL(clicked()),this,SLOT(CalButtonPressed()));
connect(ui->Button7,SIGNAL(clicked()),this,SLOT(CalButtonPressed()));
connect(ui->Button8,SIGNAL(clicked()),this,SLOT(CalButtonPressed()));
connect(ui->Button9,SIGNAL(clicked()),this,SLOT(CalButtonPressed()));
connect(ui->Button0,SIGNAL(clicked()),this,SLOT(CalButtonPressed()));
connect(ui->ButtonAddition,SIGNAL(clicked()),this,SLOT(CalButtonPressed()));
connect(ui->ButtonMinus,SIGNAL(clicked()),this,SLOT(CalButtonPressed()));
connect(ui->ButtonModulas,SIGNAL(clicked()),this,SLOT(CalButtonPressed()));
connect(ui->ButtonDivide,SIGNAL(clicked()),this,SLOT(CalButtonPressed()));
connect(ui->ButtonMultiplication,SIGNAL(clicked()),this,SLOT(CalButtonPressed()));
}
Calculator::~Calculator()
{
delete ui;
}

void Calculator::CalButtonPressed()
{
QPushButton *senderbtn = static_cast<QPushButton*>(sender());
QString string = senderbtn->text();
char ch = string.at(0).toLatin1();
// for testing I have taken only one button
QKeyEvent * event = new QKeyEvent(QEvent::KeyPress,Qt::Key_A,Qt::NoModifier,nullptr);
QApplication::postEvent(ui->Input,event);

}

主.cpp

#include <QApplication>
#include <QFile>
#include <cstring>
#include "calculator.h"
int main(int argc,char **argv)
{
QApplication *app= new QApplication(argc,argv);
app->setWindowIcon(QIcon(":/Images/Icon.png"));

Calculator *cal = new Calculator(nullptr);
cal->show();
return  app->exec();
}

它应该在名为Input的小部件QLineEdit显示"a"

您需要为此创建一个QKeyEvent,如下所示。

QKeyEvent *event = new QKeyEvent(QEvent::KeyPress, Qt::Key_A, Qt::NoModifier, 0);

上面的语法创建了一个类型为KeyPressQKeyEvent,按键按下为A,并告知在此过程中没有按下修饰符。

创建后,您可以使用postEvent将其添加到事件队列中。

QApplication::postEvent(this, event);

Qt文档,你可以通过这些文档来更好地了解你将要使用的内容:

  • QApplication::p ostEvent(..(

  • 秦思事件

在Windows和Mac的跨平台解决方案的情况下,我已经使用了上述方法。 希望它也适用于linux。


更新:

由于您要使用文本更新行编辑,因此需要执行以下操作:

QKeyEvent *event = new QKeyEvent(QEvent::KeyPress, Qt::Key_A, Qt::NoModifier, QKeySequence(Qt::Key_A).toString());
// This is because QLineEdit does not deduce the text from the event type, but instead just adds the text it receives.

当按下button A时,这肯定会用字符"A"更新QLineEdit。显然,在使用上面创建的新event调用postEvent之后。