Mac Delete Key Bug Demonstration

Platforms Affected

All MAC VM's (Netscape 3 & 4, IE 4 Tested) end up ignoring the forward delete key and inserting weird characters into the text field instead.

Details On The Mac Delete Key Bug

Most platforms have both a large backspace key on the upper right of the keyboard and a small "forward delete" key in a smaller set of keys next to the backspace key.

This "forward delete" key works as expected on Windows. But it is not interpreted properly on the Mac. Not only is it ignored in the sense that it APPEARS that nothing happens, but even worse, a bogus INVISIBLE character is inserted in its place.

The bug demonstration applet below allows you to type in characters and then press the button. Clicking the button will print the ASCII codes of all the characters in the text field below -- even the invisible ones.

If you are using a Mac, simply type a few characters and then anywhere in the text field press the forward delete key. On JDK 1.0.2 VMs, you will notice weird ASCII codes like [2244]. On JDK 1.1 VMs, you will notice ASCII codes like [127] appearing.

These are invisible in the actual text field, but when we print the ASCII codes, you can see they really do exist.

Mac Delete Key Bug Workaround

The workaround is relatively simple. In your event handler code for KEY PRESS, simply look for a key press key equal to 127. This is the forward delete key.

If you are using AWT 1.0.2 handleEvent code, to make this do nothing, simply return true in your applet before the text field has a chance to process the event itself.

Another, more sophisticated alternative is to simulate what SHOULD happen when the forward delete is pressed. This is done in the workaround applet demonstrated below.

The problem with the simulation is that at the end, a setText() has to be done in order to replace the text. This sets the cursor back to the end of the text. You could use setCaretPosition() but this is a JDK 1.1 ONLY solution.

In the workaround code, there are two cases that I look at:

Note: Since the key id is 127 for other platforms as well, I also check the OS to make sure it is a Mac before overriding the default behavior.

The following is a snippet of code that does the forward delete simulation:

    if (event.id == event.KEY_PRESS) {
      if (event.key == 127) {
      String osName = 
          System.getProperty("os.name").toLowerCase();
        if (event.target instanceof TextComponent && osName.indexOf("mac") != -1) {
          TextComponent tf = (TextComponent)event.target;
          int start = tf.getSelectionStart();
          int end = tf.getSelectionEnd();
          String text = tf.getText();
          if (end - start == 0) {
            text = text.substring(0,start) + text.substring(end + 1,text.length());
          } else {
            text = text.substring(0,start) + text.substring(end,text.length());
          }
          tf.setText(text);
        }
        return true;
      } else {
        return super.handleEvent(event);
      }
    }

References

Applet Demonstrating The Mac Delete Key Bug

View The Source To DeleteKeyBug.java

Applet With Delete Key Bug Workaround

View The Source To DeleteKeyFix.java

Gunther Birznieks <gunther@clark.net>