0

How to catch keyPressed while windows is active

asked 2019-11-05 12:47:48 +0800

onsir gravatar image onsir
132 2

updated 2019-11-06 11:11:44 +0800

cor3000 gravatar image cor3000
4887 2 7
ZK Team

hello how to catch keyPressed while windows is active ? i want worite that event in java class my code like this

file zul

<zk><window id="win1" width="850px"  border="normal" sizable="true" apply="mypackage.TestCatchKeyController">
         <grid vflex="1" zclass="MyGridRowHeight">
           <columns visible="false">
               <column width="90px"/><column/>
           </columns>
           <rows >
             <row>
                <div align="right">
                   <label id="lblCode" value="Code"/>
                </div>
                <textbox id="code" />
             </row>
             <row height="10px"> 
                <div align="right">
                   <label id="lblDes"  value="Description"/>
                </div>
                <textbox id="description" cols="60"/>
             </row>
           </rows>
        </grid>
    </window>
</zk>

class java

public class TestCatchKeyController extends GenericForwardComposer {

private Window win1;

@Override
public void doAfterCompose(Component comp) throws Exception {
    super.doAfterCompose(comp);

    //here i want write code to catch Key is pressed
    //etc
    if(key==F1){
         addItem();
     }
    if(key==F2){
         saveData();
     }

  }

}

thanks

delete flag offensive retag edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2019-11-06 10:09:04 +0800

onsir gravatar image onsir
132 2

i have try like this

win1.addEventListener(Events.ON_CREATE, new EventListener(){
            public void onEvent(KeyEvent e) throws Exception{
                     if(e.getKeyCode() == KeyEvent.VK_SPACE){
                           System.out.println("space is pressed");
                        }

                      if(e.getKeyCode() == KeyEvent.VK_ESCAPE){
                           System.out.println("escape is pressed");
                        }

            }

         @Override
         public void onEvent(Event t) throws Exception {
         }
     });

but still not succes

link publish delete flag offensive edit
0

answered 2019-11-06 12:33:41 +0800

cor3000 gravatar image cor3000
4887 2 7
ZK Team

updated 2019-11-06 12:37:53 +0800

Hi,

looks like to tried too hard ;). It's much simpler than you think: ON_CREATE is definitely not a key event -> Better use ON_CTRL_KEY

import static org.zkoss.zk.ui.event.KeyEvent.*;

public class TestCatchKeyController extends GenericForwardComposer{

  private Window win1;

  public void doAfterCompose(Component comp) throws Exception {
    super.doAfterCompose(comp);

    win1.setCtrlKeys("#f1#f2");

    win1.addEventListener(Events.ON_CTRL_KEY, (KeyEvent e) -> {
      int key = e.getKeyCode();
      if(key == F1) {
        Clients.log("F1 pressed");
      }
      if(key == F2) {
        Clients.log("F2 pressed");
      }
    });
  }

}

Here a runnable example: http://zkfiddle.org/sample/15nt2kf/1-ctrl-key-handling-from-java

TS;WRM (Too Short; Want to Read More?):

Docs on Keystroke Handling

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2019-11-05 12:47:48 +0800

Seen: 10 times

Last updated: Nov 06

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More