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"?>
 <window title="new page title" border="normal" id="inp"
  viewModel="@id('vm') @init('')" 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)">
    <listheader value="A"></listheader>
    <listheader value="B"></listheader>
    <listheader value="C"></listheader>

   <template name="model" var="mymodel">

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

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

And Java Code for this demo

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;

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


 public void ctrlKeyClick(@org.zkoss.bind.annotation.BindingParam("item") String ctekKeyCode ){
   int keyCode =Integer.parseInt(ctekKeyCode);
         String s = "";
             case 65: s = "Ctrl+A";break;
             case 119: s = "F8";break;
             case 83:s="Ctrl+S";break;
             case 68:s="Ctrl+D";break;
         }" is pressed", "CtrlKey",
                 Messagebox.OK, Messagebox.EXCLAMATION);
  System.out.println("I am clicked");
 public void addNewItem(){
  Data data = new 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) {
   this.a = a;
   this.b = b;
   this.c = c;



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

    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