Remove listener when global var reset to null

While playing around with XKeyHandler listerner in basic, I came across a problem where every time I edit the code, my global variable, that stores the object from an earlier activation of the listener, it is reset to null but the listener is still active. So the only way to clear the listener attached to the document is to close and reopen it. So to be clear, if I don’t make any changes in the LO’s Basic editor then the code runs fine. Is there another way to clear the listener on the active document?

Global oXKeyHandler As Object

const K_BACKSPACE = 1283
const K_SPACE = 1284
const K_DELETE = 1286
const K_RETURN = 1280


Sub StartXKeyHandler()
    If IsNull(oXKeyHandler) Then
        oXKeyHandler = CreateUnoListener("XKeyHandler_", "com.sun.star.awt.XKeyHandler")
        ThisComponent.GetCurrentController.AddKeyHandler(oXKeyHandler)
        print "Key handler activated."
	else
		print "Key Handler already active!"
    End If
End Sub

Sub StopXKeyHandler()
    If Not IsNull(oXKeyHandler) Then
        ThisComponent.GetCurrentController.removeKeyHandler(oXKeyHandler)
        oXKeyHandler = Nothing
        print "Key handler has been DEACTIVATED."
	else
		print "Key handler is NOT active at the moment."
    End If
End Sub

Sub XKeyHandler_disposing(oEvent)
    Call StopXKeyHandler
End Sub

Function XKeyHandler_keyPressed(oEvent) As Boolean
    XKeyHandler_keyPressed = False  'Don't cancel this‼

   dim vCode
	
    vCode = oEvent.KeyCode
    
    select case vCode 
    	case K_SPACE, K_BACKSPACE, K_RETURN:
    		print "Got it!"
    end select
End Function

Function XKeyHandler_keyReleased(oEvent) As Boolean
    XKeyHandler_keyReleased = False  'Don't cancel this‼
End Function

Using,

Version: 7.0.5.2 (x64)
Build ID: 64390860c6cd0aca4beafafcfd84613dd9dfb63a
CPU threads: 12; OS: Windows 10.0 Build 19043; UI render: Skia/Raster; VCL: win
Locale: en-AU (en_AU); UI: en-GB
Calc: CL

Try using constructs:

If oXKeyHandler Is Nothing Then

and

If Not (oXKeyHandler Is Nothing) Then

It works correctly for me in the specified edition.
LO 7.3.4.2 Win 10.

Downloaded and tested 7.3.4.2 on a fresh profile and behaves the same way. It is actually Null according to the watch function of the Basic editor. So its not what I am testing for the wrong type but that it resets the variable so I have no reference to pass to the remove function. Quoting Andrew Pitonyak,

Variant variables start with no value at all; they are initially empty. Object variables are initialized with the value null. Use the functions IsEmpty and IsNull to test these conditions. Use the IsObject function to determine if a variable is an object.

I’m sorry, I didn’t pay attention to your bold words. Let me also quote from A. Pitonyak (OOME_4_0.odt ):

Global variables are reset when the containing library is compiled. Exiting and restarting OpenOffice.org causes all libraries to be compiled and all Global variables to be initialized. Modifying the module containing the Global definition also forces the module to be recompiled.

A rule from my own experience is to stop all previously running listeners before modifying the macro code.

Thanks for the quote, makes sense. I was too focused on the listener and didn’t think to look at the global variables section. Any way other way to clear the listener?

Your macro StopXKeyHandler is the right way to go. If desired, it can be assigned to a hot key.