From 32e4b9762bd86e092def4a8af90227ea7a4f82b2 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 30 Dec 2025 12:41:13 +0000 Subject: [PATCH] Refactor: Use maps instead of if-else chains for key mappings Co-authored-by: jonasbark <1151304+jonasbark@users.noreply.github.com> --- .../keypress_simulator_method_channel.dart | 23 +++++++--------- .../keypress_simulator_windows_plugin.cpp | 27 +++++++++---------- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/keypress_simulator/packages/keypress_simulator_platform_interface/lib/src/keypress_simulator_method_channel.dart b/keypress_simulator/packages/keypress_simulator_platform_interface/lib/src/keypress_simulator_method_channel.dart index fd77103..798a517 100644 --- a/keypress_simulator/packages/keypress_simulator_platform_interface/lib/src/keypress_simulator_method_channel.dart +++ b/keypress_simulator/packages/keypress_simulator_platform_interface/lib/src/keypress_simulator_method_channel.dart @@ -65,21 +65,16 @@ class MethodChannelKeyPressSimulator extends KeyPressSimulatorPlatform { @override Future simulateMediaKey(PhysicalKeyboardKey mediaKey) async { // Map PhysicalKeyboardKey to string identifier since keyCode is null for media keys - String? keyIdentifier; - if (mediaKey == PhysicalKeyboardKey.mediaPlayPause) { - keyIdentifier = 'playPause'; - } else if (mediaKey == PhysicalKeyboardKey.mediaStop) { - keyIdentifier = 'stop'; - } else if (mediaKey == PhysicalKeyboardKey.mediaTrackNext) { - keyIdentifier = 'next'; - } else if (mediaKey == PhysicalKeyboardKey.mediaTrackPrevious) { - keyIdentifier = 'previous'; - } else if (mediaKey == PhysicalKeyboardKey.audioVolumeUp) { - keyIdentifier = 'volumeUp'; - } else if (mediaKey == PhysicalKeyboardKey.audioVolumeDown) { - keyIdentifier = 'volumeDown'; - } + final keyMap = { + PhysicalKeyboardKey.mediaPlayPause: 'playPause', + PhysicalKeyboardKey.mediaStop: 'stop', + PhysicalKeyboardKey.mediaTrackNext: 'next', + PhysicalKeyboardKey.mediaTrackPrevious: 'previous', + PhysicalKeyboardKey.audioVolumeUp: 'volumeUp', + PhysicalKeyboardKey.audioVolumeDown: 'volumeDown', + }; + final keyIdentifier = keyMap[mediaKey]; if (keyIdentifier == null) { throw UnsupportedError('Unsupported media key: $mediaKey'); } diff --git a/keypress_simulator/packages/keypress_simulator_windows/windows/keypress_simulator_windows_plugin.cpp b/keypress_simulator/packages/keypress_simulator_windows/windows/keypress_simulator_windows_plugin.cpp index 88c92ff..4b36789 100644 --- a/keypress_simulator/packages/keypress_simulator_windows/windows/keypress_simulator_windows_plugin.cpp +++ b/keypress_simulator/packages/keypress_simulator_windows/windows/keypress_simulator_windows_plugin.cpp @@ -12,6 +12,7 @@ #include #include +#include using flutter::EncodableList; using flutter::EncodableMap; @@ -261,23 +262,21 @@ void KeypressSimulatorWindowsPlugin::SimulateMediaKey( std::string keyIdentifier = std::get(args.at(EncodableValue("key"))); // Map string identifier to Windows virtual key codes - UINT vkCode = 0; - if (keyIdentifier == "playPause") { - vkCode = VK_MEDIA_PLAY_PAUSE; - } else if (keyIdentifier == "stop") { - vkCode = VK_MEDIA_STOP; - } else if (keyIdentifier == "next") { - vkCode = VK_MEDIA_NEXT_TRACK; - } else if (keyIdentifier == "previous") { - vkCode = VK_MEDIA_PREV_TRACK; - } else if (keyIdentifier == "volumeUp") { - vkCode = VK_VOLUME_UP; - } else if (keyIdentifier == "volumeDown") { - vkCode = VK_VOLUME_DOWN; - } else { + static const std::unordered_map keyMap = { + {"playPause", VK_MEDIA_PLAY_PAUSE}, + {"stop", VK_MEDIA_STOP}, + {"next", VK_MEDIA_NEXT_TRACK}, + {"previous", VK_MEDIA_PREV_TRACK}, + {"volumeUp", VK_VOLUME_UP}, + {"volumeDown", VK_VOLUME_DOWN} + }; + + auto it = keyMap.find(keyIdentifier); + if (it == keyMap.end()) { result->Error("UNSUPPORTED_KEY", "Unsupported media key identifier"); return; } + UINT vkCode = it->second; // Send key down event INPUT inputs[2] = {};