Results 1 to 6 of 6

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

  1. #1
    Join Date
    May 2013
    Posts
    47
    Qt products
    Qt5
    Platforms
    Windows

    Default How to focus on a line edit and start typing using another keypad in linux

    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
    Qt Code:
    1. void LoginGui::CheckKeypadPressed()
    2. {
    3. if(key_Available()==1)
    4. {
    5. int CheckNumber;
    6. CheckNumber=key_Get();
    7.  
    8. if(CheckNumber==1)
    9. {
    10. ui->password->setText(ui->password->text()+"1");
    11. }
    12. if(CheckNumber==2)
    13. {
    14. ui->password->setText(ui->password->text()+"2");
    15. }
    16. if(CheckNumber==3)
    17. {
    18. ui->password->setText(ui->password->text()+"3");
    19. }
    20. if(CheckNumber==4)
    21. {
    22. ui->password->setText(ui->password->text()+"4");
    23. }
    24. if(CheckNumber==5)
    25. {
    26. ui->password->setText(ui->password->text()+"5");
    27. }
    28. if(CheckNumber==6)
    29. {
    30. ui->password->setText(ui->password->text()+"6");
    31. }
    32. if(CheckNumber==7)
    33. {
    34. ui->password->setText(ui->password->text()+"7");
    35. }
    36. if(CheckNumber==8)
    37. {
    38. ui->password->setText(ui->password->text()+"8");
    39. }
    40. if(CheckNumber==9)
    41. {
    42. ui->password->setText(ui->password->text()+"9");
    43. }
    44. }
    To copy to clipboard, switch view to plain text mode 
    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

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to focus on a line edit and start typing using another keypad in linux

    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.
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class Window: public QWidget
    4. {
    5. Q_OBJECT
    6. public:
    7. explicit Window(QWidget *p = 0): QWidget(p) {
    8. QVBoxLayout *layout = new QVBoxLayout(this);
    9. layout->addWidget(new QLineEdit());
    10. layout->addWidget(new QLineEdit());
    11. setLayout(layout);
    12.  
    13. // Organise some fake keyboard activity
    14. QTimer *timer = new QTimer(this);
    15. connect(timer, SIGNAL(timeout()), SLOT(fakeKeyPress()));
    16. timer->setInterval(500);
    17. timer->start();
    18. }
    19. private slots:
    20. void fakeKeyPress() {
    21. QWidget *focussed = QApplication::focusWidget();
    22. if (focussed) {
    23. QKeyEvent press(QEvent::KeyPress, Qt::Key_1, Qt::NoModifier, "1");
    24. QApplication::sendEvent(focussed, &press);
    25. QKeyEvent release(QEvent::KeyRelease, Qt::Key_1, Qt::NoModifier, "1");
    26. QApplication::sendEvent(focussed, &release);
    27. }
    28. }
    29. };
    30.  
    31. int main(int argc, char **argv)
    32. {
    33. QApplication app(argc, argv);
    34. Window w;
    35. w.show();
    36. return app.exec();
    37. }
    38. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    May 2013
    Posts
    47
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to focus on a line edit and start typing using another keypad in linux

    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:


    Quote Originally Posted by ChrisW67 View Post
    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.
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class Window: public QWidget
    4. {
    5. Q_OBJECT
    6. public:
    7. explicit Window(QWidget *p = 0): QWidget(p) {
    8. QVBoxLayout *layout = new QVBoxLayout(this);
    9. layout->addWidget(new QLineEdit());
    10. layout->addWidget(new QLineEdit());
    11. setLayout(layout);
    12.  
    13. // Organise some fake keyboard activity
    14. QTimer *timer = new QTimer(this);
    15. connect(timer, SIGNAL(timeout()), SLOT(fakeKeyPress()));
    16. timer->setInterval(500);
    17. timer->start();
    18. }
    19. private slots:
    20. void fakeKeyPress() {
    21. QWidget *focussed = QApplication::focusWidget();
    22. if (focussed) {
    23. QKeyEvent press(QEvent::KeyPress, Qt::Key_1, Qt::NoModifier, "1");
    24. QApplication::sendEvent(focussed, &press);
    25. QKeyEvent release(QEvent::KeyRelease, Qt::Key_1, Qt::NoModifier, "1");
    26. QApplication::sendEvent(focussed, &release);
    27. }
    28. }
    29. };
    30.  
    31. int main(int argc, char **argv)
    32. {
    33. QApplication app(argc, argv);
    34. Window w;
    35. w.show();
    36. return app.exec();
    37. }
    38. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    I tried some trial and errors with your codes , I tried doing this , Not sure whether it is the right way to do it .
    Qt Code:
    1. void PatientGui:: fakeKeyPress() {
    2. QWidget *focussed = ui->nric->focusWidget();
    3. if (focussed) {
    4. if(key_Available()==1)
    5. {
    6. int CheckNumber;
    7. CheckNumber=key_Get();
    8.  
    9. if(CheckNumber==1)
    10. {
    11. ui->nric->setText(ui->nric->text()+"1");
    12. }
    13. if(CheckNumber==2)
    14. {
    15. ui->nric->setText(ui->nric->text()+"2");
    16. }
    17. if(CheckNumber==3)
    18. {
    19. ui->nric->setText(ui->nric->text()+"3");
    20. }
    21. if(CheckNumber==4)
    22. {
    23. ui->nric->setText(ui->nric->text()+"4");
    24. }
    25. if(CheckNumber==5)
    26. {
    27. ui->nric->setText(ui->nric->text()+"5");
    28. }
    29. if(CheckNumber==6)
    30. {
    31. ui->nric->setText(ui->nric->text()+"6");
    32. }
    33. if(CheckNumber==7)
    34. {
    35. ui->nric->setText(ui->nric->text()+"7");
    36. }
    37. if(CheckNumber==8)
    38. {
    39. ui->nric->setText(ui->nric->text()+"8");
    40. }
    41. if(CheckNumber==9)
    42. {
    43. ui->nric->setText(ui->nric->text()+"9");
    44. }
    45.  
    46. }
    47. QWidget *focussed1 = ui->bednumber->focusWidget();
    48. if (focussed1) {
    49. if(key_Available()==1)
    50. {
    51. int CheckNumber;
    52. CheckNumber=key_Get();
    53.  
    54. if(CheckNumber==1)
    55. {
    56. ui->password->setText(ui->password->text()+"1");
    57. }
    58. if(CheckNumber==2)
    59. {
    60. ui->password->setText(ui->password->text()+"2");
    61. }
    62. if(CheckNumber==3)
    63. {
    64. ui->password->setText(ui->password->text()+"3");
    65. }
    66. if(CheckNumber==4)
    67. {
    68. ui->password->setText(ui->password->text()+"4");
    69. }
    70. if(CheckNumber==5)
    71. {
    72. ui->password->setText(ui->password->text()+"5");
    73. }
    74. if(CheckNumber==6)
    75. {
    76. ui->password->setText(ui->password->text()+"6");
    77. }
    78. if(CheckNumber==7)
    79. {
    80. ui->password->setText(ui->password->text()+"7");
    81. }
    82. if(CheckNumber==8)
    83. {
    84. ui->password->setText(ui->password->text()+"8");
    85. }
    86. if(CheckNumber==9)
    87. {
    88. ui->password->setText(ui->password->text()+"9");
    89. }
    90. }
    91. }
    To copy to clipboard, switch view to plain text mode 
    Can I do this ?
    Last edited by 020394; 13th August 2013 at 04:44.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to focus on a line edit and start typing using another keypad in linux

    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.

  5. #5
    Join Date
    May 2013
    Posts
    47
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to focus on a line edit and start typing using another keypad in linux

    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
    Last edited by 020394; 14th August 2013 at 06:29.

  6. #6
    Join Date
    May 2013
    Posts
    47
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to focus on a line edit and start typing using another keypad in linux

    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 ?

Similar Threads

  1. QTableView line edit clears the text on edit
    By PlasticJesus in forum Qt Programming
    Replies: 5
    Last Post: 14th March 2015, 19:06
  2. Replies: 1
    Last Post: 28th August 2012, 20:11
  3. Replies: 2
    Last Post: 2nd May 2011, 06:33
  4. Is there focus event for line edit?
    By vjsharma_30 in forum Qt Programming
    Replies: 6
    Last Post: 19th February 2010, 20:12
  5. line edit focus
    By addu in forum Qt Programming
    Replies: 3
    Last Post: 16th July 2009, 15:39

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.