When developing GUIs with Swing in Java I often swear about event handling, because it sometimes slightly differs from what the underlying platform would do. However when using SWT/eSWT and getting the native behaviors there are also gotchas waiting...
In our Eclipse Embedded Rich Client Platform (eRCP) based mobile application we want to allow keyboard as well as touch-screen access. In order to do so we attach a KeyListener to the buttons that handle the assigned shortcut bindings in addition to a SelectionListener to respond to clicks.
Another feature request called for the possibility to trigger a button by moving the focus to it with the keyboard and then hitting enter.
During development we usually work with the eSWT implementation for Win32. Because by default pressing enter does not do anything on a button, we add another KeyListener to respond to the return key. This usually works flawlessly. However on the mobile device we had trouble with button actions being triggered twice, even though the return key was only pressed once.
It took us some time to realize that the problem only occurs on the Windows CE platform, but never on our XP development machines.
This is were that “closer to the platform” part of SWT comes is: Hitting a button on a CE device seems to be the same as pressing return on it (as well as the space key, however on our hardware there is no way to do so except the onscreen keyboard). As soon as we removed the KeyListener that responded to the CR KeyEvent, the problems vanished on the mobile devices.
In retrospect it is hard to believe that we did not solve this much earlier. Seemingly regardless of whether you use a cross-platform library like Swing or the “closer to the real thing” SWT variant, you always seem to get caught by the same things – the one way round or the other…