Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Archived:How to use Keys in PySymbian

From Wiki
Jump to: navigation, search

Archived.pngAquivado: Este artigo foi arquivado, pois o conteúdo não é mais considerado relevante para se criar soluções comerciais atuais. Se você achar que este artigo ainda é importante, inclua o template {{ForArchiveReview|escreva a sua justificativa}}.

All PySymbian articles have been archived. PySymbian is no longer maintained by Nokia and is not guaranteed to work on more recent Symbian devices. It is not possible to submit apps to Nokia Store.

Article Metadata
Created: gaba88 (05 Aug 2008)
Last edited: hamishwillee (31 May 2013)



Keyboard keys are a very important and integral part of our S60 device. Hence keys plays a important role in our application development. This article will explain how to use effectively the keys in making a application in PySymbian.

Our Mobile phone keyboard is very fascinating in the sense that a single key in our keyboard provide different results depending on the modes which will be explained later. Suppose a key might produce a upper A or a lower a depending on the fact that any other keys are active or not or it can produce upper A or lower a depending on the fact that how many times the key was pressed so we have to get the record all these key presses.

The article explains how to bind an event to a particular key. For Example when the user presses the keyboard key there will be a note dialog that the key 2 was pressed.

Creating Canvas Object

When you want to register any key events except the left and right soft keys, you have to assign the event to the appuifw.Canvas object of the application body i.e.

canvas = appuifw.Canvas() = canvas

Binding a Keycode

  • The code below is an example of how to bind a key with a with a particular event. In the code below when one will press 2 then there will be a note dialog that key 2 was pressed. Similarly we can do that with many different keys and can bind them with different kind of events.
  • For using keys in our application we require a key_codes module. In this module every keycode is defined as a constant. The Various keycodes can be obtained from the PySymbian documentation.
  • One key can have more than one keycodes. This is because one single key can produce more than one characters.
  • The canvas.bind() function binds a key to a callback function like the statement canvas.bind(key_codes.EKey2, two) binds to the key two. This statement informs the canvas object that whenever a key is pressed please call the corresponding callback function attached to it.
# import modules
import appuifw, e32, key_codes
# define functions to show notifications
def up():
appuifw.note(u"Arrow up was pressed")
def two():
appuifw.note(u"Key 2 was pressed")
def quit():
# Create Canvas object
canvas = appuifw.Canvas() = canvas
# Bind keys
canvas.bind(key_codes.EKeyUpArrow, up)
canvas.bind(key_codes.EKey2, two)
# Exit handling = quit
app_lock = e32.Ao_lock()

Modes of Keyboard

Before designing a application with the help of Keyboard keys one needs to understand some basic concepts of modes of keys.

  • First one is Keycode this is an identifier for the keyboard key event. Some physical keys in the keyboard can have several keycodes. For example a key with a upper and lower case a. Keycodes are suited for applications where one wants to recognize a particular character. Keycodes start with the string Ekey.
  • Second one is the scancode which is a lower level identifier for the physical keyboard keys. If you want in your applications that a particular event must be related with a single key that scancode are the best options. Scancode names start with EScancode.
  • most important one is type which indicates wheather the key has been pressed down or is being released. We can use this information in our application if we want to know make a difference between single clicks and multiple clicks.

There is also extensions developed by y.a.k and cyke64 which deals with keypress simulation.


Key up.jpg Key 2.jpg

Related Links In the Wiki and Discussion Boards

This page was last modified on 31 May 2013, at 01:09.
36 page views in the last 30 days.