1

I am using KeyPressEvent in my applicatiion. But letters from a to z are not working.

void mywindow::keyPressEvent(QKeyEvent *event)
{
    switch(event->key())
    {
        case Qt::Key_Left:
            break;
    }
}

This is working properly

But when I'm using Key_R or Key_L it is not working.

Edit: keyReleaseEvent works with those letters.

László Papp
  • 51,870
  • 39
  • 111
  • 135
Suresh
  • 745
  • 4
  • 11
  • 25
  • This will not be compiled. – Nikita Jan 09 '14 at 09:24
  • 1
    Key_Left nad Key_L are not the same. Key_L is the 'L' pressed, Key_Left is the left arrow. Either way, do you have any other event filter taking over certain key presses? – László Papp Jan 09 '14 at 09:36
  • 1
    Can you post all code of this class? Maybe something wrong in another place. – DeadWarlock Jan 09 '14 at 09:51
  • I am telling all the letters a to z are not working. left key, right key, up and down they are working – Suresh Jan 09 '14 at 09:54
  • Code which you write must work correctly with letters too (Key_R or Key_L does not matter). I suppose you have something wrong in another place (maybe eventFilter) – DeadWarlock Jan 09 '14 at 10:02
  • I have not used eventFilter. Escape, Alt key are also working fine – Suresh Jan 09 '14 at 10:03
  • You understand my opinion: Normal people can not find error in whole code which you write looking only in little method which you show and not have that error. Can you post whole code? – DeadWarlock Jan 09 '14 at 10:16
  • 2
    Explain what you mean by "not working". No event arrives? Event with a wrong key (which one)? Compilation error? Also post a complere compilable example. – n. m. could be an AI Jan 09 '14 at 10:16
  • I mean when i am pressing A,B or any other letters then event are not executing under them. Code is same @DeadWarlock. Just i have added Key_A, Key_B similar to Key_Left. – Suresh Jan 09 '14 at 10:23
  • Rupesh, I am voting for closure until you can provide an sscce.org compliant example. Once, you do that, I am happy to help myself further on. – László Papp Jan 09 '14 at 10:34
  • Sorry but i am very new to qt and C++. I have to work for my office, so i am asking – Suresh Jan 09 '14 at 10:39

2 Answers2

1

Try this:

void SimpleWidget::keyPressEvent(QKeyEvent *event)
{
    if (event->key() == Qt::Key_R)
    {
        // Key R was pressed
    }
}

Or you can simply check key value using QString QKeyEvent::text () const method.

Maxim Makhun
  • 2,197
  • 1
  • 22
  • 26
  • 4
    @LaszloPapp, to be fair, the OP changed his question after Maxim had replied. – TheDarkKnight Jan 09 '14 at 09:36
  • 4
    @LaszloPapp you'd better compile my code and check before voting down my answer – Maxim Makhun Jan 09 '14 at 09:37
  • 1
    @Merlin: original content, http://stackoverflow.com/revisions/f8076eed-2bb7-46c8-972b-408160c04c13/view-source So, the point remained the same, but even if it did not, this answer has no value now, so I would remove it if there are no real answers instead. – László Papp Jan 09 '14 at 09:37
1

Not sure you still need it, but maybe someone will.

I was in the same situation, when pressing a to z letters none event was sent to my QMainWindow, but ctrl, cmd, alt or esc... was working.

I solved the problem adding this in the constructor.

this->setFocusPolicy ( Qt::StrongFocus );

read setFocusPolicy doc

then

MyQMainWindow::keyPressEvent(:keyPressEvent(QKeyEvent *pevent)
{
    if (pevent->key() == Qt::Key_Control)
        qDebug() << "ctrl pressed";
    if (pevent->key() == Qt::Key_A)
        qDebug() << "a pressed";
}
epicanriome
  • 21
  • 2
  • 4