×
Namespaces

Variants
Actions

Archived:How to handle keypress using 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.

Article Metadata
Article
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):
self._keyboard_state={}
self._downs={}
self._onevent=onevent
def handle_event(self,event):
if event['type']==appuifw.EEventKeyDown:
code=event['scancode']
if not self.is_down(code):
self._downs[code]=self._downs.get(code,0)+1
self._keyboard_state[code]=1
elif event['type']==appuifw.EEventKeyUp:
self._keyboard_state[event['scancode']]=0
self._onevent()
def is_down(self,scancode):
return self._keyboard_state.get(scancode,0)
def pressed(self,scancode):
if self._downs.get(scancode,0):
self._downs[scancode]-=1
return True
return False
keyboard=Keyboard()

and to create a loop that checks for keypresses:

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

Related Links

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