user9728682
user9728682

Reputation:

Handle keyboard in C++ in cross-platform way

I have created an application in C++ using Qt. I want that if the user clicked on the button named btnA ,then the program should emulate it as if A is pressed.

I searched on google and came up to a platform dependent solution. I want it to be cross platform .Is there any API ? I would prefer cross-platform(or linux) solution.

Edit:-

here is my code and its not wrking :-

#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();

}

It should display 'a' in QLineEdit widget named Input

Upvotes: 0

Views: 814

Answers (1)

Vishaal Shankar
Vishaal Shankar

Reputation: 1658

You need to create a QKeyEvent for this as shown below.

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

The above syntax creates a QKeyEvent of type KeyPress, with the key pressed as A and tells that there was no modifier pressed in the process.

Once that is created, you can add it to the eventQueue using postEvent.

QApplication::postEvent(this, event);

Qt Documentations that you can go through for better understanding of what you are going to use :

I have used the above in case of a cross-platform solution for Windows and Mac. Hopefully, It would work just fine for linux as well.


Update :

Since you want to update the line edit with the text, you need to do the following :

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.

This will surely update the QLineEdit with the character 'A' when the button A is pressed. Obviously after you call postEvent with the new event that is created above.

Upvotes: 3

Related Questions