PDA

View Full Version : Keyboard Capturing



ToddAtWSU
28th June 2007, 23:00
I am trying to have a QMainWindow capture keyPressEvents Qt::Key_Up and Qt::Key_Down. Unfortunately in my keyPressEvent( ) function, the print statements I have for these 2 keys do not print. My main window contains about 12 push buttons, 2 sliders, and 2 QGLWidgets. Upon further observation I noticed my 2nd slider bar was moving with the up and down keys.

Is there a way to get my sliders and push buttons from accpeting the key presses so my main window can receive them? My keyPressEvent looks like this:



void PlotWindow::keyPressEvent( QKeyEvent *evnt )
{
switch( evnt->key( ) )
{
case Qt::Key_Control:
evnt->accept( );
mpFirstPlot->setControlKeyPressed( true );
break;
case Qt::Key_Up:
evnt->accept( );
fprintf( stderr, "up pressed\n" );
break;
case Qt::Key_Down:
evnt->accept( );
fprintf( stderr, "down pressed\n" );
break;
default:
evnt->ignore( );
}
}

Capturing the Control key works fine but I do not get anything to capture the up and down keys. Thanks for your help!

Rayven
29th June 2007, 02:09
Try subclassing the QSlider and reimplementing the keyPressEvent handler to ignore all incoming events.



void NewSlider::keyPressEvent( QKeyEvent *evnt )
{
evnt->ignore( );
}

jpn
29th June 2007, 07:37
One option could be to install an event filter on the application object. This way you'll get the events before they reach any widget, though.