Key Events"
From Documentation
Line 10: | Line 10: | ||
Key event represents a key pressed by user. | Key event represents a key pressed by user. | ||
* '''onCtrlKey''' - This event is fired when user presses a key along with a ctrl key. Event listeners are provided with <code>org.zkoss.zk.ui.event.KeyEvent</code> in the event listener. | * '''onCtrlKey''' - This event is fired when user presses a key along with a ctrl key. Event listeners are provided with <code>org.zkoss.zk.ui.event.KeyEvent</code> in the event listener. | ||
− | ==Registering | + | ==Registering Key Events== |
Key event can be registered to ZK Spreadsheet by calling <javadoc method="addEventListener(java.lang.String, org.zkoss.zk.ui.event.EventListener)">org.zkoss.zk.ui.AbstractComponent</javadoc>. Here is an example shown using first way | Key event can be registered to ZK Spreadsheet by calling <javadoc method="addEventListener(java.lang.String, org.zkoss.zk.ui.event.EventListener)">org.zkoss.zk.ui.AbstractComponent</javadoc>. Here is an example shown using first way | ||
<source lang="java"> | <source lang="java"> |
Revision as of 01:11, 19 November 2010
Users can write key event listeners for key events such as ctrl+C, ctrl+V and so on.
Purpose
Implement onCtrlKey event listener.
Key event
Key event represents a key pressed by user.
- onCtrlKey - This event is fired when user presses a key along with a ctrl key. Event listeners are provided with
org.zkoss.zk.ui.event.KeyEvent
in the event listener.
Registering Key Events
Key event can be registered to ZK Spreadsheet by calling AbstractComponent.addEventListener(String, EventListener). Here is an example shown using first way
...
ss.addEventListener(org.zkoss.zk.ui.event.Events.ON_CTRL_KEY,
new EventListener() {
public void onEvent(Event event) throws Exception {
doCtrlKeyEvent((KeyEvent) event);
}
});
...
Note: Here instead of onCtrlKey a corresponding static constant declared in org.zkoss.zk.ui.event.Events
class is used.
Example
Here is a sample example ZUL file.
<?page title="ZSS" contentType="text/html;charset=UTF-8"?>
<zk>
<window title="ZSS Editing Events" border="normal" width="100%"
height="100%" apply="org.zkoss.zss.example.KeyEventComposer">
<hlayout>
<label value="Edit Box:"></label>
<textbox id="editBox"></textbox>
</hlayout>
<spreadsheet id="ss" width="800px" height="800px" maxrows="20"
maxcolumns="10" src="/test2/xls/edit.xlsx" ctrlKeys="^c^x^v">
</spreadsheet>
</window>
</zk>
In your composer.
public class KeyEventComposer extends GenericForwardComposer {
private transient Spreadsheet ss;
private transient Textbox editBox;
public void doAfterCompose(Component comp) throws Exception {
super.doAfterCompose(comp);
ss.addEventListener(org.zkoss.zk.ui.event.Events.ON_CTRL_KEY,
new EventListener() {
public void onEvent(Event event) throws Exception {
doCtrlKeyEvent((KeyEvent) event);
}
});
}
private void doCtrlKeyEvent(KeyEvent event) {
editBox.setValue("" + event.getKeyCode());
char c = (char) event.getKeyCode();
switch (c) {
case 'X':
// do cut operation
break;
case 'C':
// do copy operation
break;
case 'V':
// do paste operation
break;
default:
return;
}
}
You can retrieve key pressed from KeyEvent using getKeyCode()
.
Version History
Version | Date | Content |
---|---|---|
All source code listed in this book is at Github.