×
Namespaces

Variants
Actions

Archived:How to detect key presses in PySymbian

From Nokia Developer 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.

This code snippet demonstrates how to detect key presses using the PySymbian keycapture module. Describing how to use the bind method of various UI controls is beyond the scope of this article; the PySymbian documentation or other material should be consulted for details on this matter.

Article Metadata
Tested with
Devices(s): Nokia N95, Nokia E90, Nokia N97
Compatibility
Platform(s): S60 1st Edition, S60 2nd Edition, S60 3rd Edition, S60 5th Edition
Article
Keywords: keycapture
Created: bogdan.galiceanu (02 Jun 2009)
Last edited: hamishwillee (31 May 2013)

Preconditions

Note: The keycapture module requres the SwEvent capability in S60 3rd Edition and later devices.

It should be noted that not all keys can be captured (for example, the multimedia menu key on the Nokia N95 and Nokia N95 8GB). Furthermore, although using this method to capture key events generally cancels the action that would normally be performed when that key would be pressed, some keys are impossible to override (for example, the menu key and hand-up key); in other words, it is impossible to prevent their standard action from taking place.

Source code

import keycapture, key_codes, globalui
 
 
#Define a method to be called when a key press event occurs
def cb_key_capture(key):
#TODO: Handle events here
#For example, tell the user what key was pressed
for i in dir(key_codes):
if key == getattr(key_codes, i):
globalui.global_note(unicode(i), 'info')
break
#Make the application close when the right softkey is pressed
if key == key_codes.EKeyRightSoftkey:
capturer.stop()
 
#Create a KeyCapturer object, specifying the method
capturer = keycapture.KeyCapturer(cb_key_capture)
 
#All keys should be captured
capturer.keys = keycapture.all_keys
#Alternatively, if only certain keys need to be handled, they can be specified as a tuple of keycodes
 
#Start detecting key presses
capturer.start()

Postconditions

Key presses are detected and the user is notified accordingly.

This page was last modified on 31 May 2013, at 04:03.
43 page views in the last 30 days.
×