FIX keyboard

This commit is contained in:
2025-03-15 00:07:30 +01:00
parent 7601cf3a22
commit 64895c12b9

View File

@@ -307,17 +307,20 @@ class _MyHomePageState extends State<MyHomePage> {
return _formatDuration(duration); return _formatDuration(duration);
} }
// Update keyboard handler method to use modern KeyEvent API
void _handleKeyEvent(KeyEvent event) { void _handleKeyEvent(KeyEvent event) {
// Only process key down events to avoid duplicate triggers
if (event is KeyDownEvent) { if (event is KeyDownEvent) {
// Handle Space key
if (event.logicalKey == LogicalKeyboardKey.space) { if (event.logicalKey == LogicalKeyboardKey.space) {
// Space: Start if paused, Lap if running
if (_isRunning) { if (_isRunning) {
_recordLap(); _recordLap();
} else { } else {
_startTimer(); _startTimer();
} }
} else if (event.logicalKey == LogicalKeyboardKey.enter) { }
// Enter: Stop if paused, Pause if running // Handle Enter key
else if (event.logicalKey == LogicalKeyboardKey.enter) {
if (_isRunning) { if (_isRunning) {
_pauseTimer(); _pauseTimer();
} else { } else {
@@ -336,16 +339,25 @@ class _MyHomePageState extends State<MyHomePage> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return KeyboardListener( // Use Focus directly to capture key events
return Focus(
focusNode: _focusNode, focusNode: _focusNode,
onKeyEvent: _handleKeyEvent,
autofocus: true, autofocus: true,
onKeyEvent: (FocusNode node, KeyEvent event) {
_handleKeyEvent(event);
// Always return KeyEventResult.handled to indicate we've processed the event
return KeyEventResult.handled;
},
child: Scaffold( child: Scaffold(
appBar: AppBar( appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary, backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title), title: Text(widget.title),
), ),
body: Center( body: GestureDetector(
// This ensures tapping anywhere gives focus back to our listener
onTap: () => _focusNode.requestFocus(),
behavior: HitTestBehavior.translucent,
child: Center(
child: Column( child: Column(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: [ children: [
@@ -407,6 +419,7 @@ class _MyHomePageState extends State<MyHomePage> {
), ),
), ),
), ),
),
); );
} }
} }