You Are Here:

Community: Wiki

This page was last modified on 25 September 2009, at 19:52.

How to detect key presses in Python

From Forum Nokia Wiki

Reviewer Approved   
Reviewer Approved   


ID Creation date June 2, 2009
Platform S60 1st Edition, S60 2nd Edition, S60 3rd Edition, S60 5th Edition Tested on devices Nokia N95, Nokia E90, Nokia N97
Category Python Subcategory


Keywords (APIs, classes, methods, functions): keycapture


Overview

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

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 == eval("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.

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fCreatingE5fscreencastsE5fwithE5fmmakerX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtopicQUqfnTopicZpythonQ qfnZtopicQUqfnTopicZseriesE5f60Q qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qfnZuserE5ftagQSxpythonX qfnZuserE5ftagQSxs60X qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