| 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 |
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.
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.
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()
Key presses are detected and the user is notified accordingly.
No related wiki articles found