diff --git a/packages/flutter/lib/src/services/raw_keyboard.dart b/packages/flutter/lib/src/services/raw_keyboard.dart index 6b53aaa814..ec5fad1a3d 100644 --- a/packages/flutter/lib/src/services/raw_keyboard.dart +++ b/packages/flutter/lib/src/services/raw_keyboard.dart @@ -260,6 +260,7 @@ abstract class RawKeyEvent extends Diagnosticable { eventSource: message['source'] ?? 0, vendorId: message['vendorId'] ?? 0, productId: message['productId'] ?? 0, + deviceId: message['deviceId'] ?? 0, ); break; case 'fuchsia': diff --git a/packages/flutter/lib/src/services/raw_keyboard_android.dart b/packages/flutter/lib/src/services/raw_keyboard_android.dart index fae5b536a6..99ee8055f9 100644 --- a/packages/flutter/lib/src/services/raw_keyboard_android.dart +++ b/packages/flutter/lib/src/services/raw_keyboard_android.dart @@ -36,6 +36,7 @@ class RawKeyEventDataAndroid extends RawKeyEventData { this.eventSource = 0, this.vendorId = 0, this.productId = 0, + this.deviceId = 0, }) : assert(flags != null), assert(codePoint != null), assert(keyCode != null), @@ -127,6 +128,12 @@ class RawKeyEventDataAndroid extends RawKeyEventData { /// for the numerical values of the `productId`. final int productId; + + /// The ID of the device that produced the event. + /// + /// See https://developer.android.com/reference/android/view/InputDevice.html#getId() + final int deviceId; + // The source code that indicates that an event came from a joystick. // from https://developer.android.com/reference/android/view/InputDevice.html#SOURCE_JOYSTICK static const int _sourceJoystick = 0x01000010; diff --git a/packages/flutter/test/services/raw_keyboard_test.dart b/packages/flutter/test/services/raw_keyboard_test.dart index 7a78b11098..1f080e3766 100644 --- a/packages/flutter/test/services/raw_keyboard_test.dart +++ b/packages/flutter/test/services/raw_keyboard_test.dart @@ -40,6 +40,7 @@ void main() { 'scanCode': 0x20, 'metaState': modifier, 'source': 0x101, // Keyboard source. + 'deviceId': 1, }); final RawKeyEventDataAndroid data = event.data; for (ModifierKey key in ModifierKey.values) { @@ -75,6 +76,7 @@ void main() { 'scanCode': 0x20, 'metaState': modifier | RawKeyEventDataAndroid.modifierFunction, 'source': 0x101, // Keyboard source. + 'deviceId': 1, }); final RawKeyEventDataAndroid data = event.data; for (ModifierKey key in ModifierKey.values) { @@ -112,6 +114,7 @@ void main() { 'scanCode': 30, 'metaState': 0x0, 'source': 0x101, // Keyboard source. + 'deviceId': 1, }); final RawKeyEventDataAndroid data = keyAEvent.data; expect(data.physicalKey, equals(PhysicalKeyboardKey.keyA)); @@ -128,6 +131,7 @@ void main() { 'scanCode': 1, 'metaState': 0x0, 'source': 0x101, // Keyboard source. + 'deviceId': 1, }); final RawKeyEventDataAndroid data = escapeKeyEvent.data; expect(data.physicalKey, equals(PhysicalKeyboardKey.escape)); @@ -145,6 +149,7 @@ void main() { 'scanCode': 42, 'metaState': RawKeyEventDataAndroid.modifierLeftShift, 'source': 0x101, // Keyboard source. + 'deviceId': 1, }); final RawKeyEventDataAndroid data = shiftLeftKeyEvent.data; expect(data.physicalKey, equals(PhysicalKeyboardKey.shiftLeft)); @@ -162,6 +167,7 @@ void main() { 'scanCode': 0, 'metaState': 0, 'source': 0x1000010, // Joystick source. + 'deviceId': 1, }); final RawKeyEventDataAndroid data = joystickDpadDown.data; expect(data.physicalKey, equals(PhysicalKeyboardKey.arrowDown)); @@ -196,12 +202,29 @@ void main() { 'scanCode': 317, // Left side thumb joystick center click button. 'metaState': 0, 'source': 0x501, // Gamepad and keyboard source. + 'deviceId': 1, }); final RawKeyEventDataAndroid data = joystickDpadCenter.data; expect(data.physicalKey, equals(PhysicalKeyboardKey.gameButtonThumbLeft)); expect(data.logicalKey, equals(LogicalKeyboardKey.select)); expect(data.keyLabel, isNull); }); + test('Device id is read from message', () { + final RawKeyEvent joystickDpadCenter = RawKeyEvent.fromMessage(const { + 'type': 'keydown', + 'keymap': 'android', + 'keyCode': 23, // DPAD_CENTER code. + 'plainCodePoint': 0, + 'codePoint': 0, + 'character': null, + 'scanCode': 317, // Left side thumb joystick center click button. + 'metaState': 0, + 'source': 0x501, // Gamepad and keyboard source. + 'deviceId': 10, + }); + final RawKeyEventDataAndroid data = joystickDpadCenter.data; + expect(data.deviceId, equals(10)); + }); }); group('RawKeyEventDataFuchsia', () { const Map modifierTests = {