I was away for a couple of days and switched off the iMac. When I returned and started up (actually my wife did this, I was still away, but when I got back there was a message on screen) saying the computer needed to be shut down or to restart. When I restarted it I was asked to pair the keyboard again. It seems to be registering that there is a keyboard, but it doesn't finish the process. See images:

