| Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date | |
| All threads index page 1 | |
<<< text/html: EXCLUDED >>>
Quoth Tim Harvey (on 02/03/04) :
>
> This whole mess would be a ton simpler if there were a way for me to get
> the state of the keyboard asynchronously while a drag operation is
> taking place as all I want to do is perform a different operation
> depending on the state of the control key (and provide feedback by
> altering the drag icon). I've installed an event handler to catch
> keypress events and hold the state of the Control key, however I've
> found that while a drag is taking place, the event handler doesn't get
> called. argh! Is there still no way to query the state of the keyboard?
> I guess my next step is to break down and do a platform specific thing
> to query it.
This is a simple function I wrote to query the state of the keyboard
modifiers :
int ModifierState()
{
// Get the current state of the mouse and keyboard modifiers
int modifierState = 0;
#ifdef PLATFORM_WINTEL
BYTE stateBuffer[256];
GetKeyboardState(stateBuffer);
// Do the keyboard modifiers
if (stateBuffer[VK_MENU] & 0x80) modifierState |= Qt::AltButton;
if (stateBuffer[VK_SHIFT] & 0x80) modifierState |= Qt::ShiftButton;
if (stateBuffer[VK_CONTROL] & 0x80) modifierState |= Qt::ControlButton;
#else
Display *display = qt_xdisplay();
Window windowID = qApp->desktop()->winId();
Window root, child; // These are just unused dummy variables.
int rootX, rootY;
int state;
int cursorX, cursorY;
XQueryPointer(display, windowID, &root, &child, &rootX, &rootY,
&cursorX, &cursorY, &state);
if (state & Mod1Mask) modifierState |= Qt::AltButton;
if (state & ShiftMask) modifierState |= Qt::ShiftButton;
if (state & ControlMask) modifierState |= Qt::ControlButton;
#endif
return modifierState;
}
This works for NT and UNIX; if you need it on Mac too then you're on your
own. The function may be called at any time, like so :
if (ModifierState() & Qt::ControlButton) // the Control key is down...
Similarly, as Mike Stief was asking a few days ago, it may be used in
a keyPressEvent to determine whether the Shift key is down, although
in that case that may more easily be determined by simply using the
QKeyEvent::state() method.
HTH,
Anthony
Maptek Adelaide
Anthony Gibbs -- Software Engineer
Email: Anthony.Gibbs@maptek.com.au Phone: +61 8 8338 9225
Dale Cooper: [speaking into tape recorder] Diane, I'm
holding in my hands a small box of chocolate bunnies