If you mean mouse cursor, nothing can prevent moving it.
If you mean text input caret, you don't want to prevent its motion, because it's used to fill in the text input control.
So, the question, as formulated, makes no sense.
If you mean to capture shifting of the keyboard focus, you can do it by handling some of the focus-related events:
blur, change, focus, focusin, focusout
:
Event reference | MDN[
^].
—SA