与直觉相反,似乎将 KeyListener 添加到 CurrentController 并没有达到人们的想法。它似乎会启动文档以接收输入,您会认为它无论如何都会这样做。
将对键盘输入产生响应的函数是XKeyHandler.
它不是作为 EventListener 添加的,而是作为 KeyHandler 添加的。
oEventListener这个函数有它自己的怪癖,要取消它,你需要删除与用于启动它的实例完全相同的处理程序。oobasic 不是问题,实例可以存储为“全局”,我假设,因为它是不可或缺的,它以某种方式维护或存储。
python的问题是它不能以这种方式存储,除非有人读到这个,知道一种方法。它不会“腌制”,至于如何储存它,我最终陷入了腌制。我很可能错过了一些明显的东西。
我最终使用的解决方案是根据键盘输入使功能自动取消。
警告:这仅在 Linux 上进行了测试
以下可以通过从命令行启动 LibreOffice 进行测试,lowriter然后作为任何其他宏运行,使用 Shift+Alt+Ctrl+k 终止
#!/usr/bin/python
import unohelper
from com.sun.star.awt import XKeyHandler
from com.sun.star.awt import Key
from com.sun.star.awt.MessageBoxButtons import BUTTONS_OK
from com.sun.star.awt.MessageBoxType import INFOBOX
fs_fkeys={} # dictionary of keys to identify each key
for key in dir(Key):
fs_fkeys[getattr(Key, key)] = key
#Idiosyncrasies
# Shift_L, Ctrl_L, Alt_L are not reported as separate keys but are reported as modifiers
# 1,2 and 4 respectively. Shift_R and Ctrl_R are identical to their Left twins
# Alt_R (AltGr) is not reported and not a modifier.
# Super_R (Right Windows) is not reported but is a modifier, even though it doesn't modify any keys.
# It reports as modifier 8
# Super_L (Left Windows) is not reported and not a modifier.
# Caps_Lock and Num_Lock report as unidentified keys but not as modifiers.
# track key input with option of consuming the input (return True)
def fs_Tracker(*args):
doc = XSCRIPTCONTEXT.getDocument()
desktop = XSCRIPTCONTEXT.getDesktop()
global contr, oEventHandler
contr = desktop.getCurrentComponent().getCurrentController()
oEventHandler = KeyHandler(doc)
contr.addKeyHandler(oEventHandler)
mess = "Key tracker active\nTo deactivate close document or Shift+Alt+Ctrl K"
heading = "Key Tracker"
MessageBox(None, mess, heading, INFOBOX, BUTTONS_OK)
class KeyHandler( unohelper.Base, XKeyHandler ):
def __init__(self, parent):
self.parent = parent
return None
def Terminate ( self, event ):
mess = "Key tracker deactivated!"
heading = "Key Tracker"
MessageBox(None, mess, heading, INFOBOX, BUTTONS_OK)
contr.removeKeyHandler(oEventHandler)
def keyPressed( self, event ):
k = event.KeyCode
c = event.KeyChar.value
mods = event.Modifiers
# mods are additive
# 0 - None
# 1 - Shift
# 2 - Ctrl
# 4 - Alt
# 8 - Super_R
if c == "K" and mods == 7: #Shift+Ctrl+Alt+k
self.Terminate(None)
return True # Returning True consumes the key
# Thus assigning this macro to the same keyboard shortcut means that
# the macro is toggled On/Off by Shift+Alt+Ctrl+k
if k in fs_fkeys:
name = fs_fkeys[k]
else:
name = "Undefined"
print(name, k, c, mods)
return False
def keyReleased( self, event ):
return False
def MessageBox(ParentWindow, MsgText, MsgTitle, MsgType, MsgButtons):
ctx = XSCRIPTCONTEXT.getComponentContext()
sm = ctx.ServiceManager
si = sm.createInstanceWithContext("com.sun.star.awt.Toolkit", ctx)
mBox = si.createMessageBox(ParentWindow, MsgType, MsgButtons, MsgTitle, MsgText)
mBox.execute()
#List components that are accessible
g_exportedScripts = fs_Tracker,