BASIC Macro seems unable to send "SPACE" keystroke

I tried creating a simple macro to repeatedly send 3 keystrokes: END, SPACE, DELETE. This is what I have been keying over & over to eliminate hard carriage returns in text from a source I regularly copy/paste into a presentation (impress). But the macro seems to only like sending END and DELETE, ignoring the SPACE keypress & release, which is vital for this to work properly.

I just have the macro repeating this 10 times for now. Ideally I’d like to detect when I’m at the end of the text, but that’s less important. If sending the space would work I could simply loop 100 times (or whatever) and it would remove the hard returns and add a bunch of spaces at the end if it ran too many times.

sub removeNewLines
	rem https://api.libreoffice.org/docs/idl/ref/namespacecom_1_1sun_1_1star_1_1awt_1_1Key.html
	Dim oWindow As Object, oToolkit As Object
	Dim oKeyEvent_EOL As New com.sun.star.awt.KeyEvent
	Dim oKeyEvent_SPACE As New com.sun.star.awt.KeyEvent
	Dim oKeyEvent_DELETE As New com.sun.star.awt.KeyEvent
	oWindow = ThisComponent.CurrentController.Frame.getContainerWindow()

	oKeyEvent_EOL.Source = oWindow		
	oKeyEvent_EOL.KeyCode = com.sun.star.awt.Key.MOVE_TO_END_OF_LINE  REM 1547
	oKeyEvent_EOL.Modifiers	= 0		REM A combination of com.sun.star.awt.KeyModifier.
	oKeyEvent_EOL.KeyChar = 0	REM using keycodes
	
	oKeyEvent_SPACE.Source = oWindow		
	oKeyEvent_SPACE.KeyCode = com.sun.star.awt.Key.SPACE  REM com.sun.star.awt.Key.SPACE  REM 1284
	oKeyEvent_SPACE.Modifiers	= 0		REM A combination of com.sun.star.awt.KeyModifier.
	oKeyEvent_SPACE.KeyChar = chr(32)
	
	oKeyEvent_DELETE.Source = oWindow		
	oKeyEvent_DELETE.KeyCode = com.sun.star.awt.Key.DELETE  REM 1286
	oKeyEvent_DELETE.Modifiers	= 0		REM A combination of com.sun.star.awt.KeyModifier.
	oKeyEvent_DELETE.KeyChar = 0	REM using keycodes

	oToolkit = oWindow.getToolkit()			REM com.sun.star.awt.Toolkit
	for i=0 to 10
		oToolkit.keyPress( oKeyEvent_EOL )			REM methods of XToolkitRobot.
		oToolkit.keyRelease( oKeyEvent_EOL )
		oToolkit.keyPress( oKeyEvent_SPACE )			REM methods of XToolkitRobot.
		oToolkit.keyRelease( oKeyEvent_SPACE )
		oToolkit.keyPress( oKeyEvent_DELETE )			REM methods of XToolkitRobot.
		oToolkit.keyRelease( oKeyEvent_DELETE )
	next
end sub

Not an answer, just a note: remember that keyPress/keyRelease are part of an unpublished interface, which is subject to change without notice.

.KeyChar = 32

seems like another XY problem - Wikipedia
 
might be simply :

	oSel = ThisComponent.CurrentController.Selection.getByIndex(0)
	oSel.string = replace(oSel.string, chr(10), " ")

imageimage

Thanks for the search/replace solution. I had tried that but I couldn’t find the right starting object to run it on and even had a couple failed attempts with your solution until I noticed the text box had to be what was selected.

Changing to .keyChar = 32 seemed to work for the space key, but contradicts other sample code I’d seen for sending keystrokes and seems to contradict the spec which says it’s a character, not the numeric value of the character. With that change it now goes to the end of the line, sends the space and delete keys without going to the new end of line created by pressing delete. So that isn’t workable.

It’s very difficult to navigate these structures and definitions to determine how to accomplish anything with libreoffice macros until you’ve stumbled around for ages.

had hoped screenshots were explanatory :expressionless:

Chr Function returns a string

like where ?

yep.
learning curve is unfortunately tedious.

therefore, personal therapy is available :

1 Like

Pierre: what does this have to do with user support on macro authoring? You seem to be increasingly quoting me out of context and twisting my words, including yesterday when you took a comment I made on software development in general and applied it to TDF decision making.

1 Like

When trying to get something working I found these 2 “Ask libreoffice” posts that both use chr(ascii-value):

https://ask.libreoffice.org/t/create-a-macro-that-press-enter-automatic-once/27172
https://ask.libreoffice.org/t/macro-how-to-simulate-pressing-a-key/47132

1 Like

yep, and at the bottom :

1 Like