toggleHotKey method

void toggleHotKey([
  1. bool? value
])

Toggles hotKeyRecording on and off, storing the _keysRecorded.

Implementation

void toggleHotKey([bool? value]) {
  if (value == false) {
    if (_keysRecorded.isNotEmpty) {
      final List<HotKeyModifier> modifiers = [];
      PhysicalKeyboardKey? lastKey;

      for (var e in _keysRecorded) {
        if (e.logicalKey.isModifier) {
          modifiers.add(e.logicalKey.asModifier!);
        } else {
          lastKey = e.physicalKey;
        }
      }

      _settingsRepo.setMuteKeys([
        ...modifiers.map((e) => e.name),
        if (lastKey != null) lastKey.usbHidUsage.toString(),
      ]);
    }
  }

  value ??= !hotKeyRecording.value;

  _timer?.cancel();
  _timer = null;
  _keysRecorded.clear();

  hotKeyRecording.value = value;

  if (hotKeyRecording.value) {
    HardwareKeyboard.instance.addHandler(_hotKeyListener);
  } else {
    HardwareKeyboard.instance.removeHandler(_hotKeyListener);
  }
}