Created
August 11, 2014 07:31
-
-
Save shibukawa/e1aaf6e6ad27fd53dbc3 to your computer and use it in GitHub Desktop.
Qtでコナミコマンドを実装する方法。ただし、ウインドウ内にQTableViewとかQTreeViewがあると上下キーを持っていかれる。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void MainWindow::keyPressEvent(QKeyEvent *event) | |
{ | |
Qt::Key command[] = { | |
Qt::Key_Up, | |
Qt::Key_Up, | |
Qt::Key_Down, | |
Qt::Key_Down, | |
Qt::Key_Left, | |
Qt::Key_Right, | |
Qt::Key_Left, | |
Qt::Key_Right, | |
Qt::Key_B, | |
Qt::Key_A | |
}; | |
auto current = QTime::currentTime(); | |
// 前のキーから4秒以上たってたらリセット | |
if (this->lastKeyPress.secsTo(current) > 3) { | |
this->commandProgress = 0; | |
} | |
if (event->key() == command[this->commandProgress]) { | |
this->commandProgress++; | |
this->lastKeyPress = current; | |
if (this->commandProgress == 10) { | |
this->commandProgress = 0; | |
// コマンド成立時にやりたいことはここにかく | |
ui->button.setEnabled(true); | |
} | |
} else { | |
this->commandProgress = 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment