-
-
Notifications
You must be signed in to change notification settings - Fork 899
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
onKeyEvent
doesn't work if you press two buttons.
#2245
Comments
Have you tried on the stable channel of Flutter? |
Yes, same issue. |
Very strange, this definitely used to work! It seems to not happen all the time either. Here is a full repro if anyone wants to try: |
For me, Flutter-only seems to be working fine. This is an old sample, it is working: import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(),
home: const HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Focus(
autofocus: true,
onKey: (FocusNode node, RawKeyEvent event) {
KeyEventResult result = KeyEventResult.ignored;
if (event is RawKeyDownEvent) {
if (event.logicalKey == LogicalKeyboardKey.keyZ ||
event.logicalKey == const LogicalKeyboardKey(0x0000005a)) {
// When press Z, ctrl+Z, meta+Z, shift+Z:
print('Z pressed');
result = KeyEventResult.handled;
} else {
// When press meta+shift+Z.
print('something pressed (???)');
}
}
setState(() {});
return result;
},
child: Text('key is: ${RawKeyboard.instance.keysPressed}'),
),
);
}
} |
Hold on. Still debugging. |
This is not a Flutter issue, this is how the OS works. These are the possible workarounds: Also this: |
Thanks for investigating it! I guess we should close this then. |
Yes and no, ideally you could refactor |
I made a sketch for a mixin that could somewhat be integrated into Flame, since it guarantees the button works. Just giving the idea. But ideally you could override mixin WithCorrectTimer on Component, HasKeyboardHandlerComponents {
Timer? _timer;
final Set<String?> currentKeysPressed = {};
final int interval = 16;
@override
Future<void>? onLoad() {
_timer?.cancel();
_timer = Timer.periodic(
Duration(milliseconds: interval), (Timer t) => updateMovement());
return super.onLoad();
}
void updateMovement();
@override
void onRemove() {
_timer?.cancel();
super.onRemove();
}
@override
KeyEventResult onKeyEvent(
RawKeyEvent event, Set<LogicalKeyboardKey> keysPressed) {
currentKeysPressed
..clear()
..addAll(RawKeyboard.instance.keysPressed.map((e) => e.debugName));
return super.onKeyEvent(event, keysPressed);
}
} |
You shouldn't need the timer if you use |
My worry here is that there could be a situation where a key gets released without the corresponding "KeyUp" event -- say it gets released when the game widget has no focus. In that case we would keep thinking that the key is pressed, and it will effectively become "stuck". |
But perhaps we could create a new version of the |
There is a Flutter "bug" that already happens when you switch window. It will be fixed in the coming months when the key behavior is synced across window. Right NOW, the only way to not trigger the bug behavior is using ShortcutActivator or SingleActivator, which you aren't using on keyboard event. So there wouldn't be any change. |
I can't believe you worked so hard on it. I thought I was happy with my placeholder (which came from you, lol). And you ended up doing something even better. Thanks!! |
How to reproduce:
Screen.Recording.2022-12-30.at.19.04.01.mov
Really simple code:
Using Flame 1.5.0 and Flutter (Channel main, 3.7.0-13.0.pre.127, on macOS 13.1 22C65 darwin-x64,
locale en-BR)
The text was updated successfully, but these errors were encountered: