Categories: S60 | Python | How To | Code Examples | UI
This page was last modified 16:22, 24 March 2008.
How to handle keypress easily
From Forum Nokia Wiki
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 Discussions | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Get a screenshot | er_benji | Symbian Media (Graphics & Sounds) | 12 | 2007-02-28 13:44 |
| To Nokia please: which phone supports mutiple keypress, which not? | anrikun | Mobile Java General | 0 | 2003-10-27 23:38 |
| handle game event in j2me | wmchan78 | Mobile Java General | 2 | 2006-03-04 03:56 |
| urgent!!! need help | shadow-2005 | Symbian Networking & Messaging | 5 | 2007-02-28 10:11 |
| To handle volume keys in my application | ppreethijain | Mobile Java General | 3 | 2007-02-28 02:36 |
