Receive the next character of typed input
| 461 | |
| 462 | // Receive the next character of typed input |
| 463 | void NetHackQtExtCmdRequestor::keyPressEvent(QKeyEvent *event) |
| 464 | { |
| 465 | /* |
| 466 | * This will select a command outright--as soon as enough chars |
| 467 | * to not be ambiguous are entered--but also narrows down visible |
| 468 | * choices [via enableButtons()] in the grid of clickable command |
| 469 | * buttons as the text-so-far makes many become impossible to match. |
| 470 | * Use of backspace/delete or ESC/kill restores suppressed choices |
| 471 | * to the grid as they become matchable again. |
| 472 | */ |
| 473 | |
| 474 | unsigned saveexactmatchindx = exactmatchindx; |
| 475 | // if previous KeyPressEvent cleared default, restore it now |
| 476 | DefaultActionIsCancel(true); |
| 477 | QString promptstr = prompt->text(); |
| 478 | uchar uc = keyValue(event); |
| 479 | |
| 480 | if (!uc) { |
| 481 | // shift or control or meta, another character should be coming |
| 482 | QWidget::keyPressEvent(event); |
| 483 | } else if (uc == '\033' || uc == KILL_CHAR) { |
| 484 | // <escape> when partial response is present kills that text |
| 485 | // but keeps prompting; <escape> when response is empty cancels. |
| 486 | // Kill gets rid of pending text, if any, and always re-prompts. |
| 487 | if (uc == '\033' && promptstr == "#") |
| 488 | reject(); // cancel() if ESC used when string is empty |
| 489 | prompt->setText("#"); // reset to empty ('#' is always present) |
| 490 | enableButtons(); |
| 491 | } else if (uc == '\b' || uc == '\177') { |
| 492 | if (promptstr != "#") |
| 493 | prompt->setText(promptstr.left(promptstr.size() - 1)); |
| 494 | enableButtons(); |
| 495 | } else if ((uc < ' ' && !(uc == '\n' || uc == '\r')) |
| 496 | || uc > std::max('z', 'Z')) { |
| 497 | reject(); // done() |
| 498 | } else { |
| 499 | /* |
| 500 | * <return> or <space> is necessary if one command is a |
| 501 | * leading substring of another and superfluous otherwise. |
| 502 | * (When a command is not a prefix of another, it will have |
| 503 | * been selected before reaching its last letter.) |
| 504 | * |
| 505 | * If we got an exact match with the last key, we're expecting |
| 506 | * a <return> or <space> to explicitly choose it now but might |
| 507 | * get next letter of the longer command (or get a backspace). |
| 508 | * |
| 509 | * If we get an exact match this time, then stop showing |
| 510 | * [Cancel] as the default action for <return>. |
| 511 | * (That's a visual cue for the player, not a requirement to |
| 512 | * prevent <return> from triggering the default action.) |
| 513 | * If it's not the default action upon the next key press, |
| 514 | * we'll change that back (done above). |
| 515 | * |
| 516 | * TODO? |
| 517 | * Implement <tab> support: if promptstr matches multiple |
| 518 | * commands but they all have the next one or more letters in |
| 519 | * common, allow <tab> to add the common letters to promptstr. |
| 520 | */ |
nothing calls this directly
no test coverage detected