以跨平台方式处理 C++ 中的键盘
Handle keyboard in C++ in cross-platform way
我已经使用 Qt 在 C++ 中创建了一个应用程序。我希望如果用户单击名为 btnA
的按钮,那么程序应该模拟它就像按下 A 一样。
我在 google 上搜索并找到了一个依赖于平台的解决方案。我希望它是跨平台的。有 API 吗?我更喜欢跨平台(或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);
}
main.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);
上面的语法创建了一个类型为 KeyPress
的 QKeyEvent
,按下的键为 A 并且告诉我们在这个过程中没有按下修饰符.
创建后,您可以使用 postEvent
.
将其添加到 eventQueue
QApplication::postEvent(this, event);
您可以阅读 Qt 文档以更好地理解您将要使用的内容:
我在 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
之后。
我已经使用 Qt 在 C++ 中创建了一个应用程序。我希望如果用户单击名为 btnA
的按钮,那么程序应该模拟它就像按下 A 一样。
我在 google 上搜索并找到了一个依赖于平台的解决方案。我希望它是跨平台的。有 API 吗?我更喜欢跨平台(或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);
}
main.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);
上面的语法创建了一个类型为 KeyPress
的 QKeyEvent
,按下的键为 A 并且告诉我们在这个过程中没有按下修饰符.
创建后,您可以使用 postEvent
.
QApplication::postEvent(this, event);
您可以阅读 Qt 文档以更好地理解您将要使用的内容:
我在 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
之后。