flutter_screen_lock icon indicating copy to clipboard operation
flutter_screen_lock copied to clipboard

physical keyboard input support

Open clragon opened this issue 3 years ago • 5 comments

It would be great if the package supported input via a physical keyboard.

I have already tested a rough implementation of this:

final FocusNode _focusNode = FocusNode();

bool _canInput = true;

void _blockInput() {
  _canInput = false;
  Timer(const Duration(milliseconds: 100), () => _canInput = true);
}

@override
void initState() {
  super.initState();
  _focusNode.requestFocus();
}

@override
void didChangeDependencies() {
  super.didChangeDependencies();
  FocusScope.of(context).autofocus(_focusNode);
}

@override
void dispose() {
  _focusNode.dispose();
  super.dispose();
}

@override
Widget build(BuildContext context) {
  return KeyboardListener(
    focusNode: _focusNode,
    onKeyEvent: (key) {
      if (!_canInput) {
        return;
      }
      if (widget.inputButtonConfig.inputStrings.contains(key.character)) {
        widget.inputState.addCharacter(key.character!);
        _blockInput();
      }
      if (key.logicalKey == LogicalKeyboardKey.backspace) {
        widget.inputState.removeCharacter();
        _blockInput();
      }
    },
    child: widget.child,
  );
}

It can be used as a widget wrapped around a keypad to enter a PIN code from the keyboard. There are some issues with delays. KeyEvent can be triggered very fast. Maybe only add delay on backspace? not sure.

I probably won't make a pull request. I don't want to change the structure of your project too much. But I think this feature is a great addition.

clragon avatar May 18 '22 11:05 clragon

We are planning to make Dart SDK 2.17, so I think we will merge this with a major version upgrade. Please give me a little time until then.

naoki0719 avatar May 19 '22 12:05 naoki0719

there would be alot of benefits in using actual textfields behind the scenes. these would not need delays and support multiple input devices.

An idea would be to use the following package: https://pub.dev/packages/pin_code_fields

This would come with quite a bit of restructuring as Secrets would have to be built very differently.

clragon avatar Oct 18 '22 11:10 clragon

If you think it would be interesting to look into the pin_code_fields package, please let me know, @naoki0719. I will experiment with it. In that case, please do not release version 8.0.0 after merging #89. I will add more pull requests. If not, then that is okay.

clragon avatar Oct 18 '22 15:10 clragon

Thank you for all your help. I want to release #89 with v8, so I am testing it now. I've been playing with python lately, so the release may be delayed!

naoki0719 avatar Oct 19 '22 12:10 naoki0719

after working on a completely unrelated package, I now have new knowledge.

@override
Widget build(BuildContext context) {
  return KeyboardListener(
    focusNode: _focusNode,
    onKeyEvent: (key) {
      if (key is! KeyDownEvent) return;
      if (widget.inputButtonConfig.inputStrings.contains(key.character)) {
        widget.inputState.addCharacter(key.character!);
      } else if (key.logicalKey == LogicalKeyboardKey.backspace) {
        widget.inputState.removeCharacter();
      }
    },
    child: widget.child,
  );
}

essentially we can ignore all key events which arent "down" meaning the key is being pressed. this way, we dont need to do any input blocking.

clragon avatar Dec 15 '23 19:12 clragon