daily pastebin goal
34%
SHARE
TWEET

Untitled

a guest Jan 23rd, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const KEYCODE_A = 0;
  2. const KEYCODE_HOME = 115;
  3. const KEYCODE_DEL = 117;
  4. const KEYCODE_END = 119;
  5. const KEYCODE_LEFT = 123;
  6. const KEYCODE_RIGHT = 124;
  7.  
  8. class TextEditingRawKeyboardListener extends StatelessWidget {
  9.   final Widget child;
  10.   final FocusNode focusNode;
  11.   final TextEditingController controller;
  12.  
  13.   TextEditingRawKeyboardListener({Key key, this.child, this.focusNode, this.controller}): super(key: key);
  14.  
  15.   @override
  16.   Widget build(BuildContext context) => RawKeyboardListener(
  17.     child: child,
  18.     focusNode: focusNode,
  19.     onKey: onKeyEvent
  20.   );
  21.  
  22.   void onKeyEvent(RawKeyEvent e) {
  23.     if (e.runtimeType != RawKeyDownEvent || e.data.runtimeType != RawKeyEventDataAndroid)
  24.       return;
  25.     final keyEvent = e.data as RawKeyEventDataAndroid;
  26.     if (keyEvent.isModifierPressed(ModifierKey.metaModifier)) {
  27.       if (keyEvent.keyCode == KEYCODE_A)
  28.         controller.selection = TextSelection(baseOffset: 0, extentOffset: controller.text.length);
  29.     }
  30.     else if (keyEvent.isModifierPressed(ModifierKey.shiftModifier)) {
  31.       if (keyEvent.keyCode == KEYCODE_LEFT)
  32.         controller.selection = controller.selection.copyWith(extentOffset: max(controller.selection.extentOffset - 1, 0));
  33.       else if (keyEvent.keyCode == KEYCODE_RIGHT)
  34.         controller.selection = controller.selection.copyWith(extentOffset: min(controller.selection.extentOffset + 1, controller.text.length));
  35.       else if (keyEvent.keyCode == KEYCODE_HOME)
  36.         controller.selection = controller.selection.copyWith(extentOffset: 0);
  37.       else if (keyEvent.keyCode == KEYCODE_END)
  38.         controller.selection = controller.selection.copyWith(extentOffset: controller.text.length);
  39.     }
  40.     else if (keyEvent.keyCode == KEYCODE_HOME)
  41.       controller.selection = TextSelection.collapsed(offset: 0);
  42.     else if (keyEvent.keyCode == KEYCODE_END)
  43.       controller.selection = TextSelection.collapsed(offset: controller.text.length);
  44.     else if (keyEvent.keyCode == KEYCODE_DEL) {
  45.       final start = min(controller.selection.baseOffset, controller.selection.extentOffset);
  46.       var end = max(controller.selection.baseOffset, controller.selection.extentOffset);
  47.       if (start == end && end < controller.text.length) {
  48.         // If nothing is selected, behave as if the character after the cursor was selected (only for DEL).
  49.         end += 1;
  50.       }
  51.       controller.text = controller.text.substring(0, start)
  52.                       + controller.text.substring(end);
  53.       controller.selection = TextSelection.collapsed(offset: start);
  54.     }
  55.   }
  56. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top