Monday, February 4, 2013

ZK KeyStroke event with Windows Component in Java

If you do not like my previous post Zk KeyStroke Event where iused ZScript to show example but lots of developer do not like ZScript even i am not comfortable with Zscript even in my project i disable Zscript feature . Here i would like to show how can you capture which CTRL key pressed by user in ZUL page and capture that Key in your class file then you can follow the below demo example.... ZUL Code for this Demo
<?page title="new page title" contentType="text/html;charset=UTF-8"?>
<zk>
 <window title="new page title" border="normal" id="inp"
  viewModel="@id('vm') @init('com.team.MyListbox')" apply="org.zkoss.bind.BindComposer" ctrlKeys="^a^s^d#f8" onCtrlKey="@command('ctrlKeyClick',item=event.getKeyCode())">
  <button label="AddItem" onClick="@command('addNewItem')"  ></button>
   
  <listbox model="@bind(vm.dataList)">
   <listhead>
    <listheader value="A"></listheader>
    <listheader value="B"></listheader>
    <listheader value="C"></listheader>

   </listhead>
   <template name="model" var="mymodel">
    <listitem>
     <listcell>

      <textbox value="@bind(mymodel.a)" />
     </listcell>
     <listcell>
      <label value="@bind(mymodel.b)" />

     </listcell>
     <listcell>
      <label value="@bind(mymodel.c)" />

     </listcell>
    </listitem>
   </template>
  </listbox>
 </window>
</zk>
And Java Code for this demo
package com.team;

import java.util.ArrayList;
import java.util.List;

import org.zkoss.bind.annotation.AfterCompose;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zhtml.Messagebox;
import org.zkoss.zk.ui.Component;


public class MyListbox  {

 private List dataList;

 @AfterCompose
 public void afterCompose(@ContextParam(ContextType.VIEW) Component view) {
  try {
   dataList = new ArrayList();
   Data data;
   data = new Data("a1", "b1", "c1");
   dataList.add(data);
   data = new Data("a2", "b2", "c2");
   dataList.add(data);
   data = new Data("a3", "b3", "c3");
   dataList.add(data);
  } catch (Exception e) {

  }
 }

 @Command
 public void ctrlKeyClick(@org.zkoss.bind.annotation.BindingParam("item") String ctekKeyCode ){
   int keyCode =Integer.parseInt(ctekKeyCode);
         String s = "";
         switch(keyCode){
             case 65: s = "Ctrl+A";break;
             case 119: s = "F8";break;
             case 83:s="Ctrl+S";break;
             case 68:s="Ctrl+D";break;
         }
         Messagebox.show(s+" is pressed", "CtrlKey",
                 Messagebox.OK, Messagebox.EXCLAMATION);
  System.out.println("I am clicked");
 }
 
 @Command
 @NotifyChange("dataList")
 public void addNewItem(){
  Data data = new Data("", "", "");
  dataList.add(data);
 }
 public List getDataList() {
  return dataList;
 }

 public void setDataList(List dataList) {
  this.dataList = dataList;
 }

 public class Data {
  String a;
  String b;
  String c;
  public String getA() {
   return a;
  }
  public String getB() {
   return b;
  }
  public String getC() {
   return c;
  }
  public void setA(String a) {
   this.a = a;
  }
  public void setB(String b) {
   this.b = b;
  }
  public void setC(String c) {
   this.c = c;
  }
  public Data(String a, String b, String c) {
   super();
   this.a = a;
   this.b = b;
   this.c = c;
  }

 }
}

2 comments:

  1. This will work on ZK CE Version ? Because i am trying this, it is not working for me

    ReplyDelete
    Replies
    1. I am not sure but i do not think version will be effect the CTRL key feature but i will confirm and let you know asap

      Delete