Keystroke Handling"
Robertwenzel (talk | contribs) |
(→Allowed Control Keys: improve allow control key) |
||
Line 60: | Line 60: | ||
== Allowed Control Keys == | == Allowed Control Keys == | ||
− | {| border="1" | width="100%" | + | {| border="1" | width="100%" |
− | | <center>''' | + | ! <center>Key</center> |
− | | < | + | ! <center>Syntax</center> |
+ | ! <center>Description</center> | ||
+ | |||
+ | |- | ||
+ | | Control | ||
+ | | <center>^[?]</center> | ||
+ | | <tt>[?]</tt> can be '''a~z, 0~9, #[?]''', | ||
+ | e.g. <tt>^k</tt> represents <tt>Ctrl+k</tt> | ||
+ | |||
+ | |- | ||
+ | | Alt | ||
+ | | <center>@[?]</center> | ||
+ | | <tt>[?]</tt> can be '''a~z, 0~9, #[?]''', | ||
+ | e.g. <tt>@k</tt> represents <tt>Alt+k</tt> | ||
|- | |- | ||
− | | <center> | + | | Shift |
− | | | + | | <center>$[?]</center> |
+ | | <tt>[?]</tt> can be '''#[?]'''. Note: $a ~ $z are not supported. | ||
+ | e.g. <tt>$#down</tt> represents <tt>Shift+↓</tt> | ||
|- | |- | ||
− | | <center> | + | | Mac command(⌘) |
− | | | + | | <center>%[?]</center> |
− | + | | <pre>since 8.5</pre> | |
+ | <tt>[?]</tt> can be ''' a~z, 0~9, #[?]'''. | ||
+ | e.g. <tt>%k</tt> represents <tt>command+k</tt> | ||
|- | |- | ||
− | | <center> | + | | '''Navigation key''' |
− | | | + | | <center>#[?]</center> |
− | + | | the supported value of <tt>[?]</tt> are listed below: | |
|- | |- | ||
− | | | + | | Home |
− | |||
− | |||
− | |||
− | |||
| <center>#home</center> | | <center>#home</center> | ||
− | | | + | | |
+ | |- | ||
+ | | End | ||
| <center>#end</center> | | <center>#end</center> | ||
− | | | + | | |
+ | |- | ||
+ | | Insert | ||
| <center>#ins</center> | | <center>#ins</center> | ||
− | | | + | | |
− | |||
|- | |- | ||
+ | | Delete | ||
| <center>#del</center> | | <center>#del</center> | ||
− | | | + | | |
+ | |- | ||
+ | | ← | ||
| <center>#left</center> | | <center>#left</center> | ||
− | | | + | | |
+ | |- | ||
+ | | → | ||
| <center>#right</center> | | <center>#right</center> | ||
− | | | + | | |
− | |||
|- | |- | ||
+ | | ↑ | ||
| <center>#up</center> | | <center>#up</center> | ||
− | | | + | | |
+ | |- | ||
+ | | ↓ | ||
| <center>#down</center> | | <center>#down</center> | ||
− | | | + | | |
+ | |- | ||
+ | | PgUp | ||
| <center>#pgup</center> | | <center>#pgup</center> | ||
− | | | + | | |
− | |||
|- | |- | ||
+ | | PgDn | ||
| <center>#pgdn</center> | | <center>#pgdn</center> | ||
− | | | + | | |
+ | |- | ||
+ | | Backspace | ||
| <center>#bak</center> | | <center>#bak</center> | ||
− | | | + | | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
− | | | + | | function key (F1, F2,... F12) |
− | | < | + | | <center>#f1, #f2, ... #f12</center> |
− | + | | | |
|} | |} | ||
Revision as of 10:43, 22 February 2018
Keystroke handling is generic. Any component inherited from XulElement can handle the key event in the same way.
ENTER and ESC
To handle ENTER, you could listen to the onOK event (notice O and K are both in upper case). To handle ESC, you could listen to the onCancel event. For example,
<grid id="form" apply="foo.Login">
<rows>
<row>Username: <textbox id="username"/></row>
<row>Password: <textbox id="password" type="password"/></row>
<row><button label="Login" forward="form.onOK"/><button label="Reset" forward="form.onCancel"/></row>
</rows>
</grid>
Then, you could implement a composer as follows.
package foo;
import org.zkoss.zul.*;
public class Login extends org.zkoss.zk.ui.util.GenericForwardComposer {
Textbox username;
Textbox password;
public void onOK() {
//handle login
}
public void onCancel() {
username.setValue("");
password.setValue("");
}
}
Notice that the onOK and onCancel events are sent to the nearest ancestor of the component that has the focus. In other words, if you press ENTER in a textbox, then ZK will look up the textbox, its parent, its parent's parent and so on to see if any of them has been registered a listener for onOK. If found, the event is sent to it. If not found, nothing will happen.
Also notice that, if a button gains the focus, ENTER will be intercepted by the browser and interpreted as pressed. For example, if you move the focus to the Reset button and press ENTER, you will receive onCancel rather than onOK (since onClick will be fired and it is converted to onCancel because of the forward attribute specified).
Control Keys
To handle the control keys, you have to specify the keystrokes you want to handle with XulElement.setCtrlKeys(String). Then, if any child component gains the focus and the user presses a keystroke matches the combination, the onCtrlKey will be sent to the component with an instance of KeyEvent.
Like ENTER and ESC, you could specify the listener and the ctrlKeys property in one of the ancestors. ZK will search the component having the focus, its parent, its parent's parent and so on to find if any of them specifies the ctrlKeys property that matches the keystroke.
For example,
<vbox ctrlKeys="@c^a#f10^#f3" onCtrlKey="doSomething(event.getKeyCode())">
<textbox/>
<datebox/>
</vbox>
As shown, you could use KeyEvent.getKeyCode() to know which key was pressed.
Allowed Control Keys
Control | [?] can be a~z, 0~9, #[?],
e.g. ^k represents Ctrl+k | |
Alt | [?] can be a~z, 0~9, #[?],
e.g. @k represents Alt+k | |
Shift | [?] can be #[?]. Note: $a ~ $z are not supported.
e.g. $#down represents Shift+↓ | |
Mac command(⌘) | since 8.5 [?] can be a~z, 0~9, #[?]. e.g. %k represents command+k | |
Navigation key | the supported value of [?] are listed below: | |
Home | ||
End | ||
Insert | ||
Delete | ||
← | ||
→ | ||
↑ | ||
↓ | ||
PgUp | ||
PgDn | ||
Backspace | ||
function key (F1, F2,... F12) |
Document-level Keystrokes
[5.0.6]
When you set the library property org.zkoss.zk.ui.invokeFirstRootForAfterKeyDown.enabled to true. If there is no widget gaining a focus when an end user presses a keystroke, ZK can forward a key event to the first root component. For example, when visiting the following page, the div component will receive the onOK event.
<div onOK="doSomething(event)" ctrlKeys="^K" onCtrlKey="doSomething(event)" >
press enter key or ctrl+k.
<zscript><![CDATA[
public void doSomething(KeyEvent e){
Clients.showNotification(e.getKeyCode()+"");
}
]]></zscript>
</div>
In other words, doSomething()
will be called if the user presses ENTER, even though no widget ever gains the focus.
Nested Components
Keystrokes are propagated up from the widget gaining the focus to the first ancestor widget that handles the keystroke. For example,
<div onOK="doFirst()">
<textbox id="t1"/>
<div onOK="doSecond()">
<textbox id="t2"/>
</div>
</div>
Then, doSecond()
is called if t2 is the current focus, and doFirst()
is called if t1 has the focus.
Key handling and onChange event
When a onChange listener alone is registered on a component, onChange will be triggered by blur events exclusively.
However, some key events will cause a check for change value and will fire a change event if necessary.
These key events are: onOK onCancel onCtrlkeys. If a listener for any of these events is registered and triggers, an onChange event calculation will be triggered, and an onChange event will be fired if the value of the control have changed.
Version History
Version | Date | Content |
---|---|---|
5.0.6 | January 2011 | Document-level keystroke handling was introduced. |