I managed to find the issue - I run windowed mode on multi-screens. When moving the mouse it went outside the bounds of the Minecraft window and right clicking cause it to lose focus.
Enabling fullscreen mode, then turning it off again has seemed to reset Minecraft’s mouse tracking so that even if the mouse is outside the window it doesn’t lose focus.
I managed to find the issue - I run windowed mode on multi-screens. When moving the mouse it went outside the bounds of the Minecraft window and right clicking cause it to lose focus.
Enabling fullscreen mode, then turning it off again has seemed to reset Minecraft’s mouse tracking so that even if the mouse is outside the window it doesn’t lose focus.