@@ -24,6 +24,7 @@ any extensions or special installation.
24
24
* [ Cursor] ( #cursor )
25
25
* [ History] ( #history )
26
26
* [ Autocomplete] ( #autocomplete )
27
+ * [ Keys] ( #keys )
27
28
* [ Pitfalls] ( #pitfalls )
28
29
* [ Install] ( #install )
29
30
* [ Tests] ( #tests )
@@ -503,6 +504,57 @@ disable the autocomplete function:
503
504
$readline->setAutocomplete(null);
504
505
```
505
506
507
+ #### Keys
508
+
509
+ The ` Readline ` class is responsible for reading user input from ` STDIN ` and
510
+ registering appropriate key events.
511
+ By default, ` Readline ` uses a hard-coded key mapping that resembles the one
512
+ usually found in common terminals.
513
+ This means that normal Unicode character keys ("a" and "b", but also "?", "ä",
514
+ "µ" etc.) will be processed as user input, while special control keys can be
515
+ used for [ cursor movement] ( #cursor ) , [ history] ( #history ) and
516
+ [ autocomplete] ( #autocomplete ) functions.
517
+ Unknown special keys will be ignored and will not processed as part of the user
518
+ input by default.
519
+
520
+ Additionally, you can bind custom functions to any key code you want.
521
+ If a custom function is bound to a certain key code, the default behavior will
522
+ no longer trigger.
523
+ This allows you to register entirely new functions to keys or to overwrite any
524
+ of the existing behavior.
525
+
526
+ For example, you can use the following code to print some help text when the
527
+ user hits a certain key:
528
+
529
+ ``` php
530
+ $readline->on('?', function () use ($stdio) {
531
+ $stdio->write('Here\'s some help: …' . PHP_EOL);
532
+ });
533
+ ```
534
+
535
+ Similarly, this can be used to manipulate the user input and replace some of the
536
+ input when the user hits a certain key:
537
+
538
+ ``` php
539
+ $readline->on('ä', function () use ($readline) {
540
+ $readline->addInput('a');
541
+ });
542
+ ```
543
+
544
+ The ` Readline ` uses raw binary key codes as emitted by the terminal.
545
+ This means that you can use the normal UTF-8 character representation for normal
546
+ Unicode characters.
547
+ Special keys use binary control code sequences (refer to ANSI / VT100 control
548
+ codes for more details).
549
+ For example, the following code can be used to register a custom function to the
550
+ UP arrow cursor key:
551
+
552
+ ``` php
553
+ $readline->on("\033[A", function () use ($readline) {
554
+ $readline->setInput(strtoupper($readline->getInput()));
555
+ });
556
+ ```
557
+
506
558
## Pitfalls
507
559
508
560
The [ ` Readline ` ] ( #readline ) has to redraw the current user
0 commit comments