This problem is more complicated than you think. It's called keyboard ghosting. To make sure multiple keys work when pressed simultaneously,you may need to have a specialized keyboard that provides anti-ghosting feature, or you may have to remap the Keyboard as per this article. Read it here for more detailed information.
C++ Differentiating Key Press vs Key Release
Question
I'm currently listening to keyboard inputs through a getch() while loop and everything is working great. However, if I'm holding left and press up, then the left movement is stopped until I press it again. The only way I can think of solving this requires knowing if a key is being held / when it is released. I'm using ncurses which supposedly has this ability, but through all my searches I haven't found anything useful.
Tetris Code Snippet in Question:
int ch = getch();
while(ch != 'x') {
// Handle arrow keys first
if (ch == '\033') {
getch(); // Get rid of slash
switch (getch()) {
case 'A': rotate(); redraw(); break; // Up
case 'B': move(DOWN); redraw(); break; // Down
case 'C': move(RIGHT); redraw(); break; // Right
case 'D': move(LEFT); redraw(); break; // Left
}
} else {
switch(ch) {
// Stuff not relevant to this question
}
}
ch = getch();
}
No correct solution
OTHER TIPS
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow