python S60 Effort

Aug

24

Long keypress using keycapture

By raissa

While developing Fake Call, we had to find a way of detecting long keypress of an especific key. The solution we found was to keep calling a function that puts the current time in a list when the key is pressed and check everytime if the last value minus the first is equal to the amount of seconds desired. It goes something like this:

import time
import key_codes
import keycapture
import globalui
 
 
def check():
    global press
    global secs
    time_so_far = int(secs[-1] - secs[0])
    if (time_so_far == press):
        secs = []
        globalui.global_note(unicode("done"), 'info')
        #here you put what you want to do
        #when the key is pressed for the specified
        #number of seconds
    elif (time_so_far > press): #Reset the couting
        secs = []
 
 
def cb_key_capture(key):
    global secs
    if key == key_codes.EKeyStar:
        #EKeyStar is just an example! You can use other
        secs.append(time.time())
        check()
 
 
secs = [] #list of seconds
press = 3 #3 seconds long, for ex.
 
#Create a KeyCapturer object, specifying the method
capturer = keycapture.KeyCapturer(cb_key_capture)
 
#only the star key should be captured
capturer.keys = (keycapture.EKeyStar,)
 
#Start detecting key presses
capturer.start()

This is just a way of making it. If you know any other, share!

Leave a comment