1.物理建监听
RawKeyboardListener( autofocus: true, onKey: (event) { if (event.runtimeType == RawKeyDownEvent && event.data is RawKeyEventDataAndroid) { RawKeyEventDataAndroid rData = event.data as RawKeyEventDataAndroid; if (rData.keyCode == CodeKey.enter) { // 处理逻辑 } //电脑键盘 // if (event.physicalKey == PhysicalKeyboardKey.enter) { // // } } }, focusNode: FocusNode(), child:Container() );
//注:CodeKey.enter 是我自定义的一个存放CodeKey的静态类。具体KEY值可以自行去网上所搜
2.返回键导航栏拦截监听
WillPopScope( child: Container(), onWillPop: () async { if (!isExit) { isExit = true; Utils.showBNoImgshortToast("再次点击退出应用程序", context); //2秒内没有点击 isExit 从新置为false Future.delayed(const Duration(milliseconds: 2000), () { isExit = false; }); return false; } else { // 退出app //await SystemChannels.platform.invokeMethod('SystemNavigator.pop'); //Navigator.pop(context); //关闭当前页面 return true; } }, );