Minecraft Bedrock 1.21.100+ behavior pack, detects which hotbar slot a player selects and assigns the corresponding value to a scoreboard objective. You can use the scores target selector argument in your own commands.
You may dowload the latest release from the Releases Page
Add the Detect Slot add-on to your behavior packs on your world. Behavior packs may disable achievements, but editing the world with an NBT editor can re-enable achievements when uploading your world to a realm.
Re-enable achievements on either pc or mobile with the free NBT editor Dovetail
You can target players using the scores selector argument in your commands:
# Target players who have selected the first slot of the hotbar
execute as @a[scores={detect:slot=1}] run <command># Target players who have selected the last slot of the hotbar
execute as @a[scores={detect:slot=9}] run <command> _ _ _ _ _
(_) _ _ | | | | | | |
____ _ _| |_ _| |_ _____ ____ ___| |___| |_____| | |
| \| (_ _|_ _) ___ | _ \ /___)_____ (____ | | |
| | | | | | |_ | |_| ____| | | |___ | | / ___ | | |
|_|_|_|_| \__) \__)_____)_| |_(___/ |_\_____|\_)_)