In my case i need to scan barcode and fetch product details. Normally barcode scanner devices emit enter key(keycode=13) event at end of scanning, But in flutter enter key is not same as Done so how can code to detect enter key pressed in my TextFormField widget?


Solution 1: Benjith Kizhisseri

TextFormField(
maxLines: null,
autovalidate: true,
validator: (value){
if(value.contains('\n')){
doFun(value);
}
}

)

When user press enter key new line create in text box. We check with that.

maxLine:null - to hide multiline

autovalidate:true -to automatically run validator fun

'\n' - new line ('\s'-whitespace,'\t'-tab.. etc)


Solution 2: Al Walid Ashik

if you are using TextField then you have to add onSubmitted in your text field to detect when user press Enter key. For my case, I changed Done in keyboard to TextInputAction.Search. It also works for TextInputAction.Done too. here is a sample code

   TextField(
    onSubmitted: (value){
      //value is entered text after ENTER press
      //you can also call any function here or make setState() to assign value to other variable
    },
    textInputAction: TextInputAction.search,
  )


Solution 3: Sergey Yamshchikov

The solution above works, but I believe RawKeyboardListener is a more reliable and flexible solution. You just need to cover the text field with it and start to listen to keyboard events:

var focusNode = FocusNode();
RawKeyboardListener(
        focusNode: focusNode,
        onKey: (event) {
          if (event.isKeyPressed(LogicalKeyboardKey.enter)) {
            // Do something
          }
        },
        child: TextField(controller: TextEditingController())
    )

As a second option you can use onKey method of the FocusNoded and pass the node to your text field:

var focusNode = FocusNode(onKey: (node, event) {
    if (event.isKeyPressed(LogicalKeyboardKey.enter)) {
        // Do something
        // Next 2 line needed If you don't want to update the text field with new line.
        // node.unfocus(); 
        // return true;
    }
    return false;

});
TextField(focusNode: focusNode, controller: TextEditingController())


Solution 4: loonix

In case someone is looking for the same solution (as Al Walid Ashik) but for TextFormField, just use the following:

TextFormField(
  /// ...
  onFieldSubmitted: (value) {
     /// do some stuff here
  },
),


Solution 5: user2134488

In addition to the Sergey Yamshchikov's answer:

In case if it is a multiline TextField (maxLines: null) and you want to catch up the entered key and prevent passing it into the text field, you can use this approach:

RawKeyboardListener(
    focusNode: FocusNode(onKey: (node, event) {
              if (event.isKeyPressed(LogicalKeyboardKey.enter)) {
                return KeyEventResult.handled; // prevent passing the event into the TextField
              }
              return KeyEventResult.ignored; // pass the event to the TextField
            }),
    onKey: (event) {
      if (event.isKeyPressed(LogicalKeyboardKey.enter)) {
        // Do something
      }
    },
    child: TextField(controller: TextEditingController())
)

But, if you need to detect the keys combination, like ctrl+enter, then you can use CallbackShortcuts:

CallbackShortcuts(
  bindings: {
    const SingleActivator(LogicalKeyboardKey.enter, control: true): _doSomething(),
  },
  child: Focus(
    autofocus: true,
    child:  TextField(controller: TextEditingController()),
  ),
);