PDA

View Full Version : Query keyboard state in Qt 3



Pieter from Belgium
9th February 2006, 16:32
At a certain point in the program I would like to find out whether or not the Control key is pressed.
(How) can I do this in Qt3?

So I am looking for a Qt 3 approach for Qt 4's QApplication::keyboardModifiers().

One solution I can think of is to filter all keyboard events and to do explicit bookkeeping of whether the CTRL key is pressed, but I wonder if there is a simpler approach.

Pieter

zlatko
9th February 2006, 17:05
just example from QAsistant


bool MyMainWindow::eventFilter( QObject *obj, QEvent *ev )
{
if ( obj == textEdit ) {
if ( e->type() == QEvent::KeyPress ) {
QKeyEvent *k = (QKeyEvent*)ev;
qDebug( "Ate key press %d", k->key() );
return TRUE;
} else {
return FALSE;
}
} else {
// pass the event on to the parent class
return QMainWindow::eventFilter( obj, ev );
}
}


read about eventFilter()

Chicken Blood Machine
9th February 2006, 18:20
At a certain point in the program I would like to find out whether or not the Control key is pressed.
(How) can I do this in Qt3?

So I am looking for a Qt 3 approach for Qt 4's QApplication::keyboardModifiers().


I needed the same thing. Here's my implementation (I subclassed QApplication to do this).


#ifdef Q_WS_WIN
# include <windows.h>
# include <shellapi.h>
#else /* Q_WS_X11 */
# include <X11/Xlib.h>
#endif
#include <qpaintdevice.h>

// Implement a way to easily deduce which mousebuttons and modifier
// keys are held down
static Qt::ButtonState get_modifiers()
{
int result = Qt::NoButton;

#ifdef Q_WS_WIN

result |= GetAsyncKeyState(VK_SHIFT) < 0 ? Qt::ShiftButton : 0;
result |= GetAsyncKeyState(VK_MENU) < 0 ? Qt::AltButton : 0;
result |= GetAsyncKeyState(VK_CONTROL) < 0 ? Qt::ControlButton : 0;

// Check if left and right mousebuttons have been swapped
bool swap_buttons = GetSystemMetrics(SM_SWAPBUTTON) != 0;

if (swap_buttons)
{
result |= GetAsyncKeyState(VK_LBUTTON) < 0 ? Qt::RightButton : 0;
result |= GetAsyncKeyState(VK_RBUTTON) < 0 ? Qt::LeftButton : 0;
}
else
{
result |= GetAsyncKeyState(VK_LBUTTON) < 0 ? Qt::LeftButton : 0;
result |= GetAsyncKeyState(VK_RBUTTON) < 0 ? Qt::RightButton : 0;
}
result |= GetAsyncKeyState(VK_MBUTTON) < 0 ? Qt::MidButton : 0;

#else /* Q_WS_X11 */

Window root, child;
int root_x, root_y;
int win_x, win_y;
uint keys_buttons;
bool status = XQueryPointer(QPaintDevice::x11AppDisplay(),
QPaintDevice::x11AppRootWindow(),
&root, &child,
&root_x, &root_y,
&win_x, &win_y,
&keys_buttons);

if (status)
{
result |= keys_buttons & ShiftMask ? Qt::ShiftButton : 0;
result |= keys_buttons & Mod1Mask ? Qt::AltButton : 0;
result |= keys_buttons & Mod4Mask ? Qt::MetaButton : 0;
result |= keys_buttons & ControlMask ? Qt::ControlButton : 0;

result |= keys_buttons & Button1Mask ? Qt::LeftButton : 0;
result |= keys_buttons & Button2Mask ? Qt::MidButton : 0;
result |= keys_buttons & Button3Mask ? Qt::RightButton : 0;
}
#endif
return Qt::ButtonState(result);
}

Qt::ButtonState MyApplication::keyboardModifiers()
{
return Qt::ButtonState(get_modifiers() & Qt::KeyButtonMask);
}

Qt::ButtonState MyApplication::mouseButtons()
{
return Qt::ButtonState(get_modifiers() & Qt::MouseButtonMask);
}




One solution I can think of is to filter all keyboard events and to do explicit bookkeeping of whether the CTRL key is pressed, but I wonder if there is a simpler approach.

That's exactly how they implement it in Qt4!