Can I simulate pressing Escape for example?
There is a macro in this post → Create a macro that press “Enter” automatic once.
It specifically deals with the Enter key. You would just need to substitute the Enter values for what you want.
For example the Esc key:
oKeyEvent.KeyCode = com.sun.star.awt.Key.ESCAPE REM 1281. oKeyEvent.KeyChar = chr( 27 )
Also note that the routine is for a Base form. Some modification may be needed for use in Writer or another module.
Just tested in Writer doc as posted with ENTER and worked without modification.