Fatal Error in KeyHandler_keyPressed, an uninitialised event object?

Using LO version, Ubuntu 18.04.3.

When I run the below macro in Impress with the msgbox commented out, then pressing a key causes a Fatal Error: Type coercion failed: TYPE is not supported!

However if the msgbox is not commended out, then the code runs almost fine (except that I get repeated message boxes for a single key press, that I cannot explain either.)

I would be very grateful to anyone who can explain to me why this is happening.


Global oController
Global oKeyHandler 
Global oPresentation

Sub Main
  oController = ThisComponent.getCurrentController

  oKeyHandler = createUnoListener( "KeyHandler_", "com.sun.star.awt.XKeyHandler" )
  oController.addKeyHandler( oKeyHandler )
  oPresentation = ThisComponent.Presentation

End Sub

Function KeyHandler_keyPressed( oEvent ) as Boolean

  If oEvent.keyCode = com.sun.star.awt.Key.A Then

	REM msgbox "A was pressed"
	 KeyHandler_keyPressed = true
  ElseIf oEvent.keyCode <> com.sun.star.awt.Key.A Then

	 REM msgbox "Not A was pressed"
	 KeyHandler_keyPressed = false

  End If
End Function


Please take care about the preformatted text


I can show you how to get a key handler working but in your context it is not clear what or why you are trying to do this. The key handler is NOT present during the actual presentation. Only an ‘Event’ handler can be added to the presentation as far as can be seen through exam of contents. So running the code:


does nothing but runs through the presentation before allowing you to use the key handler. Please further explain your intentions by editing your question or use add a comment. Please do not reply with an Answer.

Thank you for your response.

I want the keyhandler to be listening through-out the presentation. To do that I was starting the presentation using the main subroutine, only because that was the first thing I thought of.

There are probably better architectures, but this one ‘works’ just fine, except that it crashes if I don’t have the msgbox in the code. I would quite like to understand why, just out of curiosity, but I would also like to fix it.

Is there a way I can upload my odp file here?


To upload your file, please edit your question. Then using the toolbar (upper left in question) there is a paperclip icon. Click on that to add your sample.

I will be glad to look at and provide code for the entire handler (yours is lacking parts). I am especially curious as my testing did not provide for the key handler to be active during the presentation.

The only way for the macro to be running during the presentation, is to start the presentation by running the Main subroutine.

I have uploaded the test file.


Thank you for posting the sample. As I am not much into Impress your sample was helpful.

First here is a post about creating a listener → How to properly code a broadcaster and listener in LO Basic.

Now this link also mentions Pitonyak book (Open Office Macros Explained). Here is a link for that PDF → OOME

My problem was the running of the Presentation. If run as you have set the attached sample works. However, and as stated not much experience with Impress, my testing was done with Presentation set to Full Screen and in that case the handler does not display messages during the presentation.

Now your problem is mostly due to the fact of lacking methods. When coding ANY handler you need to include all methods except for queryInterface. The linked post relates some of this and the book goes into more detail. In your case you are missing the keyReleased and disposing methods. These should be present even if empty. Also you only need to define the handler object as a global element. And you have no method to stop (remove) the handler. Without doing this every subsequent execution of your main routine "stacks’ another handler. This can certainly cause problems.

Now I have taken your sample and completely replaced the code. There are a few notes. You still start by running Main. Have added a message box to show the handler was started. When finished with the presentation, you should run StopKeyHandler to remove the handler. This also has a message to show it was removed. Also the start & stop of the handler does some checking if the handler exists to help eliminate some of these issues.

Sample ----- KeyHandler.odp

You can test with the messages boxes on and off. As posted, A is on and Not A is off - your problem situation.

Thank you very much, I am extremely grateful for the time you have spent on this, I am very pleased to have it working.

As with all questions, if this answers your question please tick the :heavy_check_mark: (upper left area of answer). It helps others to know there was an accepted answer.