PDA

View Full Version : Capture a keyboard event



mahe2310
15th February 2006, 10:14
Hi all,

Can anybody help me to know how to capture keyboard input.

QKeyEvent class i refered. But i want to know how to initialize it to capture an event.

Plz help...

Mahe2310

zlatko
15th February 2006, 11:27
use eventFilter() or keyPressEvent()

mahe2310
15th February 2006, 12:10
hi,

I didn't get how this process is being taking place...

Can you provide me a sample program that takes a keybord input on the window like up arrow press and message "Up arrow Pressed"...

Plz do consider...

dec0ding
15th February 2006, 14:19
For Qt 3.3.x:
There is great example in the Documentation. U can simply use QWidget::keyPressEvent()


void PictureDisplay::keyPressEvent( QKeyEvent *k )
{
switch ( tolower(k->ascii()) ) {
case 'r': // reload
pict->load( name );
update();
break;
case 'q': // quit
QApplication::exit();
break;
}
}

Here: http://doc.trolltech.com/3.3/picture-example.html#x127

mahe2310
16th February 2006, 05:55
hi,

I gone thru the codes you given and understood the concept...
Thank you...

Now i experienced a problem that all keys except arrow keys are detected by QKeyEvent...
Can you tell why arrow keys are not detected...

Or whether the arrow key press are not among QKeyEvent...

Help me plz...


Mahesh



For Qt 3.3.x:
There is great example in the Documentation. U can simply use QWidget::keyPressEvent()


void PictureDisplay::keyPressEvent( QKeyEvent *k )
{
switch ( tolower(k->ascii()) ) {
case 'r': // reload
pict->load( name );
update();
break;
case 'q': // quit
QApplication::exit();
break;
}
}

Here: http://doc.trolltech.com/3.3/picture-example.html#x127

high_flyer
16th February 2006, 09:29
Can you show your code that is dealing with the key events?
If you are using ascii() for these keys it wont work since they don't have an ascii() representation, you should use the Qt::Key value.

mahe2310
16th February 2006, 10:10
Can you show your code that is dealing with the key events?
If you are using ascii() for these keys it wont work since they don't have an ascii() representation, you should use the Qt::Key value.

I could receive the arrow press event but on enter press nothing is happening except a repeating of
other events (defined in the code).....

key_press is my global variable

void MyWidget::keyPressEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_Up) {
key_press = -1;
}am able to
else if(event->key() == Qt::Key_Down) {
key_press = 1;
}am able to
else if(event->key() == Qt::Key_Enter) {
key_press = 3; // But this part of the code is not reached on enter key press
}
else {
QWidget::keyPressEvent(event);
}

// other events....

}

lumber44
16th February 2006, 10:57
I had a similiar problem a day ago, i posted my solution in the end how to catch Tab, enter and space and so on.

http://www.qtcentre.org/forum/showthread.php?t=670

mahe2310
16th February 2006, 12:19
I had a similiar problem a day ago, i posted my solution in the end how to catch Tab, enter and space and so on.

http://www.qtcentre.org/forum/showthread.php?t=670


thanks lumber... That is a good one...

Mahe2310