Skip to content

Instantly share code, notes, and snippets.

@shibukawa
Created August 11, 2014 07:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shibukawa/e1aaf6e6ad27fd53dbc3 to your computer and use it in GitHub Desktop.
Save shibukawa/e1aaf6e6ad27fd53dbc3 to your computer and use it in GitHub Desktop.
Qtでコナミコマンドを実装する方法。ただし、ウインドウ内にQTableViewとかQTreeViewがあると上下キーを持っていかれる。
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