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