3

I would like to be able to check if the key is pressed at any time. I imagine such a solution:

void MyQQuickPaintedItem::keyPressEvent(QKeyEvent * event)
{

  isKeyPressed[ event->key() ] = 1;

}

void MyQQuickPaintedItem::keyReleaseEvent(QKeyEvent *event)
{

  isKeyPressed[ event->key() ] = 0;

}

To check if the right arrow key is pressed it would be enough to check isKeyPressed[ Qt::Key_Right ] value.

I implemented it and... it doesn't work. I don't mean that program crashes. isKeyPressed[ Qt::Key_Right ] is just always 0, even if I'm pressing this right arrow key or any other key.

EDIT:

One of header files:

...
bool isKeyPressed[255];
...

One of linked files:

...
extern bool isKeyPressed[255];
...

I don't know exactly how big isKeyPressed should be, but I don't get SIGSEGV, so the size is probably ok.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
yomol777
  • 463
  • 4
  • 16
  • And when you used your debugger to run your program, what did you see? This is precisely what a debugger is for. If you don't know how to use a debugger this is a good opportunity to learn how to use it to run your program one line at a time, monitor all variables and their values as they change, and analyse your program's logical execution flow. Knowing how to use a debugger is a required skill for every C++ developer, no exceptions. With your debugger's help you should be able to quickly find all bugs in this and all future programs you write, without having to ask anyone for help. – Sam Varshavchik May 06 '20 at 10:20
  • 1
    You need to provide more context (preferably a [mcve]). Generally speaking, an application will only receive key press/release events if it currently has the input focus. – G.M. May 06 '20 at 10:24
  • OK, so the method is good and I have a bug probably in another part of the program? The code is really very simple. These key...event functions certainly work (I tested them), and another part of the program only checks the value of isKeyPressed[ Qt::Key_Right ]. – yomol777 May 06 '20 at 10:29
  • You didn't even mention the type of `isKeyPressed`. This makes it hard to justice whether this may work or is just Undefined Behavior (which may or may not crash incidentally). – Scheff's Cat May 06 '20 at 10:42
  • 1
    It's a mistake to believe that there are only 255 key codes. [QKeyEvent::key()](https://doc.qt.io/qt-5/qkeyevent.html#key) returns `int` (which is probably 32 bit) and the first value in [Qt::Key](https://doc.qt.io/qt-5/qt.html#Key-enum) is `Qt::Key_Escape = 0x01000000`. It might be a better idea to remember pressed keys in a `std::set` from which they are removed in `keyReleaseEvent()`. – Scheff's Cat May 06 '20 at 10:55
  • OK, `std::unordered_map isKeyPressed;` works for me but why didn't I get SIGSEGV? – yomol777 May 06 '20 at 12:16

2 Answers2

3

you normally dont address the problem like that... at least not using QT...

if you are interested to "catch" some key pressed events, then Qt offers ways to do that

what you can do is "connect" the shortcut to a lambda or a slot and inside there do what ever you need... like e.g. catching when the user press "control + i"

connect(new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_I), this), &QShortcut::activated, [](){qDebug() << "Here we are!";});
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

Instead of an array you can use the map, if you are not interested in the order then you can use unordered_maps which is faster. There are rather few keys so I think the program will run fast anyway.

yomol777
  • 463
  • 4
  • 16