Hello Molly826,
Thank you for using Apple Support Communities. Based on your post, you can't connect your wireless keyboard to your iMac.
If this is happening with the Magic Keyboard, try these steps out: If you can't connect your Magic Keyboard, Magic Mouse, or Magic Trackpad to your Mac
You can also connect a wired keyboard to the iMac, then log in and try reconnecting the wireless keyboard using System Preferences > Bluetooth.
Should it persist, what happens when you connect a wired keyboard to the iMac? Does it respond properly? Were you able to log in? Has there been any damage to this wireless keyboard at all?
Hoping to hear back, take care.