You will need to install a user script manager extension to install this script.
This is the most comprehensive hat macro you'll ever find.
You can equip any hat with any key you desire from the menu.
The keycodes you enter in the menu and save will PERSIST even if you CLOSE the page and REOPEN it.
If you have any questions about identifying the keycode for a specific key on your keyboard, this page will assist you: https://www.toptal.com/developers/keycode