Fix and optimize cursor detection (#12537)

Change-Id: Ic91604641219c6173f839cbd1e3ff41a8adf835e