简体   繁体   English

如何在 ZK 7.X 中订阅 KeyEvent

[英]How to subscribe KeyEvent in ZK 7.X

We are creating a Window to add attachments.我们正在创建一个 Window 来添加附件。 The attachments can be added by either Drag N Drop or using Ctrl+V to copy from the clipboard.可以通过 Drag N Drop 或使用 Ctrl+V 从剪贴板复制来添加附件。 Able to implement Drag and Drop but unable to subscribe KeyEvent on the Window or Page.可以在Window或Page上实现Drag and Drop但无法订阅KeyEvent。

Tried the following codes but failed:尝试了以下代码但失败了:

  1. onPageAttached & onPageDetached onPageAttached 和 onPageDetached

     @Override public void onPageDetached(Page page) { super.onPageDetached(page); try { SessionManager.getSessionApplication().getKeylistener().removeEventListener(Events.ON_CTRL_KEY, this); } catch (Exception e) {} } @Override public void onPageAttached(Page newpage, Page oldpage) { super.onPageAttached(newpage, oldpage); if (newpage.= null) { SessionManager.getSessionApplication().getKeylistener().addEventListener(Events,ON_CTRL_KEY; this); } }
  2. addEventListener inside the custom Window class which implements EventListener.在实现 EventListener 的自定义 Window class 中添加EventListener。

    this.addEventListener(Events.ON_CTRL_KEY, this); this.addEventListener(Events.ON_CTRL_KEY, this);

All the code sample that I could see on the web is with ZUL file.我在 web 上看到的所有代码示例都带有 ZUL 文件。 But I need to achieve this using java code dynamically.但我需要动态地使用 java 代码来实现这一点。

I am able to subscribe to other events like click event, on close, etc.我可以订阅其他事件,如点击事件、关闭等。

You have a few things to configure to listen to keys:您需要配置一些东西来收听按键:

1: the target If you want to listen to keys in the whole page, the first thing you need is to declare this library property in your zk.xml: https://www.zkoss.org/wiki/ZK_Configuration_Reference/zk.xml/The_Library_Properties/org.zkoss.zk.ui.invokeFirstRootForAfterKeyDown.enabled with a value of "true" 1:目标如果你想听整个页面的按键,首先你需要在你的zk.xml中声明这个库属性: https://www.zkoss.org/wiki/ZK_Configuration_Reference/zk.xml /The_Library_Properties/org.zkoss.zk.ui.invokeFirstRootForAfterKeyDown.enabled值为“true”

This library property redirects every key listener event to the root component of your page.此库属性将每个关键侦听器事件重定向到页面的根组件。 If you don't set it, you will only be able to listen to keys on a component when it has focus.如果您不设置它,您将只能在组件具有焦点时收听组件上的键。

2: the key declaration You need to declare which keys you listen to with comp.setCtrlKeys(ctrlKeys); 2:按键声明需要用comp.setCtrlKeys(ctrlKeys); where ctrlKeys is a string containing the keys you want to listen to for example, ctrl+v would be comp.setCtrlKeys("^v");其中 ctrlKeys 是一个包含您要收听的键的字符串,例如, ctrl+v 将是comp.setCtrlKeys("^v");

3: the listener Your sample code already have a version of this. 3:监听器 你的示例代码已经有了这个版本。 You can use the add addEventLister(this) syntax, but it's over complicating the task.您可以使用 add addEventLister(this)语法,但这会使任务过于复杂。 simplest option is to inline the listener if you are not reusing it:如果您不重用它,最简单的选择是内联侦听器:

//comp here is the root div of my page
comp.setCtrlKeys("^v");
comp.addEventListener(Events.ON_CTRL_KEY, new EventListener<Event>() {
    @Override
    public void onEvent(Event event) throws Exception {
        Clients.log("do something at page level");
    }
});

See this fiddle ...!看到这个小提琴......! THIS FIDDLE CANNOT WORK FROM PAGE LEVEL !!!!这个小提琴不能从页面级别工作!!!! due to library property not being settable on zkfiddle.由于库属性无法在 zkfiddle 上设置。 Just run it locally with your zk.xml including the property above to have page-level listeners.只需使用包含上述属性的 zk.xml 在本地运行它即可拥有页面级侦听器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM