簡體   English   中英

以跨平台方式處理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);

上面的語法創建一個類型為KeyPressQKeyEvent ,將按鍵按下為A,並告訴您在該過程中沒有按下任何修飾符。

創建之后,可以使用postEvent將其添加到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之后。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM