Qt-interest Archive, January 2007
Detecting keystrokes when mousebutton is pressed
Message 1 in thread
I have an application where I need to respond to a key stroke while
a mouse button is pressed. I've created a program that responds
to the individual events (using mousePressEvent & keyPressEvent)
but it does not respond to a key press when the mouse button is
pressed - is there a way around this?
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
--
[ signature omitted ]
Message 2 in thread
Are you calling qapp->processEvents(QEventLoop::AllEvents) occasionally
while the mouse is down? If not, Qt cannot send the events.
Keith
On 01-12-2007 6:15 PM, "David Scriven" wrote:
> I have an application where I need to respond to a key stroke while
> a mouse button is pressed. I've created a program that responds
> to the individual events (using mousePressEvent & keyPressEvent)
> but it does not respond to a key press when the mouse button is
> pressed - is there a way around this?
--
[ signature omitted ]
Message 3 in thread
On 1/12/07, David Scriven <davidwriter@xxxxxxxxx> wrote:
> I have an application where I need to respond to a key stroke while
> a mouse button is pressed. I've created a program that responds
> to the individual events (using mousePressEvent & keyPressEvent)
> but it does not respond to a key press when the mouse button is
> pressed - is there a way around this?
What does your code look like? The following works fine for me - that
is, it receives key events while I hold a mouse button down.
#include <QWidget>
#include <QMouseEvent>
#include <QKeyEvent>
#include <QDebug>
class Event: public QWidget
{
protected:
void keyPressEvent(QKeyEvent* event);
void mousePressEvent(QMouseEvent* event);
};
void Event::keyPressEvent(QKeyEvent* event)
{
qDebug() << "keyPress: " << event->key();
}
void Event::mousePressEvent(QMouseEvent* event)
{
qDebug() << "mouseEvent: (" << event->x() << "," << event->y() << ")";
}
--
[ signature omitted ]
Message 4 in thread
I was a little confused on my first response because I read past the
"mousePressEvent" (mouse went UP) and read the "mouse button is pressed" as
you were holding the mouse button down and processing code.
Andrew is correct, it should work unless you are still processing code in
response to the event when you expect to get keys. Make sure your Widget's
FocusPolicy is set to receive keys (Qt::ClickFocus, Qt::StrongFocus or
Qt::WheelFocus).
Keith
On 01-13-2007 1:54 AM, "Andrew Medico" wrote:
> On 1/12/07, David Scriven <davidwriter@xxxxxxxxx> wrote:
>> I have an application where I need to respond to a key stroke while
>> a mouse button is pressed. I've created a program that responds
>> to the individual events (using mousePressEvent & keyPressEvent)
>> but it does not respond to a key press when the mouse button is
>> pressed - is there a way around this?
>
> What does your code look like? The following works fine for me - that
> is, it receives key events while I hold a mouse button down.
>
> #include <QWidget>
> #include <QMouseEvent>
> #include <QKeyEvent>
> #include <QDebug>
>
> class Event: public QWidget
> {
> protected:
> void keyPressEvent(QKeyEvent* event);
> void mousePressEvent(QMouseEvent* event);
> };
>
> void Event::keyPressEvent(QKeyEvent* event)
> {
> qDebug() << "keyPress: " << event->key();
> }
>
> void Event::mousePressEvent(QMouseEvent* event)
> {
> qDebug() << "mouseEvent: (" << event->x() << "," << event->y() << ")";
> }
--
[ signature omitted ]
Message 5 in thread
The problem was that I was losing focus - I had to
do a grabKeyboard() when the mouse button was pressed
and a releaseKeyboard() when it was released
----- Original Message ----
From: Keith Esau <keith.esau@xxxxxxx>
To:
Sent: Friday, January 12, 2007 9:45:42 PM
Subject: Re: Detecting keystrokes when mousebutton is pressed
Are you calling qapp->processEvents(QEventLoop::AllEvents) occasionally
while the mouse is down? If not, Qt cannot send the events.
Keith
On 01-12-2007 6:15 PM, "David Scriven" wrote:
> I have an application where I need to respond to a key stroke while
> a mouse button is pressed. I've created a program that responds
> to the individual events (using mousePressEvent & keyPressEvent)
> but it does not respond to a key press when the mouse button is
> pressed - is there a way around this?
--
[ signature omitted ]