Game Development Reference
In-Depth Information
with Android 1.5 and 1.6, and using the multitouch code on devices with
Android 2.0 or newer. We'll return to this topic in the next chapter.
ï?®
There's no multitouch on the emulator. The API is there if we create an
emulator running Android version 2.0 or higher, but we only have a single
mouse. Even if we had two mice, it wouldn't make a difference.
ï?®
Touch two fingers down, lift the first one, and touch it down again. The
second finger will keep its pointer ID after the first finger is lifted. When the
first finger is touched down for the second time, it gets a new pointer ID,
which is usually 0 but can be any integer. Any new finger that touches the
screen will get a new pointer ID that could be anything that's not currently
used by another active touch. That's a rule to remember.
ï?®
If you try this on a Nexus One, a Droid, or a newer, low-budget smartphone,
you will notice some strange behavior when you cross two fingers on one
axis. This is due to the fact that the screens of those devices do not fully
support the tracking of individual fingers. It's a big problem, but we can work
around it somewhat by designing our UIs with some care. We'll have another
look at the issue in a later chapter. The phrase to keep in mind is: don ' t
cross the streams !
And that's how multitouch processing works on Android. It is a pain, but once you untangle all
the terminology and come to peace with the bit twiddling, you will feel much more comfortable
with the implementation and will be handling all those touch points like a pro.
Note We're sorry if this made your head explode. This section was rather heavy duty. Sadly, the
official documentation for the API is extremely lacking, and most people “learn� the API by simply
hacking away at it. We suggest you play around with the preceding code example until you fully
grasp what's going on within it.
Processing Key Events
After the insanity of the last section, you deserve something dead simple. Welcome to
processing key events.
To catch key events, we implement another listener interface, called OnKeyListener . It has a
single method, called onKey() , with the following signature:
public boolean onKey(View view, int keyCode, KeyEvent event)
The View specifies the view that received the key event, the keyCode argument is one of the
constants defined in the KeyEvent class, and the final argument is the key event itself, which has
some additional information.
What is a key code? Each key on the (onscreen) keyboard and each of the system keys has a
unique number assigned to it. These key codes are defined in the KeyEvent class as static public
final integers. One such key code is KeyCode.KEYCODE_A , which is the code for the A key. This has
nothing to do with the character that is generated in a text field when a key is pressed. It really
just identifies the key itself.
 
 
Search WWH ::




Custom Search