0

I working on a prototype Flutter app, I have a use case where I should be able to capture all the OS Hotkey events and prevent the default behaviour.

I should be able to prevent the app from closing on CMD+Q.

any help would be appreciated, thanks in advance.

Update:

I used RawKeyboardLintener to watch on the keyboard event it captures the events when the app is in focus, but on cmd+tab or cmd+q the OS takes over and the app loses its focus or it gets closed.

I didn't find any material to capture it or prevent it from happening.

Code block

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(
        title: new Text(widget.title),
      ),
      body: RawKeyboardListener(
        focusNode: FocusNode(),
        onKey: (RawKeyEvent event) {
          if (event.runtimeType.toString() == 'RawKeyDownEvent') print(event);
        },
        autofocus: true,
        child: new Container(
          child: Text("hello"),
        ),
      ),
    );
  }
}
ADITHYAN
  • 119
  • 9

1 Answers1

0

Flutter has no ability to handle events that never reach the Flutter view. If you want to handle events for which the OS uses a different delivery path you'd need to do it in native code (the same way it would be done in a non-Flutter macOS application).

smorgan
  • 20,228
  • 3
  • 47
  • 55