Wednesday, 16 January 2013

Binding a Keycode to a Callback Function


Binding a Keycode to a Callback Function

In this approach, we bind a callback function to respond to a specific key
event. The key is identified by a keycode. For each key on the keyboard
there exists at least one keycode that corresponds to a particular character
or action, as shown in Figure 5.3. Note that a single physical key may
produce many different characters; thus, many different keycodes may
correspond to a single key.
The keycodes are defined as constants in the module key codes.
You can find an overview of often used keycodes in Figure 5.3. To use the
keycodes, you need to import the key codes module at the beginning
of the script.
Example 28: Binding a keycode to a callback function
import appuifw, e32, key_codes
def up():
appuifw.note(u"Up arrow was pressed")
def two():
appuifw.note(u"Key 2 was pressed")
def quit():
app_lock.signal()
canvas = appuifw.Canvas()
appuifw.app.body = canvas
canvas.bind(key_codes.EKeyUpArrow, up)
canvas.bind(key_codes.EKey2, two)
appuifw.app.exit_key_handler = quit
app_lock = e32.Ao_lock()
app_lock.wait()



































The canvas.bind() function binds a keycode to a callback function.
For example, canvas.bind(key codes.EKeyUpArrow, up) binds
the callback function up() to the up-arrow key. This informs the canvas
object that whenever the key corresponding to the keycode EKeyUpArrow
(navigation key up) is pressed, the callback function up() should be
called. Similarly, we bind the keycode EKey2 to the function two().
In some cases, you may want to cancel the binding. This can be
accomplished by binding the value None to a keycode, in place of a
callback function.





No comments:

Post a Comment