dart_console icon indicating copy to clipboard operation
dart_console copied to clipboard

Support for more function keys

Open treeplate opened this issue 3 years ago • 1 comments

The ControlCharacter enum has F1-F4. I would like support for more F-n keys like F5

treeplate avatar Jan 06 '21 18:01 treeplate

Would something like the following fix it?

modified   lib/src/console.dart
@@ -545,7 +545,61 @@ class Console {
               }
               escapeSequence.add(String.fromCharCode(charCode));
               if (escapeSequence[2] != '~') {
-                key.controlChar = ControlCharacter.unknown;
+                if (escapeSequence[2].codeUnits[0] >= '0'.codeUnits[0] &&
+                    escapeSequence[2].codeUnits[0] <= '9'.codeUnits[0]) {
+                  charCode = stdin.readByteSync();
+                  if (charCode == -1) {
+                    rawMode = false;
+                    return key;
+                  }
+                  escapeSequence.add(String.fromCharCode(charCode));
+                  if (escapeSequence[3] != '~') {
+                    key.controlChar = ControlCharacter.unknown;
+                  } else {
+                    switch (escapeSequence[1]) {
+                      case '1':
+                        switch (escapeSequence[2]) {
+                          case '5':
+                            key.controlChar = ControlCharacter.F5;
+                            break;
+                          case '7':
+                            key.controlChar = ControlCharacter.F6;
+                            break;
+                          case '8':
+                            key.controlChar = ControlCharacter.F7;
+                            break;
+                          case '9':
+                            key.controlChar = ControlCharacter.F8;
+                            break;
+                          default:
+                            key.controlChar = ControlCharacter.unknown;
+                        }
+                        break;
+                      case '2':
+                        switch (escapeSequence[2]) {
+                          case '0':
+                            key.controlChar = ControlCharacter.F9;
+                            break;
+                          case '1':
+                            key.controlChar = ControlCharacter.F10;
+                            break;
+                          case '3':
+                            key.controlChar = ControlCharacter.F11;
+                            break;
+                          case '4':
+                            key.controlChar = ControlCharacter.F12;
+                            break;
+                          default:
+                            key.controlChar = ControlCharacter.unknown;
+                        }
+                        break;
+                      default:
+                        key.controlChar = ControlCharacter.unknown;
+                    }
+                  }
+                } else {
+                  key.controlChar = ControlCharacter.unknown;
+                }
               } else {
                 switch (escapeSequence[1]) {
                   case '1':
modified   lib/src/key.dart
@@ -53,6 +53,14 @@ enum ControlCharacter {
   F2,
   F3,
   F4,
+  F5,
+  F6,
+  F7,
+  F8,
+  F9,
+  F10,
+  F11,
+  F12,
 
   unknown
 }

Works here in Terminal.app on macOS. The mappings agree with those found at the following link:

You can also check these in terminal like Ctrl+v F5 or with showkey or cat.

I wonder if there's an issue I'm missing with reading ahead too many bytes?

bradyt avatar Nov 16 '21 01:11 bradyt