Using the event callback() Function
In the second approach, we use a single callback function named keys()to handle all key events. Depending on the parameter event that is given
to the function, we take a corresponding action. We use the Canvas
object’s event callback parameter to direct all events to a single
function, keys().
Example 29: Using the event callback() function
import appuifw, key_codes, e32
def keys(event):
if event['keycode'] == key_codes.EKeyUpArrow:
appuifw.note(u"Up arrow was pressed")
elif event['keycode'] == key_codes.EKey2:
appuifw.note(u"Key 2 was pressed")
def quit():
app_lock.signal()
canvas = appuifw.Canvas(event_callback = keys)
appuifw.app.body = canvas
appuifw.app.exit_key_handler = quit
app_lock = e32.Ao_lock()
app_lock.wait()
When the user presses a key, the canvas receives a key event and
generates a dictionary object which is handed to the keys() function
through the parameter event. This object holds information about
the key that produced the event and whether the key was pressed or
released.
In essence, a dictionary object is a collection of key–value pairs.
This data structure might be familiar to you from other programming
languages, such as Java’s Hashtable or PHP’s or Perl’s hash arrays.
The dictionary object is introduced more thoroughly in Chapter 6 – here
we only peek inside the contents of one particular dictionary object,
event.
The event callback function receives a single parameter that is a
dictionary object, event. It includes the following information:
• keycode is an identifier for the keyboard key event. Some physical
keys on the keyboard may correspond to several keycodes, for
instance, to upper-case and lower-case A. Thus, if your application
must recognize a specific character, keycodes are a suitable choice.
Keycode names are defined in the key codes module and they start
with the letters EKey.
• scancode is a lower-level identifier of the physical keyboard key.
Scancode names start with the letters EScancode. If your application
must recognize events related to a physical key, scancodes are a
good choice. See Figure 5.3 to see some cases where the keycode
and scancode do not map to the same key. You can use scancodes
wherever keycodes are used, for instance, you could have used them
in Example 28.
• modifiers returns modifier keys that apply to this key event, such as
Ctrl or Alt. Modifiers are seldom used on a mobile phone keyboard.
• type indicates whether the key has just been pressed down
(appuifw.EEventKeyDown), is down (appuifw.EEventKey), or
has been released (appuifw.EventKeyUp). We can use this information
to distinguish between single clicks and continuous key presses
(see Example 30).
You can fetch the above values from the dictionary object as follows:
ev_keycode = event["keycode"]
ev_scancode = event["scancode"]
ev_modifiers = event["modifiers"]
ev_type = event["type"]
To detect which key is being pressed, we simply compare the keycode
or scancode value from the event dictionary with the code that we want
to detect. When the right softkey is pressed, the appuifw.app.exit
key handler() callback is always executed.
No comments:
Post a Comment