×

Discussion Board

Results 1 to 14 of 14
  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    42

    Unhappy Kindly explain this "Classic Keyboard Module" ?

    Hi everyone,
    I am new to python S60 programming, i found a class that handles keyboard activities in canvas. But i am not able to understand the details. Would someone plz care to explain them to me, otherwise the life would be tough for me.
    Here goes the 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(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

    I have used this class to handle key press event. But i want to use it for press-and-hold event. Any help in this regard is highly appreciated.

    Thanks

  2. #2
    Regular Contributor
    Join Date
    Dec 2007
    Location
    India
    Posts
    133

    Re: Kindly explain this "Classic Keyboard Module" ?

    @smilesnigam:

    There are Indentation errors in your code!
    Or could you post again properly.
    Kandyfloss

    V 7.0642.0
    18-10-06
    RH-51
    Nokia 7610

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    42

    Re: Kindly explain this "Classic Keyboard Module" ?

    yeah sure.
    this is the code which is provided by Python for S60 1.4.1.
    I want to implement press-and-hold event and its kinda very urgent. Plz help.


    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



    thnx

    PS: I didnt know how to indent.
    Last edited by smilesnigam; 2008-03-19 at 08:51.

  4. #4
    Nokia Developer Moderator
    Join Date
    Oct 2007
    Location
    Deva, Romania
    Posts
    3,471

    Re: Kindly explain this "Classic Keyboard Module" ?

    Quote Originally Posted by smilesnigam View Post
    yeah sure.
    this is the code which is provided by Python for S60 1.4.1.
    I want to implement press-and-hold event and its kinda very urgent. Plz help.


    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



    thnx

    PS: I didnt know how to indent.
    In order to post something with indentation (and highlight code at the same time) use the code tags : [ code ] and [ / code ], without the spaces
    Code:
    #program goes here
    
    def bla()
         bla bla bla   #This line is indented

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    42

    Re: Kindly explain this "Classic Keyboard Module" ?

    Thanx bogdan.galiceanu. Could u plz explain the code now. Or jus lemme know how to implement press-and-hold event.

    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:
                code1=event['scancode']
                if not self.is_down(code1):
                    self._downs[code1]=self._downs.get(code1,0)+1
                self._keyboard_state[code1]=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
    thnx a lot

  6. #6
    Regular Contributor
    Join Date
    Mar 2008
    Posts
    57

    Re: Kindly explain this "Classic Keyboard Module" ?

    Do You know how to handle a KeyDown event? If so, then I suppose You could use KeyDown to handle key-hold events, while leaving the KeyUp event for normal keypresses. A KeyDown event handler would have to wait 1 second or so and then check if there wasn't a KeyUp in between (if there wasn't, You got your event).

    I don't think this class is gonna help You. Write your own handler function. These 3 lines from the code should provide enough to get You started:

    Code:
        def handle_event(self,event):
            if event['type'] == appuifw.EEventKeyDown:
                code1=event['scancode']

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    42

    Re: Kindly explain this "Classic Keyboard Module" ?

    I have made following changes in the handler function in order to implement press and hold event. I have added the function press_hold(), but when i call press_hold(EScancode2) it gives a type error for line "self._event['type']==EEventKeyDown" ???

    Code:
    class Keyboard(object):
        def __init__(self,onevent=lambda:None):
            self._keyboard_state={}
            self._downs={}
            self._onevent=onevent
    	self._event={}
    		
        def press_hold(self,scancode):
    	print self._event    
    	if self._event['type'] == appuifw.EEventKeyDown:
    	    e32.ao_sleep(2)
    	    if self._event['type'] == appuifw.EEventKeyUp:
    		    if self.event['scancode'] == scancode:    
    	  		return 1
    	    return 0
    
        def handle_event(self,event):    
    	self._event=event
            if event['type'] == appuifw.EEventKeyDown:
                code1=event['scancode']
                if not self.is_down(code1):
                    self._downs[code1]=self._downs.get(code1,0)+1
                self._keyboard_state[code1]=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
    I have a feeling that this code is not correct but i tried starting with it. Any help is highly appreciated.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    42

    Re: Kindly explain this "Classic Keyboard Module" ?

    I tried to catch the number of repeating keys to implement press and hold event,but i think there is some error in the code.

    Code:
    class Keyboard(object):
        def __init__(self,onevent=lambda:None):
            self._keyboard_state={}
            self._downs={}
    	self._onevent=onevent	
    		
        def press_hold(self,scancode):
    	repeat=0
    	print repeat
    	while self.pressed(scancode):
    		if repeat==10:
    			 return True
    		print repeat 
    		repeat=repeat+1
    
        def handle_event(self,event):    
    	if event['type'] == appuifw.EEventKeyDown:
                code1=event['scancode']
                if not self.is_down(code1):
                    self._downs[code1]=self._downs.get(code1,0)+1
                self._keyboard_state[code1]=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
    Can someone look at it and tell me where am i making a mistake.

  9. #9
    Regular Contributor
    Join Date
    Mar 2008
    Posts
    57

    Re: Kindly explain this "Classic Keyboard Module" ?

    Something like this, rather... (untested)

    Code:
    class Keyboard(object):
        def __init__(self,onevent=lambda:None):
            self._keyboard_state={}
            self._downs={}
            self._onevent=onevent
    	self._event={}
            self._press_hold={}
            self._is_pressed={}
            self._sleeping = False
    	
        def press_hold(self,scancode):
            if self._press_hold.get(scancode,0):
                self._press_hold[scancode]=False
                return True
            return False
    
        def handle_event(self,event):    
    	self._event=event
            if event['type'] == appuifw.EEventKeyDown:
                code1=event['scancode']
                if not self.is_down(code1):
                    self._downs[code1]=self._downs.get(code1,0)+1
                self._keyboard_state[code1]=1
                if not self._sleeping:
                    self._sleeping = True
                    self._is_pressed[code1] = True
                    e32.ao_sleep(2)
                    if self._is_pressed[code1]: self._press_hold[code1] = True
                    self._sleeping = False
            elif event['type'] == appuifw.EEventKeyUp:
                self._keyboard_state[event['scancode']]=0
                self._is_pressed[code1] = False
            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
    Last edited by nokia_e70; 2008-03-21 at 16:53.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    42

    Re: Kindly explain this "Classic Keyboard Module" ?

    thanx a ton dude, it works !!!

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    42

    Re: Kindly explain this "Classic Keyboard Module" ?

    hey nokia e_70, first of all thanx for the code snippet which works pretty well , there is just one thing. It makes the whole program slow because everytime you punch a key it waits for a second or the time specified in e32_ao.sleep(time).

    Is there any alternative solution to this probably checking after a very small time that keyup has occurred or not or maybe using no of times that key has been repeated consecutively ??

  12. #12
    Regular Contributor
    Join Date
    Mar 2008
    Posts
    57

    Re: Kindly explain this "Classic Keyboard Module" ?

    Quote Originally Posted by smilesnigam View Post
    Is there any alternative solution
    First of all, the slowdown is due to the wrong way of programming. In the modern world, software is driven by events, so that You don't have to check in a loop whether something happened, instead You program a function that the OS fires when there's an event. The way You do it was valid on 8/16-bit computers and PC's of the DOS era. If You would do it the correct way, there would be no slowdown as Your code would be in event handlers, and they run just fine when a piece of code calls Ao_sleep.

    OK, so you might learn the second method someday, meanwhile I'm gonna tell You there is a way to do it without the slowdown. You have to cut the tail off the function (after Ao_sleep call) to another function, and add this new function's name as the second parameter to Ao_sleep.

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    42

    Re: Kindly explain this "Classic Keyboard Module" ?

    its very nice of you to suggest the alternate solution. I will try that. but i would like to know the event driven method that u talked abt in first para. is there any link which would help me ??

  14. #14
    Regular Contributor
    Join Date
    Mar 2008
    Posts
    57

    Re: Kindly explain this "Classic Keyboard Module" ?

    Quote Originally Posted by smilesnigam View Post
    i would like to know the event driven method that u talked abt in first para. is there any link which would help me ??
    Hmmm, I dunno... I did my lessons using delphi, and I'd recommend it, but it was, like, 10 years ago? So maybe there are other options that I don't know about (somehow I doubt it)... Some visual GUI toolkit like delphi would give You the idea of event programming in no time at all. I don't think there's such a program using python - what a pity...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •