Physical keys and virtual keyboard

The Virtual Keyboard API allows MIDlets to launch a virtual keyboard for Canvas and Canvas-based elements, such as GameCanvas and FullCanvas, and for active CustomItem elements in portrait mode. The Canvas.keyPressed and CustomItem.keyPressed methods handle key press events.

MIDlets can use the Virtual Keyboard API to:

  • Launch and control a virtual keyboard

  • Receive a notification whenever the visibility of a virtual keyboard changes, that is, when a virtual keyboard is either launched or dismissed

  • Retrieve information about the currently visible virtual keyboard

  • Suppress the sizeChanged event triggered whenever the visibility of a virtual keyboard changes

  • Hide and show the default open keypad command displayed in the native options menu for a Canvas or CustomItem

The Virtual Keyboard API defines the following categories of virtual keyboards:

  • CUSTOM_KEYBOARD is a virtual keyboard launched and controlled by the MIDlet through the Virtual Keyboard API. The virtual keyboard itself is platform-provided.

  • SYSTEM_KEYBOARD is a virtual keyboard launched and controlled by the platform. The virtual keyboard that is launched automatically for high-level UI elements such as TextEditor and TextField is a SYSTEM_KEYBOARD. The Virtual Keyboard API cannot control virtual keyboards of this category.

The Virtual Keyboard API does not provide any editor functionality. It merely launches and controls the virtual keyboard provided by the platform.

The Virtual Keyboard API is mainly intended for MIDlets that use low-level UI elements and rely on key input.

API contents

The Virtual Keyboard API consists of the following classes and interfaces (packaged as part of the Nokia UI API):

  • CustomKeyboardControl

    Use the CustomKeyboardControl interface to launch and control a virtual keyboard.

  • KeyboardVisibilityListener

    Use the KeyboardVisibilityListener interface to implement a listener for receiving a notification whenever the visibility of a virtual keyboard changes.

  • VirtualKeyboard

    Use the VirtualKeyboard class to create a CustomKeyboardControl, retrieve information about the currently visible virtual keyboard, hide and show the default open keypad command, or register a KeyboardVisibilityListener. You can also use this class to suppress the sizeChanged event triggered whenever the visibility of a virtual keyboard changes. Suppressing the event affects both MIDlet-controlled and platform-controlled virtual keyboards.

The Virtual Keyboard API is supported since Nokia UI API 1.6.

Device compatibility

The Virtual Keyboard API is supported on Series 40 full touch devices with Java Runtime 2.0.0 for Series 40 or newer.

Using the Virtual Keyboard API

The following code snippet shows how to launch, control, and hide a virtual keyboard.

try {

    // Get the CustomKeyboardControl singleton instance
    CustomKeyboardControl vkbControl = VirtualKeyboard.getCustomKeyboardControl();

    // Launch a virtual keyboard of the specified type and in the specified mode
    vkbControl.launch(VirtualKeyboard.VKB_TYPE_ITUT, VirtualKeyboard.VKB_MODE_DEFAULT);

    // ...

    // Change the mode to numeric
    vkbControl.setKeyboardMode(VirtualKeyboard.VKB_MODE_NUMERIC);

    // ...

    // Hide the virtual keyboard
    vkbControl.dismiss();

} catch (IllegalArgumentException iae) {
    // Handle the exception
} catch (IllegalStateException ise) {
    // Handle the exception
}

You can also launch a virtual keyboard by specifying only its type or by specifying neither its type nor mode. If you do not specify the type or mode, the default platform value is used. In addition to resetting the mode after the virtual keyboard has been launched, you can also reset the type by calling the setKeyboardType method. For a list of supported types and modes, see the VirtualKeyboard class reference.

The following code snippet shows how to implement a listener for receiving a notification whenever a virtual keyboard is either launched or dismissed.

class VirtualKeyboardListener implements KeyboardVisiblityListener {

    int myDisplayHeight;

    VirtualKeyboardListener() {

        // Set the current class as KeyboardVisiblityListener
        VirtualKeyboard.setVisibilityListener(this);

        // Specify myDisplayHeight
        // ...

    }

    // Called when a virtual keyboard is launched (shown)
    void showNotify(int keyboardCategory) {

        // Determine the height left for the MIDlet UI to use
        int visibleHeight;
        try {
            visibleHeight = myDisplayHeight - VirtualKeyboard.getHeight();
        } catch (IllegalStateException ise) {
            // Handle the exception
        }

        // Refresh the screen
        // ...

    }

    // Called when a virtual keyboard is dismissed (hidden)
    void hideNotify(int keyboardCategory) {

        // The MIDlet UI can use the full height available
        int visibleHeight = myDisplayHeight;

        // Refresh the screen
        // ...

    }

}

For more information about the fields and methods provided by the Virtual Keyboard API, see the individual class and interface references.

More information

