If you use your iPad as a second display for your Mac via Sidecar, you can connect to it using a keyboard shortcut with Keyboard Maestro.
You can download the macro here or create it yourself using the following instructions. (If you download it, you’ll still need to set the device name as described below.)
Create a macro with an Execute AppleScript action. Copy and paste this code into the action:
In the first line of the script, change "The name of your iPad" to the real name of your iPad (leave the quotation marks around the name). (You can find your iPad’s name in the AirPlay menu.)
Set your desired hot key (I’m using
⌃ Key Pad +), and you’re all set. Press the hot key, and your Mac will connect to the iPad. Press the hot key again, and it will disconnect.
(You could probably connect to an Apple TV using the same script, but I don’t have an Apple TV, so I can’t verify it.)