Key Events"

From Documentation
m (correct highlight (via JWB))
 
(15 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 +
{{ZKSpreadsheetEssentialsPageHeader}}
 +
 +
{{Deprecated|url=http://books.zkoss.org/wiki/ZK_Spreadsheet_Essentials}}
  
{{ZKSpreadsheetEssentialsPageHeader}}
 
  
 
__TOC__
 
__TOC__
  
 
Users can write key event listeners for key events such as ctrl+C, ctrl+V and so on.
 
Users can write key event listeners for key events such as ctrl+C, ctrl+V and so on.
==Purpose==
+
===Purpose===
 
Implement onCtrlKey event listener.
 
Implement onCtrlKey event listener.
==Key event==
+
===Key event===
Key event represents a key pressed by user.
+
Key event represents a key pressed by the 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 the user presses a key along with a ctrl key. Event listeners are provided with <javadoc directory="zk" >org.zkoss.zk.ui.event.KeyEvent</javadoc> in the event listener.
==Registering Editing 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
+
===Registering Key Events===
 +
Key event can be registered to ZK Spreadsheet by calling <javadoc directory="zss" method="addEventListener(java.lang.String, org.zkoss.zk.ui.event.EventListener)">org.zkoss.zk.ui.AbstractComponent</javadoc> as shown below.
 
<source lang="java">
 
<source lang="java">
 
...
 
...
Line 24: Line 27:
 
'''Note''': Here instead of onCtrlKey a corresponding static constant declared in <code>org.zkoss.zk.ui.event.Events</code> class is used.
 
'''Note''': Here instead of onCtrlKey a corresponding static constant declared in <code>org.zkoss.zk.ui.event.Events</code> class is used.
  
==Example==
+
===ZUML===
Here is a sample example ZUL file.
+
Here is a sample example of ZUL file.
<source lang="xml" high="10">
+
<source lang="xml" highlight="12">
 
<?page title="ZSS" contentType="text/html;charset=UTF-8"?>
 
<?page title="ZSS" contentType="text/html;charset=UTF-8"?>
 
<zk>
 
<zk>
<window title="ZSS Editing Events" border="normal" width="100%"
+
<window title="ZSS Key Events" border="normal" width="100%"
height="100%" apply="org.zkoss.zss.example.KeyEventComposer">
+
height="100%"
 +
apply="org.zkoss.zssessentials.events.KeyEventsComposer">
 
<hlayout>
 
<hlayout>
<label value="Edit Box:"></label>
+
<label value="Key code:"></label>
<textbox id="editBox"></textbox>
+
<textbox id="editBox"></textbox>
 
</hlayout>
 
</hlayout>
 
<spreadsheet id="ss" width="800px" height="800px" maxrows="20"
 
<spreadsheet id="ss" width="800px" height="800px" maxrows="20"
maxcolumns="10" src="/test2/xls/edit.xlsx" ctrlKeys="^c^x^v">
+
maxcolumns="10" src="/WEB-INF/excel/events/events.xlsx"
 +
ctrlKeys="^c^x^v">
 
</spreadsheet>
 
</spreadsheet>
 
</window>
 
</window>
 
</zk>
 
</zk>
 
</source>
 
</source>
In your composer.
+
 
<source lang="java" high="8,18">
+
Above we register ctrl+c,ctrl+x and ctrl+v key events.
public class KeyEventComposer extends GenericForwardComposer {
+
 
 +
===Composer===
 +
Here is the composer code
 +
 
 +
<source lang="java" highlight="8,18">
 +
public class KeyEventsComposer extends GenericForwardComposer {
  
 
private transient Spreadsheet ss;
 
private transient Spreadsheet ss;
Line 75: Line 85:
 
}
 
}
 
}
 
}
 +
}
 
</source>
 
</source>
  
You can retrieve key pressed from KeyEvent using <code>getKeyCode()</code>.
+
You can retrieve key pressed from KeyEvent using <javadoc directory="zss" method="getKeyCode()">org.zkoss.zss.ui.event.KeyEvent</javadoc>.
 +
View the complete source code for composer [https://code.google.com/p/zkbooks/source/browse/trunk/zssessentials/examples/src/org/zkoss/zssessentials/events/KeyEventsComposer.java here]
  
 
=Version History=
 
=Version History=

Latest revision as of 12:57, 19 January 2022


Stop.png This article is out of date, please refer to http://books.zkoss.org/wiki/ZK_Spreadsheet_Essentials for more up to date information.


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 the user.

  • onCtrlKey - This event is fired when the user presses a key along with a ctrl key. Event listeners are provided with KeyEvent in the event listener.

Registering Key Events

Key event can be registered to ZK Spreadsheet by calling AbstractComponent.addEventListener(String, EventListener) as shown below.

...
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.

ZUML

Here is a sample example of ZUL file.

<?page title="ZSS" contentType="text/html;charset=UTF-8"?>
<zk>
	<window title="ZSS Key Events" border="normal" width="100%"
		height="100%"
		apply="org.zkoss.zssessentials.events.KeyEventsComposer">
		<hlayout>
			<label value="Key code:"></label>
			<textbox id="editBox"></textbox>
		</hlayout>
		<spreadsheet id="ss" width="800px" height="800px" maxrows="20"
			maxcolumns="10" src="/WEB-INF/excel/events/events.xlsx"
			ctrlKeys="^c^x^v">
		</spreadsheet>
	</window>
</zk>

Above we register ctrl+c,ctrl+x and ctrl+v key events.

Composer

Here is the composer code

public class KeyEventsComposer 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 KeyEvent.getKeyCode(). View the complete source code for composer here

Version History

Last Update : 2022/01/19


Version Date Content
     


All source code listed in this book is at Github.


Last Update : 2022/01/19

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.