Wireless keyboard for mac keeps losing connection
Connect a wired USB keyboard / Mouse to the Mac (most any USB keyboard will do).  While holding Option + Shift click on the Bluetooth menu bar icon.  Go to Debug.  There is an an option to reset the Bluetooth module as well as factory resetting Apple Bluetooth devices.

Make sure your Mac is up-to-date with macOS and all updates
Charge the Apple Bluetooth devices fully
Perform the SMC Reset first
Reset the Bluetooth Module under the Bluetooth Debug menu (holding Option + Shift while clicking Bluetooth menu bar item)
Factory Reset the Apple Bluetooth Devices (Keyboard, etc.)
Re-pair the Apple Bluetooth devices from scratch

Check for any nearby devices that might be putting out a radio signal or EMF (Electro-Magnetic Field) Such as motorized fan, space heater, an old monitor, a radio transmitter, etc. Move them away from the Mac and your bluetooth devices. Don't plug them into the same power strip, etc. Check for other devices plugged into the Mac such as 3rd party docking stations, especially ones not actually certified by Apple (HP, etc.) try disconnecting them for awhile see if the problem goes away.
The Mac Mini M1 was plagued by bluetooth issues on Big Sur prior to 11.3.x. Latest release fixed all the bluetooth issues. Recommend using a tool such as SilentKnight to verify your firmware is up-to-date. There have been issues with Mac's updating macOS but failing to update the firmware. SilentKnight will let you know if you are missing the appropriate firmware update among other security updates.  You can find the free SilentKnight on the Eclectic Light Company website. https://eclecticlight.co make sure you search the site for &quot;bluetooth&quot; there are several blogs talking about disconnect issues and tooling to help trouble-shooting. Some USB3 devices have been known to disrupt Bluetooth. When connecting USB 3 devices to a USB-C port, Apple recommends using the front port on the left-hand side, or any USB-C port on the right side of the Mac. Use a longer USB-C cable and move the USB3 device further away from the antenna's for Wi-Fi / Bluetooth. Apple has a Wi-Fi and Bluetooth support page https://support.apple.com/en-gb/HT201542