PDA

View Full Version : How to interchange the behaviour of the Keys



gunturrohith
5th December 2015, 06:51
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


#ifndef WIDGET_H
#define WIDGET_H

#include <QtWidgets>
#include <QEvent>
#include<QTextEdit>
#include<QMessageBox>
class Widget : public QWidget
{
Q_OBJECT

public:

Widget(QWidget *parent = 0);
// QMessageBox box;
// QLabel *txt;
~Widget();
protected:
bool event(QEvent *);
};

#endif // WIDGET_H


Here is My ".cpp" File



#include "widget.h"
#include <QDebug>
#include <QKeyEvent>
#include<QTextEdit>
#include<QString>
#include<QMessageBox>
QString str,str1,str2;
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
qDebug() << "Constructor";

//txt= new QLabel(this);
//txt->setGeometry(0,0,320,240);
}

Widget::~Widget()
{
qDebug() << "Destructor";
}

bool Widget::event(QEvent *e){
//int ret;

// qDebug()<<"line num is"<<__LINE__;

if(e->type()==QEvent::KeyRelease){
QKeyEvent *ke = static_cast<QKeyEvent *>(e);
int keyValR = ke->key();
qDebug() << "Released key value is : " << keyValR;

/* QString str = QString::number(keyValR);
qDebug()<<str;*/
switch (keyValR) {
case Qt::Key_F1 :
//qDebug()<<__LINE__;
qDebug() << "You have PRESSED :: F1";
//qDebug() << "You have PRESSED :: F1";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F1</font></b>");
break;
//case 16777265:
case Qt::Key_F2 :
qDebug() << "You have PRESSED :: F2";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F2</font></b>");
break;
case Qt::Key_F3 :
qDebug() << "You have PRESSED :: F3";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F3</font></b>");
break;
case Qt::Key_F4 :
qDebug() << "You have PRESSED :: F4";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F4</font></b>");
break;
case Qt::Key_F5 :
qDebug() << "You have PRESSED :: F5";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F5</font></b>");
case Qt::Key_F6 :
qDebug() << "You have PRESSED :: F6";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F6</font></b>");
break;
case Qt::Key_F7 :
qDebug() << "You have PRESSED :: F7";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F7</font></b>");
break;
case Qt::Key_F8 :
qDebug() << "You have PRESSED :: F8";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F8</font></b>");
break;
case Qt::Key_F9 :
qDebug() << "You have PRESSED :: F9";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F9</font></b>");
break;
case Qt::Key_F10 :
qDebug() << "You have PRESSED :: F10";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F10</font></b>");
break;
case Qt::Key_F11 :
qDebug() << "You have PRESSED :: F11";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F11</font></b>");
break;
case Qt::Key_F12 :
qDebug() << "You have PRESSED :: F12";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F12</font></b>");
break;
case Qt::Key_Return :
qDebug() << "You have PRESSED :: RETURN";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>RETURN</font></b>");
break;
case Qt::Key_Enter :
qDebug() << "You have PRESSED :: ENTER";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>ENTER</font></b>");
break;

case Qt::Key_Space :
qDebug() << "You have PRESSED :: SPACE";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>SPACE</font></b>");
break;
case Qt::Key_Shift :
qDebug() << "You have PRESSED :: SHIFT";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>SHIFT</font></b>");
break;
case Qt::Key_CapsLock :
qDebug() << "You have PRESSED :: CAPSLOCK";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>CAPSLOCK</font></b>");
break;
case Qt::Key_Delete :
qDebug() << "You have PRESSED :: DELETE";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>DELETE</font></b>");
break;

case Qt::Key_Left:
qDebug() << "You have PRESSED :: <-";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red> <- </font></b>");
break;
case Qt::Key_Right:
qDebug() << "You have PRESSED ::->";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>-></font></b>");
break;
case Qt::Key_Down:
qDebug() << "You have PRESSED :: uparrow";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>UPARROW</font></b>");
break;
case Qt::Key_Up :
qDebug() << "You have PRESSED :: DownArrow";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>DownArrow</font></b>");
default:
str = ke->text();
qDebug() << "You have PRESSED :: " << str;
str = str.prepend("You have PRESSED :: <b><font color = red>");
str = str.append("</font></b>");
QMessageBox::information(this,"key pressed", str);
break;
}



}



Please help me ASAP
Thanks in advance

Regards,
Rohith.G

anda_skoa
5th December 2015, 11:34
- 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,
_

gunturrohith
7th December 2015, 06:12
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


void keyPressEvent(QKeyEvent* e)
{
qDebug("before functionality");
QMessageBox* box = new QMessageBox();
box->setWindowTitle(QString("Hello"));
box->setText(QString("You Pressed: ")+ e->text());
int keyValR = e->key();
qDebug()<<"your key value is"<<keyValR;

QKeyEvent *k = (QKeyEvent*)e;
Qt::Key keyPressed = (Qt::Key)k->key();


if(keyPressed==Qt::Key_Left)
{
qDebug()<<"inside if";
QKeyEvent key(QEvent::KeyPress,Qt::Key_Right,0,0,0);
QApplication::sendEvent(this,&key);

QTextEdit *edit = new QTextEdit;
edit->setText("divya");
edit->show();
}
qDebug()<<"before box display";
//box->show();


Thanks & Regards,
Rohith.G

anda_skoa
7th December 2015, 08:30
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,
_

Vikram.Saralaya
7th December 2015, 08:33
The code snippet you sent works fine.

The output I get when I run it:

before functionality
your key value is 16777234
inside if
before functionality
your key value is 16777236
before box display
before box display

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

gunturrohith
7th December 2015, 10:08
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

Vikram.Saralaya
7th December 2015, 10:14
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.


keyPressEvent(QKeyEvent* e) 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
QCursor::setPos() to move it.

anda_skoa
7th December 2015, 10:54
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,
_