PDA

View Full Version : How to focus on a line edit and start typing using another keypad in linux



020394
13th August 2013, 02:17
Hello Guys ,I am trying to make an program with the use of a normal keypad instead of a keyboard . And I am doing it in linux environment , using a imx53 board. What i can do now is jus make the keypad type in just one line edit without even focusing on the lineEdit at all .Here is the codes


void LoginGui::CheckKeypadPressed()
{
if(key_Available()==1)
{
int CheckNumber;
CheckNumber=key_Get();

if(CheckNumber==1)
{
ui->password->setText(ui->password->text()+"1");
}
if(CheckNumber==2)
{
ui->password->setText(ui->password->text()+"2");
}
if(CheckNumber==3)
{
ui->password->setText(ui->password->text()+"3");
}
if(CheckNumber==4)
{
ui->password->setText(ui->password->text()+"4");
}
if(CheckNumber==5)
{
ui->password->setText(ui->password->text()+"5");
}
if(CheckNumber==6)
{
ui->password->setText(ui->password->text()+"6");
}
if(CheckNumber==7)
{
ui->password->setText(ui->password->text()+"7");
}
if(CheckNumber==8)
{
ui->password->setText(ui->password->text()+"8");
}
if(CheckNumber==9)
{
ui->password->setText(ui->password->text()+"9");
}
}
What i am going to do now is to have 2 line edit . And i want to use the keypad for 2 line edits , Currently the Codes i have can't even use for 2 line edit .
What i am thinking is making an "if" statement to focus on 2 different line edits . I am do not know how to do this , Please Help me

ChrisW67
13th August 2013, 03:27
Does the keypad appear as an input device to Linux? If that's the case I would have expected it to just work like any other keyboard.

Anyway, you can use the Qt Event system to inject key press and release events into the input of whatever widget has focus. The widget will handle the key press however it normally would.


#include <QtGui>

class Window: public QWidget
{
Q_OBJECT
public:
explicit Window(QWidget *p = 0): QWidget(p) {
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(new QLineEdit());
layout->addWidget(new QLineEdit());
setLayout(layout);

// Organise some fake keyboard activity
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), SLOT(fakeKeyPress()));
timer->setInterval(500);
timer->start();
}
private slots:
void fakeKeyPress() {
QWidget *focussed = QApplication::focusWidget();
if (focussed) {
QKeyEvent press(QEvent::KeyPress, Qt::Key_1, Qt::NoModifier, "1");
QApplication::sendEvent(focussed, &press);
QKeyEvent release(QEvent::KeyRelease, Qt::Key_1, Qt::NoModifier, "1");
QApplication::sendEvent(focussed, &release);
}
}
};

int main(int argc, char **argv)
{
QApplication app(argc, argv);
Window w;
w.show();
return app.exec();
}
#include "main.moc"

020394
13th August 2013, 04:44
I am using a IMX53 board . and the keypad is connected to a GPIO pin on the board .
I am Still unclear of the QKeyEvent parts, Is there other ways i can do it ?

Added after 53 minutes:


Does the keypad appear as an input device to Linux? If that's the case I would have expected it to just work like any other keyboard.

Anyway, you can use the Qt Event system to inject key press and release events into the input of whatever widget has focus. The widget will handle the key press however it normally would.


#include <QtGui>

class Window: public QWidget
{
Q_OBJECT
public:
explicit Window(QWidget *p = 0): QWidget(p) {
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(new QLineEdit());
layout->addWidget(new QLineEdit());
setLayout(layout);

// Organise some fake keyboard activity
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), SLOT(fakeKeyPress()));
timer->setInterval(500);
timer->start();
}
private slots:
void fakeKeyPress() {
QWidget *focussed = QApplication::focusWidget();
if (focussed) {
QKeyEvent press(QEvent::KeyPress, Qt::Key_1, Qt::NoModifier, "1");
QApplication::sendEvent(focussed, &press);
QKeyEvent release(QEvent::KeyRelease, Qt::Key_1, Qt::NoModifier, "1");
QApplication::sendEvent(focussed, &release);
}
}
};

