I had the same problem with a Surface Pro 4, the keyboard never worked on it again. There seem to a lot of reports on the SP4 doing that... I bought a Brydge keyboard and never looked back. Still on sale at Amazon ($29) https://www.amazon.com/dp/B08CWTKZQV?ref=nb_sb_ss_w_as-reorder-t1_ypp_rep_k0_1_6&=&crid=2DEEB3X37TDMI&=&sprefix=brydge
It is a common problem with the SP4, it happed to me also. Buy this: https://www.amazon.com/Brydge-Wireless-Precision-Compatible-Microsoft/dp/B08CWTKZQV/ref=sr_1_3?crid=22ODQ4F8WRVXA&keywords=brydge%2B12.3%2Bsurface%2Bpro%2Bkeyboard&qid=1673300379&sprefix=brydge%2Caps%2C272&sr=8-3&th=1
The battery life on it is incredible, and it works everytime!
I had a Surface Pro 4 keyboard do the same thing, it would work sometimes, then it stopped, never to work again. I did purchase a new one but that wouldn't work either ( it is not the keyboard it is the Surface). So, I bought this one on Amazon: https://www.amazon.com/Brydge-Wireless-Precision-Compatible-Microsoft/dp/B08CWTKZQV/ref=sr_1_2?crid=PQDBT388NS73&keywords=brydge+surface+pro+4&qid=1688164828&sprefix=brydge+surface+pro%2Caps%2C253&sr=8-2 It works perfectly, buy it!!!