import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Keyboard combination detection',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Keyboard combination detection'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool _isKeyPressed = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: RawKeyboardListener(
focusNode: FocusNode(),
onKey: (RawKeyEvent keyEvent) {
if (keyEvent.isControlPressed && keyEvent.physicalKey == PhysicalKeyboardKey.keyC) {
setState(() {
_isKeyPressed = true;
});
}
},
child: Center(
child: Text(
_isKeyPressed ? "Ctrl + C is pressed" : "Press Ctrl + C",
style: TextStyle(fontSize: 22),
),
),
),
);
}
}
In the onKey callback function, we checked if the control key is pressed and also the physical key code of the pressed key is equal to the 'C' key code. If the condition is true, we set the isKeyPressed flag to true using the setState function. The flag value is used to display a message on the screen.