以跨平台的方式C++处理键盘
Handle keyboard in C++ in cross-platform way
我使用 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);
上面的语法创建了一个类型为KeyPress
的QKeyEvent
,按键按下为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
之后。
相关文章:
- 警告处理为错误这里有什么问题
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 处理多个异常集合的C++方法
- 物理键盘上的触发器按键
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 使用流处理接收到的数据
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 基于多个条件处理地图中的所有元素
- 如何在 WindowProc 处理程序中区分箭头键和数字键盘?
- 以跨平台的方式C++处理键盘
- 如何处理 OpenSceneGraph 键盘输入事件中的区分大小写
- C++键盘挂钩-退出程序,但也防止密钥在其他地方被处理
- 处理键盘输入的回调
- 如何编写一个C++例程来检查我在键盘上按下了哪个键,以便我可以单独处理每个按键?
- 处理对话框上的键盘输入 - Windows
- 如何保持Qt小部件响应键盘和鼠标,同时处理Windows事件
- 处理键盘输入使用windows消息和directx 9
- SDL 2.0键盘处理
- 在gui中处理子到父鼠标和键盘事件的最佳方法