int main(int argc, char **argv)
{
QApplication app(argc, argv);
Window w;
w.show();
return app.exec();
}
#include "main.moc"



I tried some trial and errors with your codes , I tried doing this , Not sure whether it is the right way to do it .

void PatientGui:: fakeKeyPress() {
QWidget *focussed = ui->nric->focusWidget();
if (focussed) {
if(key_Available()==1)
{
int CheckNumber;
CheckNumber=key_Get();

if(CheckNumber==1)
{
ui->nric->setText(ui->nric->text()+"1");
}
if(CheckNumber==2)
{
ui->nric->setText(ui->nric->text()+"2");
}
if(CheckNumber==3)
{
ui->nric->setText(ui->nric->text()+"3");
}
if(CheckNumber==4)
{
ui->nric->setText(ui->nric->text()+"4");
}
if(CheckNumber==5)
{
ui->nric->setText(ui->nric->text()+"5");
}
if(CheckNumber==6)
{
ui->nric->setText(ui->nric->text()+"6");
}
if(CheckNumber==7)
{
ui->nric->setText(ui->nric->text()+"7");
}
if(CheckNumber==8)
{
ui->nric->setText(ui->nric->text()+"8");
}
if(CheckNumber==9)
{
ui->nric->setText(ui->nric->text()+"9");
}

}
QWidget *focussed1 = ui->bednumber->focusWidget();
if (focussed1) {
if(key_Available()==1)
{
int CheckNumber;
CheckNumber=key_Get();

if(CheckNumber==1)
{
ui->password->setText(ui->password->text()+"1");
}
if(CheckNumber==2)
{
ui->password->setText(ui->password->text()+"2");
}
if(CheckNumber==3)
{
ui->password->setText(ui->password->text()+"3");
}
if(CheckNumber==4)
{
ui->password->setText(ui->password->text()+"4");
}
if(CheckNumber==5)
{
ui->password->setText(ui->password->text()+"5");
}
if(CheckNumber==6)
{
ui->password->setText(ui->password->text()+"6");
}
if(CheckNumber==7)
{
ui->password->setText(ui->password->text()+"7");
}
if(CheckNumber==8)
{
ui->password->setText(ui->password->text()+"8");
}
if(CheckNumber==9)
{
ui->password->setText(ui->password->text()+"9");
}
}
}
Can I do this ?

ChrisW67
14th August 2013, 05:12
Typically you'd address this by writing/using a Linux device driver that interfaces the hardware to the Linux input events system. The rest would flow naturally from that. There are GPIO keyboard modules in the mainline kernel... Device Drivers -> GPIO Support and Device Drivers -> Input device support -> Keyboards. I don't know if that is useful on your board.

If you must do it in your program. How does your program get notified that a key is pressed and/or released? Is an interrupt generated or are you expected to poll? Essentially the code in my fakeKeyPress() routine is the sort of code you should execute when you hear of a key being pressed. You need to generate appropriate QKeyEvents for the keys pressed.

020394
14th August 2013, 06:29
Basically , i put my keypad codes in a timer . Currently i am facing a problem where by i can only key in one line edit and only can key in in the line edit , when i got to the next page , i cant type at all,and when i go back to the first page , same things happened
What i am doing is a group project , the keypad part is from another guy , he put it into a threat and keep checking for the button press and returning a value to me for the number pressed .the keypad is connected to the keypad pin on the board.

Added after 28 minutes:

Anyway i tried running your codes , it will keep input 1 without any keys being pressed in windows qt

020394
14th August 2013, 12:00
Ok i tried the method u have give with my keypad codes, It works without this
There are GPIO keyboard modules in the mainline kernel... Device Drivers -> GPIO Support and Device Drivers -> Input device support -> Keyboards. .I only implented the "fakekeypress()" once in a cpp file . And i am suprised it works on other Ui even though i did not implement the "fakeKeypress()" in its cpp file ,, Is there a reason for it ?