Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

physical keyboard input support #60

Open
clragon opened this issue May 18, 2022 · 5 comments
Open

physical keyboard input support #60

clragon opened this issue May 18, 2022 · 5 comments

Comments

@clragon
Copy link
Contributor

clragon commented May 18, 2022

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.

@naoki0719
Copy link
Owner

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.

@clragon
Copy link
Contributor Author

clragon commented Oct 18, 2022

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
Copy link
Contributor Author

clragon commented Oct 18, 2022

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.

@naoki0719
Copy link
Owner

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!

@clragon
Copy link
Contributor Author

clragon commented Dec 15, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants