Not quite the same situation, but you might use the pics below as an idea. Lots of 4wd levers don't have a detent - or not much of one. They seem to rely on luck and driveshaft windup torque to keep the 4wd engaged.
Driveshaft windup happens in 4wd tractors because they have the tire/transmission ratio biased so that the tractor will always steer even in 4wd. But it is possible with worn or new tires to reduce that ratio to near zero. Then it will usually stay in 2wd OK, but there isn't any there isn't any front driveshaft windup to help keep it in 4wd. When that happens, even a little bump can cause the weight of the lever to shift out of 4wd.
Our kubota doesn't have that problem, but the position of the 4wd lever is where my heel hits it and shifts into and out of 4wd. So I mounted the gate latch shown below. Now it stays where it should be.
rScotty