Repository: flutter
Issue: [Windows][HardwareKeyboard] Incorrect Caps Lock status when starting the app with Caps Lock On
Labels: a: text input,framework,engine,platform-windows,a: desktop,has reproducible steps,P2,found in release: 2.10,found in release: 2.11,team-windows,triaged-windows

[EDIT] Changed the issue title and please refer to the [comment](https://github.com/flutter/flutter/issues/98377#issuecomment-1040906016) below.

The issue is that if you start the app with Caps Lock enabled, flutter reports the lock mode off and once you disable it, the lock mode is reported as on (present in the `HardwareKeyboard.instance.lockModesEnabled`)

Another separate issue is that on Dartpad, HardwareKeyboard stops calling the handler after the very first event, regardless of the type of event, so this issue can't be tested on Dartpad.

On MacOS the issue is not present, but, the lockMode is not added until an event happens, however, the status is reported correctly (unlike windows which shows reversed behavior, on when off and off when on).

[OLD COMMENT FROM HERE] *************************************

When I was working on my Mac, I found a way to detect caps lock, it is a bit weird, but it worked. Basically I add a listener to `RawKeyboard.instance` that checks for `RawKeyboard.instance.keysPressed` to contain either `LogicalKeyboardKey.capsLock` or `PhysicalKeyboardKey.capsLock`. Whenever Caps Lock was on, it was considered pressed and whenever it was off it was considered not pressed, I thought it was odd since keyPressed usually is reserved to store keys that are *actually* pressed, but ok, it worked.

I recently started working on my Windows PC and my little way of detecting caps lock stopped working. Now, the behavior is as I expected it should be (caps lock key is added to the `keysPressed` set on `KeyDownEvent` and removed with a `KeyUpEvent`). At first I thought it was due to the latest flutter version, but I was wrong. I went back to 2.5.1, 2.8.0, 2.10 and again to 2.10.1 puzzled because it used to work, just to figure out it wasn't the flutter version, it's the platform support.

The problem is that there's just no way to know when the Caps Lock Key is ON or OFF. So, making a simple Password Input Field that warns the user when the caps lock is ON is just impossible (for stuff like creating/changing passwords on an account).

I already checked and re-read all the documentation on HardwareKeyboard and RawKeyboard and could not find a solution. I'm not sure if this should be a feature request, if this is a bug (because the behavior on mac is correct?) or it's something that was overlooked when making the keyboard support for web and desktop for Windows (or maybe I'm just doing things wrong).

## Steps to Reproduce
- Use the following minimal code to reproduce. Check on MacOS and you'll see that it works as expected. On windows it doesn't work.
- You can use Dart Pad to test on both MacOS and Windows.
- I tested Web and Desktop on Windows 10 and MacOS Monterrey.

```dart
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      debugShowCheckedModeBanner: false,
      home:  MyWidget(),
    );
  }
}

class MyWidget extends StatefulWidget {
  const MyWidget({
    Key? key,
  }) : super(key: key);

  @override
  State<MyWidget> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  bool _capsOn = false;

  @override
  void initState() {
    super.initState();
    _capsOn = _isCapsLockOn();
    RawKeyboard.instance.addListener(_onKeyEvent);
  }

  void _onKeyEvent(RawKeyEvent event) {
    if (event.logicalKey == LogicalKeyboardKey.capsLock ||
        event.physicalKey == PhysicalKeyboardKey.capsLock) {
      setState(() {
        _capsOn = _isCapsLockOn();
      });
    }
  }

  bool _isCapsLockOn() {
    return RawKeyboard.instance.keysPressed
            .contains(PhysicalKeyboardKey.capsLock) ||
        RawKeyboard.instance.keysPressed.contains(LogicalKeyboardKey.capsLock);
  }

  @override
  void dispose() {
    RawKeyboard.instance.removeListener(_onKeyEvent);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text('Caps Lock: ${_capsOn ? "ON" : "OFF"}'),
      ),
    );
  }
}

```

## Flutter Doctor
```

PS D:\Repos> flutter doctor -v
[✓] Flutter (Channel stable, 2.10.1, on Microsoft Windows [Version 10.0.19043.1526], locale en-CA)
    • Flutter version 2.10.1 at C:\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision db747aa133 (4 days ago), 2022-02-09 13:57:35 -0600
    • Engine revision ab46186b24
    • Dart version 2.16.1
    • DevTools version 2.9.2

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.0-rc2)
    • Android SDK at D:\Android
    • Platform android-31, build-tools 30.0.0-rc2
    • ANDROID_HOME = D:\Android
    • Java binary at: D:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

[✓] Visual Studio - develop for Windows (Visual Studio Professional 2019 16.6.2)
    • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional
    • Visual Studio Professional 2019 version 16.6.30204.135
    • Windows 10 SDK version 10.0.18362.0

[✓] Android Studio (version 4.0)
    • Android Studio at D:\Program Files\Android\Android Studio
    • Flutter plugin version 49.0.2
    • Dart plugin version 193.7547
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)

[✓] VS Code (version 1.64.2)
    • VS Code at C:\Users\nosmi\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.34.0

[✓] Connected device (4 available)
    • SM G950W (mobile) • 988bdc454558353254 • android-arm64  • Android 9 (API 28)
    • Windows (desktop) • windows            • windows-x64    • Microsoft Windows [Version
      10.0.19043.1526]
    • Chrome (web)      • chrome             • web-javascript • Google Chrome 98.0.4758.82
    • Edge (web)        • edge               • web-javascript • Microsoft Edge 96.0.1054.34

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!
```