Results 1 to 8 of 8

Thread: How to interchange the behaviour of the Keys

  1. #1
    Join Date
    Nov 2014
    Posts
    54
    Thanks
    5
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default How to interchange the behaviour of the Keys

    Hi,

    I need to write an application that helps me to interchange the behaviour of the keys,
    i.e I need to swap the functionality of the both up arrow and down arrow keys that are present in the keyboard and i am trying to do it using the QKeyEvent.
    Is it possible to do so,here is my code please go through it and suggest me where i have done wrong

    Here is My HeaderFile
    Qt Code:
    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3.  
    4. #include <QtWidgets>
    5. #include <QEvent>
    6. #include<QTextEdit>
    7. #include<QMessageBox>
    8. class Widget : public QWidget
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13.  
    14. Widget(QWidget *parent = 0);
    15. // QMessageBox box;
    16. // QLabel *txt;
    17. ~Widget();
    18. protected:
    19. bool event(QEvent *);
    20. };
    21.  
    22. #endif // WIDGET_H
    To copy to clipboard, switch view to plain text mode 

    Here is My ".cpp" File

    Qt Code:
    1. #include "widget.h"
    2. #include <QDebug>
    3. #include <QKeyEvent>
    4. #include<QTextEdit>
    5. #include<QString>
    6. #include<QMessageBox>
    7. QString str,str1,str2;
    8. Widget::Widget(QWidget *parent)
    9. : QWidget(parent)
    10. {
    11. qDebug() << "Constructor";
    12.  
    13. //txt= new QLabel(this);
    14. //txt->setGeometry(0,0,320,240);
    15. }
    16.  
    17. Widget::~Widget()
    18. {
    19. qDebug() << "Destructor";
    20. }
    21.  
    22. bool Widget::event(QEvent *e){
    23. //int ret;
    24.  
    25. // qDebug()<<"line num is"<<__LINE__;
    26.  
    27. if(e->type()==QEvent::KeyRelease){
    28. QKeyEvent *ke = static_cast<QKeyEvent *>(e);
    29. int keyValR = ke->key();
    30. qDebug() << "Released key value is : " << keyValR;
    31.  
    32. /* QString str = QString::number(keyValR);
    33.   qDebug()<<str;*/
    34. switch (keyValR) {
    35. case Qt::Key_F1 :
    36. //qDebug()<<__LINE__;
    37. qDebug() << "You have PRESSED :: F1";
    38. //qDebug() << "You have PRESSED :: F1";
    39. QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F1</font></b>");
    40. break;
    41. //case 16777265:
    42. case Qt::Key_F2 :
    43. qDebug() << "You have PRESSED :: F2";
    44. QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F2</font></b>");
    45. break;
    46. case Qt::Key_F3 :
    47. qDebug() << "You have PRESSED :: F3";
    48. QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F3</font></b>");
    49. break;
    50. case Qt::Key_F4 :
    51. qDebug() << "You have PRESSED :: F4";
    52. QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F4</font></b>");
    53. break;
    54. case Qt::Key_F5 :
    55. qDebug() << "You have PRESSED :: F5";
    56. QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F5</font></b>");
    57. case Qt::Key_F6 :
    58. qDebug() << "You have PRESSED :: F6";
    59. QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F6</font></b>");
    60. break;
    61. case Qt::Key_F7 :
    62. qDebug() << "You have PRESSED :: F7";
    63. QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F7</font></b>");
    64. break;
    65. case Qt::Key_F8 :
    66. qDebug() << "You have PRESSED :: F8";
    67. QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F8</font></b>");
    68. break;
    69. case Qt::Key_F9 :
    70. qDebug() << "You have PRESSED :: F9";
    71. QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F9</font></b>");
    72. break;
    73. case Qt::Key_F10 :
    74. qDebug() << "You have PRESSED :: F10";
    75. QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F10</font></b>");
    76. break;
    77. case Qt::Key_F11 :
    78. qDebug() << "You have PRESSED :: F11";
    79. QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F11</font></b>");
    80. break;
    81. case Qt::Key_F12 :
    82. qDebug() << "You have PRESSED :: F12";
    83. QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F12</font></b>");
    84. break;
    85. case Qt::Key_Return :
    86. qDebug() << "You have PRESSED :: RETURN";
    87. QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>RETURN</font></b>");
    88. break;
    89. case Qt::Key_Enter :
    90. qDebug() << "You have PRESSED :: ENTER";
    91. QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>ENTER</font></b>");
    92. break;
    93.  
    94. case Qt::Key_Space :
    95. qDebug() << "You have PRESSED :: SPACE";
    96. QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>SPACE</font></b>");
    97. break;
    98. case Qt::Key_Shift :
    99. qDebug() << "You have PRESSED :: SHIFT";
    100. QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>SHIFT</font></b>");
    101. break;
    102. case Qt::Key_CapsLock :
    103. qDebug() << "You have PRESSED :: CAPSLOCK";
    104. QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>CAPSLOCK</font></b>");
    105. break;
    106. case Qt::Key_Delete :
    107. qDebug() << "You have PRESSED :: DELETE";
    108. QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>DELETE</font></b>");
    109. break;
    110.  
    111. case Qt::Key_Left:
    112. qDebug() << "You have PRESSED :: <-";
    113. QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red> <- </font></b>");
    114. break;
    115. case Qt::Key_Right:
    116. qDebug() << "You have PRESSED ::->";
    117. QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>-></font></b>");
    118. break;
    119. case Qt::Key_Down:
    120. qDebug() << "You have PRESSED :: uparrow";
    121. QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>UPARROW</font></b>");
    122. break;
    123. case Qt::Key_Up :
    124. qDebug() << "You have PRESSED :: DownArrow";
    125. QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>DownArrow</font></b>");
    126. default:
    127. str = ke->text();
    128. qDebug() << "You have PRESSED :: " << str;
    129. str = str.prepend("You have PRESSED :: <b><font color = red>");
    130. str = str.append("</font></b>");
    131. QMessageBox::information(this,"key pressed", str);
    132. break;
    133. }
    134.  
    135.  
    136.  
    137. }
    To copy to clipboard, switch view to plain text mode 

    Please help me ASAP
    Thanks in advance

    Regards,
    Rohith.G

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to interchange the behaviour of the Keys

    - Detect the event that you want to change
    - Create a new QKeyEvent with the different key
    - Use QCoreApplication::sendEvent() to sent the new event
    - Return from event processing without passing the original event to the base class event handler.

    Cheers,
    _

  3. #3
    Join Date
    Nov 2014
    Posts
    54
    Thanks
    5
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to interchange the behaviour of the Keys

    Thnaks for replying
    Seems like i have followed the same procedure that you have mention,here is my code once please go through it
    if possible can you please provide the above procedure in the form a sample example code
    Qt Code:
    1. void keyPressEvent(QKeyEvent* e)
    2. {
    3. qDebug("before functionality");
    4. QMessageBox* box = new QMessageBox();
    5. box->setWindowTitle(QString("Hello"));
    6. box->setText(QString("You Pressed: ")+ e->text());
    7. int keyValR = e->key();
    8. qDebug()<<"your key value is"<<keyValR;
    9.  
    10. QKeyEvent *k = (QKeyEvent*)e;
    11. Qt::Key keyPressed = (Qt::Key)k->key();
    12.  
    13.  
    14. if(keyPressed==Qt::Key_Left)
    15. {
    16. qDebug()<<"inside if";
    17. QKeyEvent key(QEvent::KeyPress,Qt::Key_Right,0,0,0);
    18. QApplication::sendEvent(this,&key);
    19.  
    20. QTextEdit *edit = new QTextEdit;
    21. edit->setText("divya");
    22. edit->show();
    23. }
    24. qDebug()<<"before box display";
    25. //box->show();
    To copy to clipboard, switch view to plain text mode 

    Thanks & Regards,
    Rohith.G

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to interchange the behaviour of the Keys

    More or less.

    When sending the new event you will also need to "remember" that this is now one of your events.
    It will get into the same keyPressEvent() method and you don't want to handle it like events coming from they keyboard, i.e. you want to pass it on to the base class.

    I would suggest a simple boolean flag member in the class:
    - set the variable to true before calling send event
    - set it to false after send event
    - at the beginning of keyPressEvent check if the variable is true.if yes, just pass event to the base class and return, otherwise do your key checking.

    Cheers,
    _

  5. #5
    Join Date
    May 2015
    Posts
    66
    Thanks
    10
    Thanked 17 Times in 17 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to interchange the behaviour of the Keys

    The code snippet you sent works fine.

    The output I get when I run it:
    Qt Code:
    1. before functionality
    2. your key value is 16777234
    3. inside if
    4. before functionality
    5. your key value is 16777236
    6. before box display
    7. before box display
    To copy to clipboard, switch view to plain text mode 

    May be make sure the proper widget is receiving the event. Rest everything is fine.

  6. #6
    Join Date
    Nov 2014
    Posts
    54
    Thanks
    5
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to interchange the behaviour of the Keys

    Hi Vikram,
    thanks for replying..!
    I too got output for the code snippet that i have posted.
    But the problem is i am unable to see change in the behaviour of the keys, is it possible to do so.
    i.e if is press on uparrow the cursor should move downwards and if i press on downarrow the cursor should move upwards this is my required output.

    Thanks & Regards,
    Rohith.G

  7. #7
    Join Date
    May 2015
    Posts
    66
    Thanks
    10
    Thanked 17 Times in 17 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to interchange the behaviour of the Keys

    • Its not clear to me what is the "cursor" that you are referring to.
    • But if the code snippet above works then you basically managed to simulate a up key press when the user presses downwards.
    • Qt Code:
      1. keyPressEvent(QKeyEvent* e)
      To copy to clipboard, switch view to plain text mode 
      will be invoked with the inverse key and you can do whatever you want here.


    Ps: If you are talking about the mouse cursor, you can use
    Qt Code:
    1. QCursor::setPos()
    To copy to clipboard, switch view to plain text mode 
    to move it.

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to interchange the behaviour of the Keys

    Quote Originally Posted by gunturrohith View Post
    But the problem is i am unable to see change in the behaviour of the keys, is it possible to do so.
    i.e if is press on uparrow the cursor should move downwards and if i press on downarrow the cursor should move upwards this is my required output.
    It might make sense to read all of the responses, some might contain additional input.

    Cheers,
    _

Similar Threads

  1. QMap, keys()
    By mugi in forum Newbie
    Replies: 2
    Last Post: 28th August 2012, 11:37
  2. QtableWidget and hot keys
    By sawerset in forum Qt Programming
    Replies: 1
    Last Post: 8th December 2008, 19:29
  3. How to identify Num pad keys
    By sanjayshelke in forum Qt Programming
    Replies: 1
    Last Post: 2nd September 2008, 17:43
  4. Using Short cut keys
    By joseph in forum Qt Programming
    Replies: 14
    Last Post: 18th March 2008, 19:34
  5. QHash with 2 keys?
    By MrGarbage in forum Qt Programming
    Replies: 5
    Last Post: 6th September 2007, 01:09

Tags for this Thread

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.