-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hi ZK Community =)
I am relatively new to ZK and busy trying to figure out how some things work, using the developer's reference. Right now I am looking into the handling of key events. I just experimented a bit and couldn't seem to specify keystrokes for the space of a panel/window. For example, I have a panel with multiple labels and textboxes. But the keystrokes I defined on the panel are only detected when one of the textboxes has the focus. So my question is how I can achieve that the whole panel listens to the specified keystrokes, regardless where I am within the panel. Hope you can give me some hints =)
Cheers,
Gulsan
Edit: I am using ZK 6.0.1
<window title="Test Key Handling"> <window width="500" border="normal"> <div id="testdiv" apply="test.Test" ctrlKeys="^s" forward="onOK"> <panel> <panelchildren> <vlayout> <label value="textbox_1"></label> <textbox></textbox> <label value="textbox_2"></label> <textbox></textbox> </vlayout> </panelchildren> </panel> </div> </window> </window>
public class Test extends GenericForwardComposer { public void onOK() { Messagebox.show("Enter pressed!", "Key Events", Messagebox.OK, null); } public void onCtrlKey(Event key) { Messagebox.show("CTRLKEY CTRL+S!", "Key Events", Messagebox.OK, null); } }
Hi Matze, I did as you said but still the same as before. The events are only triggered when I set the cursor into one of the textboxes. The div or any other container doesn't listen to the events. I guess something else is needed here, just have to figure what that is ^^
Hi,
It is because a div can not receive the focus, and a component without focus can not receive the key event,
you may try to customize it, please refer to the sample:
<zk xmlns:w="client">
<div>Click in textbox_0 then press ctrl+s, you should see the 'message:' changed to 'message: textbox onCtrlKey at [current time]'</div>
<div>Click in the purple area then press ctrl+s, you should see the 'message:' changed to 'message: div onCtrlKey at [current time]'</div>
<window title="Test Key Handling">
<window width="500" border="normal">
<label id="msg" value="message: " />
<div id="testdiv" ctrlKeys="^s" style="background-color: #9999EE;">
<attribute w:name="bind_">
function (desktop, skipper, after) {
this.$bind_(desktop, skipper, after);// create an anchor that can receive the focus
var anchor = zk.ie || zk.gecko ? document.createElement("a") : document.createElement("button");// make it focusable
anchor.href = 'javascript:;';
// make it catchable
anchor.id = this.uuid + '-a';// make it out of the screen
anchor.style.position = 'absolute';
anchor.style.left = '-3000px';
anchor.style.top = '-3000px';// make it as a part of the div component
this.$n().appendChild(anchor);// anchor listen to event focus, blur and keydown
this.domListen_(anchor, 'onFocus', 'doFocus_')
.domListen_(anchor, 'onKeyDown', 'doKeyDown_')
.domListen_(anchor, 'onBlur', 'doBlur_');
}
</attribute>
<attribute w:name="doClick_">
function (evt) {
// focus the anchor if click on self
if (evt.domTarget == this.$n())
jq(this.$n('a')).focus();
this.$doClick_(evt);
}
</attribute>
<attribute w:name="doFocus_">
function (evt) {
this.$doFocus_(evt);
// mantain focus status
if (evt.domTarget == this.$n('a'))
this._focused = true;
}
</attribute>
<attribute w:name="doBlur_">
function (evt) {
this.$doBlur_(evt);
// mantain focus status
if (evt.domTarget == this.$n('a'))
this._focused = false;
}
</attribute>
<attribute w:name="isListen">
function (evt, opts) {
// do not receive onCtrlKey event if self not focused
// see Widget.js#afterKeyDown_
if (evt == 'onCtrlKey')
return this._focused
return this.$isListen(evt, opts);
}
</attribute>
<attribute name="onCtrlKey">
msg.setValue("message: div onCtrlKey at " + new Date());
</attribute><label value="textbox_0"></label>
<div></div>
<textbox ctrlKeys="^s">
<attribute name="onCtrlKey">
msg.setValue("message: textbox onCtrlKey at " + new Date());
</attribute>
</textbox>
<panel>
<panelchildren>
<vlayout>
<label value="textbox_1"></label>
<textbox></textbox>
<label value="textbox_2"></label>
<textbox></textbox>
</vlayout>
</panelchildren>
</panel>
</div>
</window>
</window>
</zk>
Regards,
Ben
Asked: 2012-04-19 09:06:23 +0800
Seen: 192 times
Last updated: Apr 26 '12