Detect whether a change is from user typing OR from a method

I have an implementation where user can type in some code but the code can also change from an external event - which calls the

 cm.dispatch({
    changes: { from, to, insert: code },
  });

I’d like to know if a recent change is because the user manually typed something OR because the external event generated it.

It looks like I can add Annotation to a transaction and then use the isUserEvent function to distinguish them. But not sure how to actually do this. Are there any examples that implement adding annotations?

Just include annotations: [your annotations] in your transaction spec.