| Reply | « Previous Thread | Next Thread » |
|
Im trying to create an app without a main loop and with a thread that checks for keypreses.
I've tried something like the code below but cant get it to work. Code:
import appuifw
import thread
import e32
from key_codes import *
app_lock=e32.Ao_lock()
running=1
#Some other stuff... (app body etc.)
def key_check():
while(running):
#check for key presses
thread.start_new_thread(key_check, ())
app_lock.wait() #Is the problem something to do with this?
|
|
Hi,
I just started to write pys60 code (as a hobby) and I am not an expert. I can still say something about threads. I did a game and wanted a music to play in background. Seems easy? First I must start a (sub)thread not using appuifw at all to play, (just experimenting to know this)! Then, when this subthread is running (playing), if I exit the main UI- thread the system crashes on the phone ( sis-file), boots phone! I must take care the music (non main)thread to stop first, not easy because feature pack 2 has not much tools for music. Some Python codes work differently running on Python interpreter on the phone or as sis- file, not making testing more easy! Threads are tricky, good luck! |
|
Hei!
You only miss some little details. Check this out ![]() Code:
import appuifw
import thread
import e32
from key_codes import *
def __exit__():
global running
if running: # you have to terminate you thread yourself before exiting
running = 0
app_lock.signal()
print "Goodbye crual thread!"
def key_check():
global running
print "Hello thread"
while(running):
#check for key presses
pass
if __name__ == "__main__":
app_lock=e32.Ao_lock()
appuifw.app.exit_key_handler = __exit__
appuifw.app.title = u'Hello thread'
running=1
thread.start_new_thread(key_check, ())
app_lock.wait() #Noup, only wait that you signal the lock here in __exit__() function
Devices: Nokia E61 3rd Edition - pys60 1.4.0 Tips and modules: http://www.lfdm.net/thesis |
|
Thanks so much to both of you! It works like a charm now.
Ill be posting the app here as soon as i get it ready... ![]() EDIT: Works only partly. Code:
if keyboard.pressed(EKey1):
print 'hello' #This works
Code:
if keyboard.pressed(EKey1):
send('hello') #This doesn't (my send function.) It just freezez python.
#The function itself works but when called from here it doesnt.
![]()
Last edited by RICH? : 2006-11-20 at 14:41.
|
|
rich, what is your send function?
|
|
It just sends data with bluetooth (RFCOMM).
Code:
def send(txt):
bt.sock.send(u""+txt)
But now I have another problem .I need to detect the release of a key (opposite of "pressed") with Jurgens example. I need to modify the keyboard class but I have no idea how I should do it. |
|
Hi Rich?
I put here the source (I renamed code to codee or it breaks the code tag) Code:
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(codee):
self._downs[codee]=self._downs.get(codee,0)+1
self._keyboard_state[codee]=1
elif event['type'] == appuifw.EEventKeyUp: <--- here is the "release" key
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
Code:
import appuifw
from key_codes import EScancodeUpArrow, EScancodeDownArrow
def event_callback(self, event):
if event['scancode'] == EScancodeUpArrow and event['type'] == appuifw.EEventKeyUp:
#do something
elif event['scancode'] == EScancodeDownArrow and event['type'] == appuifw.EEventKeyUp:
# do something else
![]() LFD Devices: Nokia E61 3rd Edition - pys60 1.4.0 Tips and modules: http://www.lfdm.net/thesis |
|
Hello!
I think got it working! This should work. Code:
#Needed to replace code with codee again...
class Keyboard(object):
def __init__(self,onevent=lambda:None):
self._keyboard_state={}
self._downs={}
self._ups={}
self._onevent=onevent
def handle_event(self,event):
code=event['scancode']
if event['type'] == appuifw.EEventKeyDown:
if not self.is_down(codee):
self._downs[codee]=self._downs.get(code,0)+1
self._keyboard_state[codee]=1
elif event['type'] == appuifw.EEventKeyUp:
self._ups[codee]=self._ups.get(code,0)+1
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
def released(self,scancode):
if self._ups.get(scancode,0):
self._ups[scancode]-=1
return True
return False
![]() EDIT: The class is now actually a bit buggy.
Last edited by RICH? : 2006-11-22 at 17:34.
|
| Reply | « Previous Thread | Next Thread » |
| Thread Tools | Search this Thread |
|---|---|
| Rate This Thread | |
| Thread | Thread Starter | Forum | Replies | Last Post |
|---|---|---|---|---|
| Compare() works in emulator but doesn't work in phone | yencruz | General Symbian C++ | 2 | 2004-04-27 11:36 |
| CommConnection does not work with Series 60 SDK, but works in JWT | jackiechan2001 | Mobile Java Tools & SDKs | 1 | 2004-02-04 10:07 |
| Does SDK v2.0 work on Series60 V6.1? | liying | Symbian Tools & SDKs | 3 | 2004-02-02 09:57 |
| does irda function work with the emulator ? | IGhost | General Symbian C++ | 0 | 2002-12-07 18:43 |
| 8310 Simulator doesn't work | DickJones | General Browsing | 1 | 2002-11-11 13:55 |