首页 > 解决方案 > Flutter,如何处理键盘快捷键

问题描述

想在网络应用程序中实现键盘快捷键,找到了这个例子,但它没有按预期工作。

希望每次按住 alt 然后按键 A 时都会发生动作,尽管只有当我同时按下键 alt 和键 A 时才会发生动作

感谢

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

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

class MyIntent extends Intent {}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  int integer = 0;
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: Shortcuts(
        shortcuts: {
          LogicalKeySet(LogicalKeyboardKey.alt, LogicalKeyboardKey.keyA):
              MyIntent(),
        },
        child: Actions(
          actions: {
            MyIntent: CallbackAction(onInvoke: (i) {
              print('rocks!!!' '$integer');
              integer++;
              return null;
            }),
          },
          child: Focus(
            autofocus: true,
            child: SizedBox(
              height: 200,
              width: 500,
              child: Column(
                children: <Widget>[
                  const Text('List of Homework'),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}

标签: flutterkeyboardshortcut

解决方案


推荐阅读