Libreoffice Base - 如何从一个宏中调用一个控制事件?

问题描述 投票:1回答:1

问题:我需要手动调用一个对象监听器事件(如按键)来触发一个函数。 我需要手动调用一个对象监听器事件(如按下的键)来触发一个函数。 我曾经在Access中这样做,但在LibreOffice Base中没有找到相关文档。

上下文。 7年前,我从软件开发中退休,我在LibreOffice Base中建立了一个数据库,帮了一个朋友的忙。 之前有Access的经验--但更多的是使用Oracle、PLSQL、APEX等! 我在让它做我知道可以做的事情时有点困难!

database macros libreoffice libreoffice-basic libreoffice-base
1个回答
0
投票

这是我目前尝试过的代码。

Sub CauseKeyPressedEventToBeFired
    oDoc = ThisComponent
    oController = oDoc.getCurrentController()
    oVC = oController.getViewCursor()
    oForm = oDoc.getDrawpage().getForms().getByName("Form")
    oTextBox = oForm.getByName("Text Box 1")
    oControlView = oController.getControl(oTextBox)
    oControlView.setFocus()
    Dim oEvent As New com.sun.star.awt.KeyEvent
    oEvent.Source = oControlView
    oEvent.KeyCode = com.sun.star.awt.Key.A
    oControlView.keyPressed(oEvent) 
End Sub

然而,它似乎在我的系统上不工作(LibreOffice 6.4.3.2在Windows上)。我还发现 此职位但那段代码似乎对我也不起作用。

我搜索了一下 com.sun.star.awt.XToolkitRobot但它不在API文档中,也许是因为该功能没有得到完全支持。据推测,它可以从 com.sun.star.awt.Toolkit.

如需更多帮助,请在ask.libreoffice.org上发布问题。我建议解释一下你为什么要这样做,因为可能有一种不同的解决方案。Ratslinger有很多解决各种数据库问题的经验,他可能会指导你找到一个更简单的解决方案,不涉及这种事件黑客。

函数(即返回值的过程

是的,这就是函数。但是 "一个对象监听器事件 "意味着,我认为正确的是,我们谈论的是一个对象的方法。这就是LibreOffice事件监听器在Python或Java中的作用,尽管在Basic中,它们有点奇怪,用对象名称作为某种魔法来确定它们适用于什么。总之,这就跑题了,因为你的问题不是关于监听事件,而是关于触发事件。

EDIT:

下面的Python代码可以工作。我先前尝试的问题是 oEvent.KeyChar 需要设置,而在Basic中似乎不起作用。我无法想象为什么,除非我忽略了Basic代码中一些明显的错误。

def causeKeyPressedEventToBeFired(oEvent=None):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oController = oDoc.getCurrentController()
    oForm = oDoc.getDrawPage().getForms().getByName("Form")
    oTextBox = oForm.getByName("Text Box 1")
    oControlView = oController.getControl(oTextBox)
    oControlView.setFocus()
    oEvent = uno.createUnoStruct("com.sun.star.awt.KeyEvent")
    oEvent.Source = oControlView
    from com.sun.star.awt.Key import A
    oEvent.KeyCode = A
    oEvent.KeyChar = "a"       # works in Python but strangely not in Basic
    simulate_KeyPress(oEvent)

def simulate_KeyPress(oKeyEvent):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oWindow = oDoc.CurrentController.Frame.getContainerWindow()
    oKeyEvent.Source = oWindow      
    oToolkit = oWindow.getToolkit()
    oToolkit.keyPress(oKeyEvent)
    oToolkit.keyRelease(oKeyEvent)

EDIT 2:

最后,这里是工作的Basic代码。在先前的尝试中,类型是错误的。

Sub CauseKeyPressedEventToBeFired
    oDoc = ThisComponent
    oController = oDoc.getCurrentController()
    oForm = oDoc.getDrawpage().getForms().getByName("Form")
    oTextBox = oForm.getByName("Text Box 1")
    oControlView = oController.getControl(oTextBox)
    oControlView.setFocus()
    Dim oEvent As New com.sun.star.awt.KeyEvent
    oEvent.KeyCode = com.sun.star.awt.Key.A
    oEvent.KeyChar = CByte(97)
    simulate_KeyPress(oEvent)
End Sub

Sub simulate_KeyPress(oKeyEvent As com.sun.star.awt.KeyEvent)
    oWindow = ThisComponent.CurrentController.Frame.getContainerWindow()
    oKeyEvent.Source = oWindow      
    oToolkit = oWindow.getToolkit()
    oToolkit.keyPress(oKeyEvent)
    oToolkit.keyRelease(oKeyEvent)
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.