How to intercept and process user-entered keystrokes.
You can run this technique code from:
NOTE First make sure that database is running on your localhost SilverStream Server |
The example shows how to intercept the processing of a keystroke by getting its key code. It also shows how you can define a new behavior for processing the keystroke or let the keystroke continue to its normal processing.
The following code runs only if the view (vwEmployeeList) is the current focus, so make sure you click it first.
The code uses getKeycode()
to get the integer representing the key that the user hits. It uses getKeyModifiersText
to get the text representing the modifier (such as Shift or Ctrl) that the user hits. It then displays the pressed key and modifier (if the user presses one) in the form's Modifier and Key text fields by using the setText()
method. This code lets the Alt modifier continue to normal processing and it handles the Delete key differently--in addition to displaying the name of the Delete key, it deletes the selected row on the form.
private void handle_vwEmployeeList_keyPressed(KeyEvent keyEvent) { // Get the key that was hit and the modifier, if any int iKeyCode = keyEvent.getKeyCode(); String sKey = keyEvent.getKeyText(iKeyCode); String sModifier = keyEvent.getKeyModifiersText (keyEvent.getModifiers()); // Do not deal with the Alt key. As long as it is not Alt, show the key and modifier text in the text boxes if (!keyEvent.isAltDown()) { keyField.setText (sKey); fldModifier.setText(sModifier); } // If it's the delete key, delete the current row if (sKey.equals("Delete")) { AgiRowCursor rowCursor = vwEmployeeList.getSelectedRowCursor(); if (rowCursor == null) return; rowCursor.delete(); } }
getKeyCode()
.
getKeyText()
and providing the key code as its parameter.
getModifiers()
, and get the String that describes the modifier(s) by calling getKeyModifersText()
.
isAltDown()
.
setText()
from fldKey and fldModifier to display the names of the key and modifiers in the respective text fields.
getSelectedRowCursor()
method returns an agiRowCursor
object, which points to the selected row.
The frmKeyStrokes example also provides sample code for sorting rows. By clicking a column, the user sorts the rows in ascending order using that column's entry as the basis for sorting. When the user clicks the same column again, the rows are sorted in descending order. Refer to the example code in the Examples3_Java database for details.