To connect it with the iMac again, see the following steps from Set up your Magic Keyboard, Magic Mouse, or Magic Trackpad with your Mac - Apple Support
If the device came in its own box or you need to set it up again, follow the steps below.
Set up Magic Keyboard, Magic Mouse, or Magic Trackpad
- Use one of these cables to connect your wireless device to your Mac:
- Lightning to USB Cable
- If you can't connect the cable because your device doesn't have a Lightning port, follow the steps in the next section.
2- Turn on the device. You should see green under its power switch.
3 - The device uses the USB connection to recharge its battery and automatically pair with your Mac. To check its charge level and confirm that it's paired, choose Apple menu > System Preferences, then click Bluetooth. The device should appear in the list of Bluetooth devices.
4- Unplug the device for wireless use.