Wireless Keyboard Connection
I guess you are using the original Apple keyboard? In this case, you shouldn't have any problems with the bluetooth connections when the keyboard is in front of the iMac (and your Mac is located in a "normal" office, without lots of over the air devices that can cause a lot of noise).

To me, this sounds like a hardware defect. I would take the keyboard back to Apple/reseller to get a new one.