22// Use of this source code is governed by a BSD-style license that can be
33// found in the LICENSE file.
44
5-
65part of ui;
76
87/// The type of a key event.
@@ -15,7 +14,49 @@ enum KeyEventType {
1514 up,
1615
1716 /// The key is held, causing a repeated key input.
18- repeat,
17+ repeat;
18+
19+ String get label {
20+ return switch (this ) {
21+ down => 'Down' ,
22+ up => 'Up' ,
23+ repeat => 'Repeat' ,
24+ };
25+ }
26+ }
27+
28+ /// The source device for the key event.
29+ ///
30+ /// Not all platforms supply an accurate type.
31+ ///
32+ /// Defaults to [keyboard] .
33+ // Must match the KeyEventDeviceType enum in ui/window/key_data.h.
34+ enum KeyEventDeviceType {
35+ /// The source is a keyboard.
36+ keyboard,
37+
38+ /// The source is a directional pad on something like a television remote
39+ /// control or similar.
40+ directionalPad,
41+
42+ /// The source is a gamepad button
43+ gamepad,
44+
45+ /// The source is a joystick button
46+ joystick,
47+
48+ /// The source is a device connected to an HDMI bus.
49+ hdmi;
50+
51+ String get label {
52+ return switch (this ) {
53+ keyboard => 'Keyboard' ,
54+ directionalPad => 'Directional Pad' ,
55+ gamepad => 'Gamepad' ,
56+ joystick => 'Joystick' ,
57+ hdmi => 'HDMI' ,
58+ };
59+ }
1960}
2061
2162/// Information about a key event.
@@ -24,6 +65,7 @@ class KeyData {
2465 const KeyData ({
2566 required this .timeStamp,
2667 required this .type,
68+ required this .deviceType,
2769 required this .physical,
2870 required this .logical,
2971 required this .character,
@@ -32,13 +74,18 @@ class KeyData {
3274
3375 /// Time of event dispatch, relative to an arbitrary timeline.
3476 ///
35- /// For [KeyEventType.synchronize] and [KeyEventType.cancel] events, the [timeStamp]
36- /// might not be the actual time that the key press or release happens.
77+ /// For synthesized events, the [timeStamp] might not be the actual time that
78+ /// the key press or release happens.
3779 final Duration timeStamp;
3880
3981 /// The type of the event.
4082 final KeyEventType type;
4183
84+ /// The source device of this key event.
85+ ///
86+ /// Defaults to [KeyEventDeviceType.keyboard] .
87+ final KeyEventDeviceType deviceType;
88+
4289 /// The key code for the physical key that has changed.
4390 final int physical;
4491
@@ -59,17 +106,18 @@ class KeyData {
59106 ///
60107 /// For example, some key downs or ups might be lost when the window loses
61108 /// focus. Some platforms provides ways to query whether a key is being held.
62- /// If Flutter detects an inconsistency between the state Flutter records and
63- /// the state returned by the system, Flutter will synthesize a corresponding
64- /// event to synchronize the state without breaking the event model.
109+ /// If the embedder detects an inconsistency between its internal record and
110+ /// the state returned by the system, the embedder will synthesize a
111+ /// corresponding event to synchronize the state without breaking the event
112+ /// model.
65113 ///
66114 /// As another example, macOS treats CapsLock in a special way by sending
67- /// down and up events at the down of alterate presses to indicate the
115+ /// down and up events at the down of alternate presses to indicate the
68116 /// direction in which the lock is toggled instead of that the physical key is
69- /// going. Flutter normalizes the behavior by converting a native down event
70- /// into a down event followed immediately by a synthesized up event, and
71- /// the native up event also into a down event followed immediately by a
72- /// synthesized up event.
117+ /// going. A macOS embedder should normalize the behavior by converting a
118+ /// native down event into a down event followed immediately by a synthesized
119+ /// up event, and the native up event also into a down event followed
120+ /// immediately by a synthesized up event.
73121 ///
74122 /// Synthesized events do not have a trustworthy [timeStamp] , and should not be
75123 /// processed as if the key actually went down or up at the time of the
@@ -95,8 +143,22 @@ class KeyData {
95143 return ' (Unprintable)' ;
96144 case 0x002 :
97145 return ' (Flutter)' ;
146+ case 0x011 :
147+ return ' (Android)' ;
148+ case 0x012 :
149+ return ' (Fuchsia)' ;
150+ case 0x013 :
151+ return ' (iOS)' ;
152+ case 0x014 :
153+ return ' (macOS)' ;
154+ case 0x015 :
155+ return ' (GTK)' ;
156+ case 0x016 :
157+ return ' (Windows)' ;
98158 case 0x017 :
99159 return ' (Web)' ;
160+ case 0x018 :
161+ return ' (GLFW)' ;
100162 }
101163 return '' ;
102164 })();
@@ -105,7 +167,7 @@ class KeyData {
105167
106168 String ? _escapeCharacter () {
107169 if (character == null ) {
108- return character ?? '<none>' ;
170+ return '<none>' ;
109171 }
110172 switch (character! ) {
111173 case '\n ' :
@@ -133,29 +195,25 @@ class KeyData {
133195 }
134196
135197 @override
136- String toString () => 'KeyData(type: ${_typeToString (type )}, physical: 0x${physical .toRadixString (16 )}, '
137- 'logical: ${_logicalToString ()}, character: ${_escapeCharacter ()}${_quotedCharCode ()}${synthesized ? ', synthesized' : '' })' ;
198+ String toString () {
199+ return 'KeyData(key ${type .label }, '
200+ 'deviceType: ${deviceType .label }), '
201+ 'physical: 0x${physical .toRadixString (16 )}, '
202+ 'logical: ${_logicalToString ()}, '
203+ 'character: ${_escapeCharacter ()}${_quotedCharCode ()}'
204+ '${synthesized ? ', synthesized' : '' }' ;
205+ }
138206
139207 /// Returns a complete textual description of the information in this object.
140208 String toStringFull () {
141209 return '$runtimeType ('
142- 'type: ${_typeToString (type )}, '
143- 'timeStamp: $timeStamp , '
144- 'physical: 0x${physical .toRadixString (16 )}, '
145- 'logical: 0x${logical .toRadixString (16 )}, '
146- 'character: $character , '
147- 'synthesized: $synthesized '
210+ 'type: ${type .label }, '
211+ 'deviceType: ${deviceType .label }, '
212+ 'timeStamp: $timeStamp , '
213+ 'physical: 0x${physical .toRadixString (16 )}, '
214+ 'logical: 0x${logical .toRadixString (16 )}, '
215+ 'character: ${_escapeCharacter ()}, '
216+ 'synthesized: $synthesized '
148217 ')' ;
149218 }
150-
151- static String _typeToString (KeyEventType type) {
152- switch (type) {
153- case KeyEventType .up:
154- return 'up' ;
155- case KeyEventType .down:
156- return 'down' ;
157- case KeyEventType .repeat:
158- return 'repeat' ;
159- }
160- }
161219}
0 commit comments