|
|
|
|
@@ -21,6 +21,87 @@ import 'package:flutter/foundation.dart';
|
|
|
|
|
/// in a particular location on the keyboard, without regard for the modifier
|
|
|
|
|
/// state, mode, or keyboard layout.
|
|
|
|
|
///
|
|
|
|
|
/// As an example, if you wanted to implement an app where the "Q" key "quit"
|
|
|
|
|
/// something, you'd want to look at the logical key to detect this, since you
|
|
|
|
|
/// would like to have it match the key with "Q" on it, instead of always
|
|
|
|
|
/// looking for "the key next next to the TAB key", since on a French keyboard,
|
|
|
|
|
/// the key next to the TAB key has an "A" on it.
|
|
|
|
|
///
|
|
|
|
|
/// Conversely, if you wanted a game where the key next to the CAPS LOCK (the
|
|
|
|
|
/// "A" key on a QWERTY keyboard) moved the player to the left, you'd want to
|
|
|
|
|
/// look at the physical key to make sure that regardless of the character the
|
|
|
|
|
/// key produces, you got the key that is in that location on the keyboard.
|
|
|
|
|
///
|
|
|
|
|
/// {@tool snippet --template=stateful_widget}
|
|
|
|
|
/// This example shows how to detect if the user has selected the logical "Q"
|
|
|
|
|
/// key.
|
|
|
|
|
///
|
|
|
|
|
/// ```dart imports
|
|
|
|
|
/// import 'package:flutter/foundation.dart';
|
|
|
|
|
/// import 'package:flutter/services.dart';
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// ```dart
|
|
|
|
|
/// // The node used to request the keyboard focus.
|
|
|
|
|
/// final FocusNode _focusNode = FocusNode();
|
|
|
|
|
/// // The message to display.
|
|
|
|
|
/// String _message;
|
|
|
|
|
///
|
|
|
|
|
/// // Focus nodes need to be disposed.
|
|
|
|
|
/// @override
|
|
|
|
|
/// void dispose() {
|
|
|
|
|
/// _focusNode.dispose();
|
|
|
|
|
/// super.dispose();
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// // Handles the key events from the RawKeyboardListener and update the
|
|
|
|
|
/// // _message.
|
|
|
|
|
/// void _handleKeyEvent(RawKeyEvent event) {
|
|
|
|
|
/// setState(() {
|
|
|
|
|
/// if (event.logicalKey == LogicalKeyboardKey.keyQ) {
|
|
|
|
|
/// _message = 'Pressed the "Q" key!';
|
|
|
|
|
/// } else {
|
|
|
|
|
/// if (kReleaseMode) {
|
|
|
|
|
/// _message = 'Not a Q: Key label is "${event.logicalKey.keyLabel ?? '<none>'}"';
|
|
|
|
|
/// } else {
|
|
|
|
|
/// // This will only print useful information in debug mode.
|
|
|
|
|
/// _message = 'Not a Q: Pressed ${event.logicalKey.debugName}';
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
/// });
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// @override
|
|
|
|
|
/// Widget build(BuildContext context) {
|
|
|
|
|
/// final TextTheme textTheme = Theme.of(context).textTheme;
|
|
|
|
|
/// return Container(
|
|
|
|
|
/// color: Colors.white,
|
|
|
|
|
/// alignment: Alignment.center,
|
|
|
|
|
/// child: DefaultTextStyle(
|
|
|
|
|
/// style: textTheme.display1,
|
|
|
|
|
/// child: RawKeyboardListener(
|
|
|
|
|
/// focusNode: _focusNode,
|
|
|
|
|
/// onKey: _handleKeyEvent,
|
|
|
|
|
/// child: AnimatedBuilder(
|
|
|
|
|
/// animation: _focusNode,
|
|
|
|
|
/// builder: (BuildContext context, Widget child) {
|
|
|
|
|
/// if (!_focusNode.hasFocus) {
|
|
|
|
|
/// return GestureDetector(
|
|
|
|
|
/// onTap: () {
|
|
|
|
|
/// FocusScope.of(context).requestFocus(_focusNode);
|
|
|
|
|
/// },
|
|
|
|
|
/// child: Text('Tap to focus'),
|
|
|
|
|
/// );
|
|
|
|
|
/// }
|
|
|
|
|
/// return Text(_message ?? 'Press a key');
|
|
|
|
|
/// },
|
|
|
|
|
/// ),
|
|
|
|
|
/// ),
|
|
|
|
|
/// ),
|
|
|
|
|
/// );
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
|
|
|
|
/// {@end-tool}
|
|
|
|
|
/// See also:
|
|
|
|
|
///
|
|
|
|
|
/// * [RawKeyEvent], the keyboard event object received by widgets that listen
|
|
|
|
|
@@ -28,24 +109,26 @@ import 'package:flutter/foundation.dart';
|
|
|
|
|
/// * [RawKeyboardListener], a widget used to listen to and supply handlers for
|
|
|
|
|
/// keyboard events.
|
|
|
|
|
class LogicalKeyboardKey extends Diagnosticable {
|
|
|
|
|
/// Defines a KeyboardKey value and optional debug name.
|
|
|
|
|
/// Creates a LogicalKeyboardKey object with an optional key label and debug
|
|
|
|
|
/// name.
|
|
|
|
|
///
|
|
|
|
|
/// To save executable size, it is recommended that the [debugName] be null in
|
|
|
|
|
/// release mode. You can do this using the [kReleaseMode] constant:
|
|
|
|
|
/// [keyId] must not be null.
|
|
|
|
|
///
|
|
|
|
|
/// {@tool sample}
|
|
|
|
|
/// const LogicalKeyboardKey mySpecialKey = LogicalKeyboardKey(
|
|
|
|
|
/// 0x0010000000a,
|
|
|
|
|
/// debugName: kReleaseMode ? null : 'Special Key',
|
|
|
|
|
/// );
|
|
|
|
|
/// To save executable size, it is recommended that the [debugName] be null in
|
|
|
|
|
/// release mode. You can do this by using the [kReleaseMode] constant.
|
|
|
|
|
///
|
|
|
|
|
/// ```dart
|
|
|
|
|
/// const LogicalKeyboardKey(0x0010000000a, debugName: kReleaseMode ? null : 'Special Key')
|
|
|
|
|
/// ```
|
|
|
|
|
/// {@end-tool}
|
|
|
|
|
const LogicalKeyboardKey(this.keyId, {this.debugName, this.keyLabel});
|
|
|
|
|
const LogicalKeyboardKey(this.keyId, {this.debugName, this.keyLabel})
|
|
|
|
|
: assert(keyId != null);
|
|
|
|
|
|
|
|
|
|
/// A unique code representing this key.
|
|
|
|
|
///
|
|
|
|
|
/// This is an opaque identifier, and should not be unpacked to derive
|
|
|
|
|
/// information from it, as the representation of the code could change at any
|
|
|
|
|
/// time.
|
|
|
|
|
/// This is an opaque code. It should not be unpacked to derive information
|
|
|
|
|
/// from it, as the representation of the code could change at any time.
|
|
|
|
|
final int keyId;
|
|
|
|
|
|
|
|
|
|
/// The debug string to print for this keyboard key, which will be null in
|
|
|
|
|
@@ -79,7 +162,8 @@ class LogicalKeyboardKey extends Diagnosticable {
|
|
|
|
|
return keyId == typedOther.keyId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Finds the [LogicalKeyboardKey] that matches the given ID.
|
|
|
|
|
/// Returns the [LogicalKeyboardKey] constant that matches the given ID, or
|
|
|
|
|
/// null, if not found.
|
|
|
|
|
static LogicalKeyboardKey findKeyByKeyId(int keyId) => _knownLogicalKeys[keyId];
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
@@ -131,30 +215,36 @@ class LogicalKeyboardKey extends Diagnosticable {
|
|
|
|
|
/// that had a "do what I mean" key from then on.
|
|
|
|
|
bool get isAutogenerated => (keyId & autogeneratedMask) != 0;
|
|
|
|
|
|
|
|
|
|
/// Mask for the 32-bit value portion of the key code. This is used by
|
|
|
|
|
/// Mask for the 32-bit value portion of the key code.
|
|
|
|
|
///
|
|
|
|
|
/// This is used by
|
|
|
|
|
/// platform-specific code to generate Flutter key codes.
|
|
|
|
|
static const int valueMask = 0x000FFFFFFFF;
|
|
|
|
|
|
|
|
|
|
/// Mask for the platform prefix portion of the key code. This is used by
|
|
|
|
|
/// platform-specific code to generate Flutter key codes.
|
|
|
|
|
/// Mask for the platform prefix portion of the key code.
|
|
|
|
|
///
|
|
|
|
|
/// This is used by platform-specific code to generate Flutter key codes.
|
|
|
|
|
static const int platformMask = 0x0FF00000000;
|
|
|
|
|
|
|
|
|
|
/// Mask for the autogenerated bit portion of the key code. This is used by
|
|
|
|
|
/// platform-specific code to generate new Flutter key codes for keys which
|
|
|
|
|
/// are not recognized.
|
|
|
|
|
/// Mask for the autogenerated bit portion of the key code.
|
|
|
|
|
///
|
|
|
|
|
/// This is used by platform-specific code to generate new Flutter key codes
|
|
|
|
|
/// for keys which are not recognized.
|
|
|
|
|
static const int autogeneratedMask = 0x10000000000;
|
|
|
|
|
|
|
|
|
|
/// The code prefix for keys which have a Unicode representation. This is used
|
|
|
|
|
/// by platform-specific code to generate Flutter key codes.
|
|
|
|
|
/// The code prefix for keys which have a Unicode representation.
|
|
|
|
|
///
|
|
|
|
|
/// This is used by platform-specific code to generate Flutter key codes.
|
|
|
|
|
static const int unicodePlane = 0x00000000000;
|
|
|
|
|
|
|
|
|
|
/// The code prefix for keys which do not have a Unicode representation. This
|
|
|
|
|
/// is used by platform-specific code to generate Flutter key codes using HID
|
|
|
|
|
/// Usage codes.
|
|
|
|
|
/// The code prefix for keys which do not have a Unicode representation.
|
|
|
|
|
///
|
|
|
|
|
/// This is used by platform-specific code to generate Flutter key codes using
|
|
|
|
|
/// HID Usage codes.
|
|
|
|
|
static const int hidPlane = 0x00100000000;
|
|
|
|
|
@@@LOGICAL_KEY_DEFINITIONS@@@
|
|
|
|
|
// A list of all predefined constant LogicalKeyboardKeys so they can be
|
|
|
|
|
// searched..
|
|
|
|
|
// searched.
|
|
|
|
|
static const Map<int, LogicalKeyboardKey> _knownLogicalKeys = <int, LogicalKeyboardKey>{
|
|
|
|
|
@@@LOGICAL_KEY_MAP@@@
|
|
|
|
|
};
|
|
|
|
|
@@ -163,12 +253,88 @@ class LogicalKeyboardKey extends Diagnosticable {
|
|
|
|
|
/// A class with static values that describe the keys that are returned from
|
|
|
|
|
/// [RawKeyEvent.physicalKey].
|
|
|
|
|
///
|
|
|
|
|
/// These represent *physical* keys, which are keys which represent a
|
|
|
|
|
/// particular key location on the keyboard. It ignores any modifiers, modes,
|
|
|
|
|
/// or keyboard layouts which may be in effect. This is contrast to
|
|
|
|
|
/// These represent *physical* keys, which are keys which represent a particular
|
|
|
|
|
/// key location on a QWERTY keyboard. It ignores any modifiers, modes, or
|
|
|
|
|
/// keyboard layouts which may be in effect. This is contrast to
|
|
|
|
|
/// [LogicalKeyboardKey], which represents a logical key interpreted in the
|
|
|
|
|
/// context of modifiers, modes, and/or keyboard layouts.
|
|
|
|
|
///
|
|
|
|
|
/// As an example, if you wanted a game where the key next to the CAPS LOCK (the
|
|
|
|
|
/// "A" key on a QWERTY keyboard) moved the player to the left, you'd want to
|
|
|
|
|
/// look at the physical key to make sure that regardless of the character the
|
|
|
|
|
/// key produces, you got the key that is in that location on the keyboard.
|
|
|
|
|
///
|
|
|
|
|
/// Conversely, if you wanted to implement an app where the "Q" key "quit"
|
|
|
|
|
/// something, you'd want to look at the logical key to detect this, since you
|
|
|
|
|
/// would like to have it match the key with "Q" on it, instead of always
|
|
|
|
|
/// looking for "the key next next to the TAB key", since on a French keyboard,
|
|
|
|
|
/// the key next to the TAB key has an "A" on it.
|
|
|
|
|
///
|
|
|
|
|
/// {@tool snippet --template=stateful_widget}
|
|
|
|
|
/// This example shows how to detect if the user has selected the physical key
|
|
|
|
|
/// to the right of the CAPS LOCK key.
|
|
|
|
|
///
|
|
|
|
|
/// ```dart imports
|
|
|
|
|
/// import 'package:flutter/services.dart';
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// ```dart
|
|
|
|
|
/// // The node used to request the keyboard focus.
|
|
|
|
|
/// final FocusNode _focusNode = FocusNode();
|
|
|
|
|
/// // The message to display.
|
|
|
|
|
/// String _message;
|
|
|
|
|
///
|
|
|
|
|
/// // Focus nodes need to be disposed.
|
|
|
|
|
/// @override
|
|
|
|
|
/// void dispose() {
|
|
|
|
|
/// _focusNode.dispose();
|
|
|
|
|
/// super.dispose();
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// // Handles the key events from the RawKeyboardListener and update the
|
|
|
|
|
/// // _message.
|
|
|
|
|
/// void _handleKeyEvent(RawKeyEvent event) {
|
|
|
|
|
/// setState(() {
|
|
|
|
|
/// if (event.physicalKey == PhysicalKeyboardKey.keyA) {
|
|
|
|
|
/// _message = 'Pressed the key next to CAPS LOCK!';
|
|
|
|
|
/// } else {
|
|
|
|
|
/// _message = 'Wrong key.';
|
|
|
|
|
/// }
|
|
|
|
|
/// });
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// @override
|
|
|
|
|
/// Widget build(BuildContext context) {
|
|
|
|
|
/// final TextTheme textTheme = Theme.of(context).textTheme;
|
|
|
|
|
/// return Container(
|
|
|
|
|
/// color: Colors.white,
|
|
|
|
|
/// alignment: Alignment.center,
|
|
|
|
|
/// child: DefaultTextStyle(
|
|
|
|
|
/// style: textTheme.display1,
|
|
|
|
|
/// child: RawKeyboardListener(
|
|
|
|
|
/// focusNode: _focusNode,
|
|
|
|
|
/// onKey: _handleKeyEvent,
|
|
|
|
|
/// child: AnimatedBuilder(
|
|
|
|
|
/// animation: _focusNode,
|
|
|
|
|
/// builder: (BuildContext context, Widget child) {
|
|
|
|
|
/// if (!_focusNode.hasFocus) {
|
|
|
|
|
/// return GestureDetector(
|
|
|
|
|
/// onTap: () {
|
|
|
|
|
/// FocusScope.of(context).requestFocus(_focusNode);
|
|
|
|
|
/// },
|
|
|
|
|
/// child: Text('Tap to focus'),
|
|
|
|
|
/// );
|
|
|
|
|
/// }
|
|
|
|
|
/// return Text(_message ?? 'Press a key');
|
|
|
|
|
/// },
|
|
|
|
|
/// ),
|
|
|
|
|
/// ),
|
|
|
|
|
/// ),
|
|
|
|
|
/// );
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
|
|
|
|
/// {@end-tool}
|
|
|
|
|
///
|
|
|
|
|
/// See also:
|
|
|
|
|
///
|
|
|
|
|
/// * [RawKeyEvent], the keyboard event object received by widgets that listen
|
|
|
|
|
@@ -176,19 +342,20 @@ class LogicalKeyboardKey extends Diagnosticable {
|
|
|
|
|
/// * [RawKeyboardListener], a widget used to listen to and supply handlers for
|
|
|
|
|
/// keyboard events.
|
|
|
|
|
class PhysicalKeyboardKey extends Diagnosticable {
|
|
|
|
|
/// Defines a KeyboardKey value and optional debug name. The debug name must
|
|
|
|
|
/// be null in release mode.
|
|
|
|
|
/// Creates a PhysicalKeyboardKey object with an optional debug name.
|
|
|
|
|
///
|
|
|
|
|
/// To save executable size, it is recommended that the [debugName] be null in
|
|
|
|
|
/// release mode. You can do this using the [kReleaseMode] constant:
|
|
|
|
|
/// The [usbHidUsage] must not be null.
|
|
|
|
|
///
|
|
|
|
|
/// {@tool sample}
|
|
|
|
|
/// const PhysicalKeyboardKey mySpecialPhysicalKey = PhysicalKeyboardKey(
|
|
|
|
|
/// 0x0010000000a,
|
|
|
|
|
/// debugName: kReleaseMode ? null : 'Special Key',
|
|
|
|
|
/// );
|
|
|
|
|
/// To save executable size, it is recommended that the [debugName] be null in
|
|
|
|
|
/// release mode. You can do this using the [kReleaseMode] constant.
|
|
|
|
|
///
|
|
|
|
|
/// ```dart
|
|
|
|
|
/// const PhysicalKeyboardKey(0x0000ffff, debugName: kReleaseMode ? null : 'Special Key')
|
|
|
|
|
/// ```
|
|
|
|
|
/// {@end-tool}
|
|
|
|
|
const PhysicalKeyboardKey(this.usbHidUsage, {this.debugName});
|
|
|
|
|
const PhysicalKeyboardKey(this.usbHidUsage, {this.debugName})
|
|
|
|
|
: assert(usbHidUsage != null);
|
|
|
|
|
|
|
|
|
|
/// The unique USB HID usage ID of this physical key on the keyboard.
|
|
|
|
|
///
|
|
|
|
|
|