Skip to content

Problem with UTF8 terminal #25

@giorgiofran

Description

@giorgiofran

I was having problems with extended chars in a linux terminal.
When I typed "è" I got "Ã".
Running the rawkeys example I got the following output:
c3 (Ã)
a8 (¨)

Giving a look at the Key.readKey function I saw this comment:

   ///  ... , certain keys may
  /// be represented by multiple control key sequences. An example showing
  /// basic key handling can be found in the `example/command_line.dart`
  /// file in the package source code.

But I could not find the example "command_line"

So I did the following:
I copied the readKey function into an extension function and I did the following changes:
Original:

 Key readKey() {
    Key key;
    int charCode;
    int codeUnit = 0;

    rawMode = true;
    while (codeUnit <= 0) {
      codeUnit = stdin.readByteSync();
    }

    if (codeUnit >= 0x01 && codeUnit <= 0x1a) {

....

Modified:

  Key readSystemKey() {
    Key key;
    int charCode;
    var codeUnit;
    var bytes = <int>[];

    rawMode = true;
    while (key == null) {
      codeUnit = 0;
      while (codeUnit <= 0) {
        codeUnit = stdin.readByteSync();
      }

      if (codeUnit >= 0x01 && codeUnit <= 0x1a) {
...

Original end of method:

    } else {
      // assume other characters are printable
      key = Key.printable(String.fromCharCode(codeUnit));
    }
    rawMode = false;
    return key;
  }

Modified end of method:

      } else {
        // assume other characters are printable
        try {
          bytes.add(codeUnit);
          key = Key.printable(systemEncoding.decode(bytes));
          bytes.clear();
        } on FormatException catch (_) {
          if (bytes.length > 4) {
            rethrow;
          }
        }
      }
    }
    rawMode = false;
    return key;
  }

It works for my needs, but probably this logic should be added in other points of the package and tested with different OSs.
Another option would be to let the user set a specific encoding, if needed.

Let me know what do you think about.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions