Ask Your Question

Create a macro that press "Enter" automatic once. [closed]

asked 2017-08-16 21:50:48 +0100

WilliamLibreoffice gravatar image

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?

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Alex Kemp
close date 2020-10-13 01:04:55.814235


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.

Ratslinger gravatar imageRatslinger ( 2017-08-17 00:25:53 +0100 )edit

1 Answer

Sort by » oldest newest most voted

answered 2017-08-17 05:45:00 +0100

librebel gravatar image

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
    oKeyEvent.Modifiers = 0     REM A combination of
    oKeyEvent.KeyCode   =               REM 1280.
    oKeyEvent.KeyChar   = chr( 13 )
    simulate_KeyPress( oKeyEvent )
End Sub

Sub simulate_KeyPress( oKeyEvent As )
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
        oToolkit.keyPress( oKeyEvent )          REM methods of XToolkitRobot.
        oToolkit.keyRelease( oKeyEvent )
    End If
End Sub
edit flag offensive delete link more


Thanks man, I going to try that!

WilliamLibreoffice gravatar imageWilliamLibreoffice ( 2017-08-30 05:21:13 +0100 )edit

You're welcome :)

The same "RoboToolkit" allows to simulate Mouseclicks too...

librebel gravatar imagelibrebel ( 2017-08-30 06:02:07 +0100 )edit

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.

Jim K gravatar imageJim K ( 2020-05-25 20:42:47 +0100 )edit

Question Tools

1 follower


Asked: 2017-08-16 21:50:48 +0100

Seen: 2,526 times

Last updated: Aug 17 '17