Dear visitor, welcome to QtForum.org.
If this is your first visit here, please read the Help. It explains in detail how this page works.
To use all features of this page, you should consider registering.
Please use the registration form, to register here or read more information about the registration process.
If you are already registered, please login here.
How to get arrow keys
hi I'm fedorax
now I get Keycode use QKeyEvent
this is the keyPressEvent function but can't get keycode.
Please help me
-------------------------------------------------------------
void getKeyCode::keyPressEvent(QKeyEvent *event) {
switch(event->key()){
case Qt::Key_Up:
centerLabel->setText(tr("Press ↑"));
break;
case Qt::Key_Left:
centerLabel->setText(tr("Press ←"));
break;
case Qt::Key_Right:
centerLabel->setText(tr("Press →"));
break;
case Qt::Key_Down:
centerLabel->setText(tr("Press ↓"));
break;
case Qt::Key_0:
centerLabel->setText(tr("Press 0"));
break;
}
QString str;
str.setNum(event->key());
centerLabel->setText(str);
}
-------------------------------------------------------------------------
Hello fedorax,
This is a virtual function to the widget that has focus. There isn't enough information here to really tell what your doing or how this is implemented. However, I would recommend that you pass the event along even though the documentation may say differently.
Junior
thx to reply
this is the source code.
I want to get keycode or match Qt::Key_Up.
and for exampe move to charactor to go up or go left as game!
I see 'portedasteroids' that's sample program.
this sample source is connect action to arrow key.
but I want to match arrow key only key press event function better.
fedorax
--- getkeycode.h ---
#ifndef GETKEYCODE_H
#define GETKEYCODE_H
#include <QtGui>
#include <QKeyEvent>
class getKeyCode : public QWidget
{
Q_OBJECT
public:
getKeyCode(QWidget *parent = 0);
~getKeyCode();
void setupUi(QWidget *w);
protected:
void keyPressEvent(QKeyEvent *event);
private:
QLabel *centerLabel;
};
#endif // GETKEYCODE_H
---------------------------
--- getkeycode.cpp ---
---------------------------
#include "getkeycode.h"
getKeyCode::getKeyCode(QWidget *parent)
: QWidget(parent)
{
setupUi(this);
}
getKeyCode::~getKeyCode()
{
}
void getKeyCode::setupUi(QWidget *w)
{
setWindowTitle(tr("Get Key Codes"));
centerLabel = new QLabel(tr("Please Press Key"));
QPushButton *okButton = new QPushButton(tr(" OK "));
connect(okButton, SIGNAL(clicked()), this, SLOT(quit()));
QVBoxLayout *mLay = new QVBoxLayout;
mLay->addWidget(centerLabel);
mLay->addWidget(okButton);
setLayout(mLay);
resize(200,120);
show();
}
void getKeyCode::keyPressEvent(QKeyEvent *event)
{
switch(event->key()){
case Qt::Key_Up:
centerLabel->setText(tr("Press ↑"));
break;
case Qt::Key_Left:
centerLabel->setText(tr("Press ←"));
break;
case Qt::Key_Right:
centerLabel->setText(tr("Press →"));
break;
case Qt::Key_Down:
centerLabel->setText(tr("Press ↓"));
break;
case Qt::Key_0:
centerLabel->setText(tr("Press 0"));
break;
}
QString str;
str.setNum(event->key());
centerLabel->setText(str);
}
-------------------
--- main.cpp ---
-------------------
#include <QtGui/QApplication>
#include "getkeycode.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
getKeyCode w;
w.show();
return a.exec();
}
fedorax,
Changed a few things to get this to work for you, however note that if the pushbutton has focus, the arrow keys will not be filtered. To ensure all events are filtered from the application you would need to investigate on installing a global event filter (installEventFilter()).
getkeycode.h
|
Source code
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
#ifndef GETKEYCODE_H
#define GETKEYCODE_H
#include <QtGui/QWidget>
#include <QtGui/QLabel>
#include <QtGui/QKeyEvent>
class getKeyCode : public QWidget
{
Q_OBJECT
public:
getKeyCode(QWidget *parent = 0);
~getKeyCode();
void setupUi();
protected:
void keyPressEvent(QKeyEvent *event);
private:
QLabel *centerLabel;
};
#endif // GETKEYCODE_H
|
getkeycode.cpp
|
Source code
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
#include <QtGui/QPushButton>
#include <QtGui/QVBoxLayout>
#include "getkeycode.h"
getKeyCode::getKeyCode(QWidget *parent)
: QWidget(parent)
{
setupUi();
}
getKeyCode::~getKeyCode()
{
}
void getKeyCode::setupUi()
{
setWindowTitle(tr("Get Key Codes"));
centerLabel = new QLabel(tr("Please Press Key"));
QPushButton *okButton = new QPushButton(tr(" OK "));
connect(okButton, SIGNAL(clicked()), this, SLOT(close()));
QVBoxLayout *mLay = new QVBoxLayout;
mLay->addWidget(centerLabel);
mLay->addWidget(okButton);
setLayout(mLay);
resize(200,120);
this->setFocus();
}
void getKeyCode::keyPressEvent(QKeyEvent *event)
{
switch(event->key()){
case Qt::Key_Up:
centerLabel->setText(tr("Press ?"));
break;
case Qt::Key_Left:
centerLabel->setText(tr("Press ?"));
break;
case Qt::Key_Right:
centerLabel->setText(tr("Press ?"));
break;
case Qt::Key_Down:
centerLabel->setText(tr("Press ?"));
break;
case Qt::Key_0:
centerLabel->setText(tr("Press 0"));
break;
}
QString str;
str.setNum(event->key());
// centerLabel->setText(str);
}
|
main.cpp
|
Source code
|
1
2
3
4
5
6
7
8
9
10
|
#include <QtGui/QApplication>
#include "getkeycode.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
getKeyCode w;
w.show();
return a.exec();
}
|
Hope this is helpful,
Junior
Thanks a lot!!!
I understand to need setFocus function!
Thank you so very much!!
fedorax