Skip to content

Instantly share code, notes, and snippets.

@SureshKumar311
Last active February 22, 2023 11:41
Show Gist options
  • Save SureshKumar311/ef639f92a97b315765bc300fc3e7e7c7 to your computer and use it in GitHub Desktop.
Save SureshKumar311/ef639f92a97b315765bc300fc3e7e7c7 to your computer and use it in GitHub Desktop.
DisableShortcut on Flutter web
//Use this for block default contextmenu on flutter web
if (kIsWeb && kReleaseMode) {
//contextmenu will work on debug mode only
html.document.body!.addEventListener('contextmenu', (event) => event.preventDefault());
//to prevent onPaste over hold web app
//html.document.onPaste.listen((event)=>event.preventDefault());
}
/// Chid widget will not be able to use shortcuts on flutter web
class DisableShortcut extends StatelessWidget {
const DisableShortcut({super.key, required this.child, this.isEnable = true});
final Widget child;
final bool isEnable;
@override
Widget build(BuildContext context) {
final copyKeys = LogicalKeySet(
LogicalKeyboardKey.meta,
LogicalKeyboardKey.keyC,
);
final cutKeys = LogicalKeySet(
LogicalKeyboardKey.meta,
LogicalKeyboardKey.keyX,
);
final pasteKey = LogicalKeySet(
LogicalKeyboardKey.meta,
LogicalKeyboardKey.keyV,
);
final copyKeysWin = LogicalKeySet(
LogicalKeyboardKey.control,
LogicalKeyboardKey.keyC,
);
final cutKeysWin = LogicalKeySet(
LogicalKeyboardKey.control,
LogicalKeyboardKey.keyX,
);
final pasteKeyWin = LogicalKeySet(
LogicalKeyboardKey.control,
LogicalKeyboardKey.keyV,
);
final test = LogicalKeySet(
LogicalKeyboardKey.meta,
LogicalKeyboardKey.keyA,
LogicalKeyboardKey.keyV,
);
return FocusableActionDetector(
// mouseCursor: SystemMouseCursors.click,
shortcuts: {
copyKeys: CopySelectionTextIntent.copy,
//
cutKeys: const CopySelectionTextIntent.cut(SelectionChangedCause.drag),
cutKeys: const CopySelectionTextIntent.cut(SelectionChangedCause.toolbar),
cutKeys: const CopySelectionTextIntent.cut(SelectionChangedCause.keyboard),
cutKeys: const CopySelectionTextIntent.cut(SelectionChangedCause.scribble),
cutKeys: const CopySelectionTextIntent.cut(SelectionChangedCause.longPress),
cutKeys: const CopySelectionTextIntent.cut(SelectionChangedCause.forcePress),
cutKeys: const CopySelectionTextIntent.cut(SelectionChangedCause.tap),
cutKeys: const CopySelectionTextIntent.cut(SelectionChangedCause.doubleTap),
//
pasteKey: const PasteTextIntent(SelectionChangedCause.keyboard),
pasteKey: const PasteTextIntent(SelectionChangedCause.drag),
pasteKey: const PasteTextIntent(SelectionChangedCause.toolbar),
pasteKey: const PasteTextIntent(SelectionChangedCause.scribble),
pasteKey: const PasteTextIntent(SelectionChangedCause.longPress),
pasteKey: const PasteTextIntent(SelectionChangedCause.tap),
pasteKey: const PasteTextIntent(SelectionChangedCause.doubleTap),
pasteKey: const PasteTextIntent(SelectionChangedCause.forcePress),
//
pasteKeyWin: const PasteTextIntent(SelectionChangedCause.keyboard),
pasteKeyWin: const PasteTextIntent(SelectionChangedCause.drag),
pasteKeyWin: const PasteTextIntent(SelectionChangedCause.toolbar),
pasteKeyWin: const PasteTextIntent(SelectionChangedCause.scribble),
pasteKeyWin: const PasteTextIntent(SelectionChangedCause.longPress),
pasteKeyWin: const PasteTextIntent(SelectionChangedCause.tap),
pasteKeyWin: const PasteTextIntent(SelectionChangedCause.doubleTap),
pasteKeyWin: const PasteTextIntent(SelectionChangedCause.forcePress),
//
copyKeysWin: CopySelectionTextIntent.copy,
//
cutKeysWin: const CopySelectionTextIntent.cut(SelectionChangedCause.drag),
cutKeysWin: const CopySelectionTextIntent.cut(SelectionChangedCause.toolbar),
cutKeysWin: const CopySelectionTextIntent.cut(SelectionChangedCause.keyboard),
cutKeysWin: const CopySelectionTextIntent.cut(SelectionChangedCause.scribble),
cutKeysWin: const CopySelectionTextIntent.cut(SelectionChangedCause.longPress),
cutKeysWin: const CopySelectionTextIntent.cut(SelectionChangedCause.forcePress),
cutKeysWin: const CopySelectionTextIntent.cut(SelectionChangedCause.forcePress),
cutKeysWin: const CopySelectionTextIntent.cut(SelectionChangedCause.tap),
cutKeysWin: const CopySelectionTextIntent.cut(SelectionChangedCause.doubleTap),
test: const PasteTextIntent(SelectionChangedCause.keyboard),
},
actions: {
CopySelectionTextIntent: CallbackAction<CopySelectionTextIntent>(
onInvoke: (intent) {
FocusScope.of(context).unfocus();
showSnackBar(
context,
"Copy and Cut has blocked",
);
return null;
},
),
PasteTextIntent: CallbackAction<PasteTextIntent>(
onInvoke: (intent) {
FocusScope.of(context).unfocus();
showSnackBar(
context,
"Paste has been blocked",
);
return null;
},
)
},
autofocus: true,
enabled: isEnable,
child: child,
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment