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 handle keypress using 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: cyke64 (24 Mar 2007)
Last edited: hamishwillee (31 May 2013)

Method 1

Nokia offers a keyboard class in the code examples but often it's too complex to handle. Here's a more convenient and easy way to handle keypress. Add this class Keyboard.

from key_codes import *
class Keyboard(object):
def __init__(self):
self.state = {} # is this key pressing ?
self.buffer= {} # is it waiting to be processed ?
def handle_event(self, event): # for event_callback
code = event['scancode']
if event['type'] == EEventKeyDown:
self.buffer[code]= 1 # put into queue
self.state[code] = 1
elif event['type'] == EEventKeyUp:
self.state[code] = 0
def pressing(self, code): # just check
return self.state.get(code,0)
def pressed(self, code): # check and process the event
if self.buffer.get(code,0):
self.buffer[code] = 0 # take out of queue
return 1
return 0

Now you can check the keyboard status with key.pressing and key.pressed:

from appuifw import *
key = Keyboard()
app.body = canvas = Canvas(event_callback=key.handle_event)
print key.state # just pressed up arrow {17: 0}
print key.buffer # {17: 1}
print key.pressing(EScancodeUpArrow) # it's not pressing = 0
print key.pressed(EScancodeUpArrow) # yes, it's pressed = 1
print key.pressed(EScancodeUpArrow) # no, you've just processed it = 0

Remarks : Don't put it in another module because it doesn't work ! Put it directly in your main code !

Method 2

An alternate, but very similar method is to define the class like this:

from key_codes import *
class Keyboard(object):
def __init__(self,onevent=lambda:None):
def handle_event(self,event):
if event['type']==appuifw.EEventKeyDown:
if not self.is_down(code):
elif event['type']==appuifw.EEventKeyUp:
def is_down(self,scancode):
return self._keyboard_state.get(scancode,0)
def pressed(self,scancode):
if self._downs.get(scancode,0):
return True
return False

and to create a loop that checks for keypresses:

import e32, appuifw
canvas=appuifw.Canvas(event_callback=keyboard.handle_event, redraw_callback=None)
if(keyboard.pressed(EScancodeRightArrow)): #If the specified key is pressed
print "Right arrow key was pressed"
running=0 #Break the loop

Related Links

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