Application Techniques



Processing Keystrokes

How to intercept and process user-entered keystrokes.

About this technique

Details

Category

Java Client Techniques> Keystrokes and drawing

Description

You'll learn about:

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.

Getting the pressed key and modifiers   Top of page

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(); 
     } 
  } 

Notes about the code

Sorting rows by column entry   Top of page

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.






Copyright © 2000, SilverStream Software, Inc. All rights reserved.