If the problem doesn't occur when the Magic Keyboard has been disconnected, this might suggest that the keyboard has a fault.
You might be best advised to visit the Genius Bar at your local Apple Store or Apple Authorised Service Provider (AASP) - where the technicians will be able to test and assess both your iPad and Magic Keyboard for faults:
Genius Bar Reservation and Apple Support Options - Apple
If you have access to another compatible iPad, you might consider testing the keyboard with the other iPad. If the fault recurs, you have demonstrated that keyboard has a fault. Conversely, if the keyboard works normally with the substitute iPad, then your iPad has a fault.