For example MIDlets that show you how to use the Virtual Keyboard API, see:

Key events in Canvas and CustomItem

The following physical keys can be mapped to MIDP-defined logical key events. Keys marked with * are only visible on FullCanvas and in Canvas in full screen mode when there are no CommandListener or Commands set.

Table: Additional physical keys

Physical Key

Key code decimal value

Up

-1

Down

-2

Left

-3

Right

-4

Clear key

-8

Edit key

-50

End key

-11

Send key

-10

Applications key

-12

Voice key

-13

Play/pause

-20

Previous

-21

Next

-22

Stop

-23

Softkey1 (left softkey

-6

Softkey2 (right softkey)

-7

Zoom in

-63582

Zoom out

-63583

Some devices support 9-way navigation, which means that in addition to the four main directional keys, the device can also detect key presses to four diagonal directions. If the user presses into a diagonal direction on such a device, the Canvas or CustomItem receives two orthogonal key events from the adjacent main directions according to the following table:

Table: Diagonal key events

Pressed diagonal key

Translated key events

Key code decimal values

Left upwards direction

Up and Left

-1 and -3

Right upwards direction

Up and Right

-1 and -4

Left downwards direction

Down and Right

-2 and -4

Right downwards direction

Down and Left

-2 and -3

The reason for separating the diagonal key presses is to maintain backwards compatibility for legacy MIDlets. Java Runtime translates single diagonal key press events to two orthogonal key press events.

Note: The End key event is used by the system to exit the MIDlet and to go to the device’s Idle state (or to terminate active calls). A MIDlet can however explicitly define if it is closed or continues to run in the background.

Note: Some keys defined in above table may not be available with a full keyboard. For example, the Clear key is not available, because the full keyboard contains the backspace key whose Unicode value is 8. Note also that pressing the Shift key alone in a full keyboard causes a key event with the key code 50 (an Edit key event). If the Shift key is pressed together with another key, it causes only the combined key event, not the Edit key event with the key code 50.

A full keyboard contains also other standard physical keys, including letters and characters such as @ and +. Their key codes are equal to the Unicode value for the corresponding character. For example, the letter key A has the key code 97 and its key name string is a. A full keyboard provides also combined keys, for example Shift+A has the key code 65 and its key name string is A.

Table: Additional physical keys available only in full keyboard (QWERTY)

Physical key

Key code

Space

32

Backspace

8

Escape

27

Tab

9

Enter

10

Delete

127

Modifier keys

Every modifier key on keyboard (Shift, Fn, Ctrl) delivers a key event with key code -50 to Canvas and CustomItem key press methods. Key events from special keys such as Chr, zoom keys, and others are also delivered to MIDlet but they do not function in any special way. Thus, the MIDlet can do what they want for these key events.

Modifier keys (Shift, Fn, Ctrl) pressed along with other keys send combined key events (for example, pressing Fn+A sends @).

Modifier keys act as 'sticky' keys, modifying the immediately following character event. In other words, the end user does not have to keep the Fn key pressed down while entering the special letter. Pressing the key once is enough.

Pressing a modifier key twice locks the modifier key. Every following key press delivers modified key codes. Every locked modifier key except Shift can be unlocked by pressing it again. Pressing a locked Shift key unlocks it for one following key press. The Shift modifier is unlocked completely by pressing it twice.

Several system properties are closely related to using modifier keys. For more information, see section System properties.

Media Keys

Nokia devices are equipped with media keys from Series 40 5th Edition onwards. Media keys are play/pause, stop, previous/rewind, and next/fast forward.

Media key events are not created by default. In order for a MIDlet to be notified by media key events, the JAD attributeNokia-UI-Enhancement must have value MusicKeysSupported set, as described in table Nokia proprietary JAD attributes.

Media key events are delivered to all Canvas and CustomItem objects even when they are not the current Displayable or the focused item on a Form. Thus media applications can be managed without setting the MIDlet on the foreground. This can be especially useful when the MIDlet is managed via an accessory and the device is not at hand.

Media key presses are received with the keyPressed (int keyCode) method. If the MIDlet contains more than one active Canvas or CustomItem objects, you need to carefully manage what Canvas or CustomItem object(s) (if any) handle media key events, as all these objects will get a media key event when it occurs.

If the MIDlet contains more than one active Canvas or CustomItem objects, it is MIDlet developer’s responsibility to manage what Canvas or CustomItem object(s) (if any) handles media key events, because all these objects will get a media key event when it occurs.

For key codes of media keys, see table Additional physical keys above.

Table: Key events of media keys

Media key

Key event generated by short key press

Key event generated by long key press

Play/Pause

Stop

keyPressed

keyPressed

Previous/Rewind

Next/Fast Forward

keyPressed

keyPressed (on initial press)

keyRepeated (while holding down the key)

keyReleased (when releasing the key)