How to reset Magic Keyboard?
To reset your keyboard:


  Hold down the Shift and Option keys ('Alt' on some keyboards) and at the same time click on the Bluetooth icon in the menu bar. Once the menu is showing, release the keys. From the Bluetooth menu, choose Debug > Remove All Devices. Reboot your Mac, then setup your keyboard and/or mouse as normal.



http://davemeehan.com/technology/how-to-reset-an-apple-wireless-bluetooth-keyboard-mouse-or-trackpad