Create a macro that press "Enter" automatic once

I don’t know how can I make that, I try, bun don’t worked record Macro and press ENTER, so I Wold like have the code of the macro, anyone can help-me?

It may be easier to help if you explained what you are trying to accomplish. The Enter key may react differently depending upon what you are doing.

Hello @WilliamLibreoffice,

To automatically press and release the ENTER -key in the current window, you could use the following Basic macro:

Sub simulate_KeyPress_RETURN()
REM Simulate a RETURN Key press ( and -release ) in the current Window.
REM NB. This can cause the triggering of window elements.
	Dim oKeyEvent As New com.sun.star.awt.KeyEvent
	oKeyEvent.Modifiers	= 0		REM A combination of com.sun.star.awt.KeyModifier.
	oKeyEvent.KeyCode	= com.sun.star.awt.Key.RETURN				REM 1280.
	oKeyEvent.KeyChar	= chr( 13 )
	simulate_KeyPress( oKeyEvent )
End Sub

Sub simulate_KeyPress( oKeyEvent As com.sun.star.awt.KeyEvent )
REM Simulate a Key press ( and -release ) in the current Window.
REM NB. This can cause the triggering of window elements.
REM For example if there is a button currently selected in your form, and you call this method
REM while passing the KeyEvent for RETURN, then that button will be activated.
	If Not IsNull( oKeyEvent ) Then
		Dim oWindow As Object, oToolkit As Object
		oWindow = ThisComponent.CurrentController.Frame.getContainerWindow()
		oKeyEvent.Source = oWindow		
		oToolkit = oWindow.getToolkit()			REM com.sun.star.awt.Toolkit
		oToolkit.keyPress( oKeyEvent )			REM methods of XToolkitRobot.
		oToolkit.keyRelease( oKeyEvent )
	End If
End Sub
2 Likes

Thanks man, I going to try that!

You’re welcome :slight_smile:

The same “RoboToolkit” allows to simulate Mouseclicks too…

There is a bug in this code. It should say oKeyEvent.KeyChar = CByte( 13 ) instead. The posted code does work for RETURN because the KeyChar property is not required in that case.