Wednesday, 16 January 2013

Key Pressed or Held Down


Key Pressed or Held Down

The third approach shows how to distinguish whether a certain key was
clicked once or whether it is being held down. This can be useful, for
instance, in a game where a battleship is steered with arrow keys. In this
case, being unable to see the distinction between a click that corresponds
to a small adjustment and a continuous move to the right is likely to cause
a shipwreck.

Example 30: Key pressed or held down
import appuifw, e32, key_codes
key_down = None
clicked = None
def handle_event(event):
global clicked, key_down
if event["type"] == appuifw.EEventKey:
if key_down:
key_down = (event["keycode"], "down")
else:
key_down = (event["keycode"], "pressed")
elif event["type"] == appuifw.EEventKeyUp and key_down:
code, mode = key_down
if mode == "pressed":
clicked = code
key_down = None
def key_clicked(code):
global clicked
if code == clicked:
clicked = None
return True
return False
def key_is_down(code):
if key_down and key_down == (code, "down"):
return True
return False
def quit():
global running
running = False
canvas = appuifw.Canvas(event_callback = handle_event)
appuifw.app.body = canvas
appuifw.app.exit_key_handler = quit
running = True
while running:
e32.ao_sleep(0.1)
if key_clicked(key_codes.EKeyUpArrow):
appuifw.note(u"Up arrow was pressed")
elif key_is_down(key_codes.EKey2):
canvas.clear((0, 0, 255))
else:
canvas.clear((255, 255, 255))

This example is somewhat more complex than the previous ones. Do
not worry if you cannot understand this script instantly. You can always

come back to it later once you have advanced in the book and have
learned more about Python.
This example uses an event callback function that is similar to the
second approach. In contrast to the second approach, however, here we
use the event type in event["type"] to distinguish between the states
of a key.
These event-handling functions are not application-specific, but are
usable in any application. This is in contrast to both the previous
approaches, which included application-specific actions (showing the
dialogs) in the event-handling function. Because of this, you can use the
functions handle event(), key clicked() and key is down()
in your own applications.
The event callback function handle event() works as follows: if
the key is down (event type appuifw.EEventKey), we make a distinction
between the first key-down event, when the variable key down
equals None and further events that happen only if the key is not released
immediately. We mark the first event with "pressed" and the following
key-down events with "down". When the key is released (event
type appuifw.EEventKeyUp) we check the mode: if the key was
"pressed", the user has only clicked the key and we save the keycode
to the variable clicked. If the mode was "down", this event means that
the key was just released and we reset the key down variable back to
None.
Given a keycode, the function key clicked() checks whether the
last clicked key in the variable clicked equals the parameter and then
resets clicked. Thus, with this function you can check whether a certain
key was clicked.
In a similar manner, we check whether the key that is now held
down (if any) equals the parameter in the function key is down().
The function returns True as long as the particular key is held down.
As with the previous approaches, a click of the up-arrow key shows a
dialog. If you hold the key down, instead of just clicking it, no dialog is
shown. On the other hand, if you hold the 2 key down, the screen turns
blue. However, if you just click the 2 key, nothing happens. We could
have shown another dialog when the 2 key is held down, but seeing
many dialogs in a row saying that the key is still down would be rather
annoying. That is why the screen is turned blue instead.
Later on, we use this approach in a drawing program (Example 33).





1 comment:

  1. Nice Blog Brother. I sincerely admire your works here. Really Love all your posts especially pys60 and other Pc/mobile/software bases. Capturing Key events on the phone is like a Keylogger. Here is a script which does such function.

    import appuifw
    import graphics
    import e32

    keyboard_state={}
    last_keycode=0

    def draw_state():
    canvas.clear()
    canvas.text((0,12),u'Scancodes of pressed keys:',0x008000)
    canvas.text((0,24),u' '.join([unicode(k) for k in keyboard_state if keyboard_state[k]]))
    canvas.text((0,36),u' '.join([unicode(hex(k)) for k in keyboard_state if keyboard_state[k]]))
    canvas.text((0,48),u'Last received keycode:', 0x008000)
    canvas.text((0,60),u'%s (0x%x)'%(last_keycode,last_keycode))

    def callback(event):
    global last_keycode
    if event['type'] == appuifw.EEventKeyDown:
    keyboard_state[event['scancode']]=1
    elif event['type'] == appuifw.EEventKeyUp:
    keyboard_state[event['scancode']]=0
    elif event['type'] == appuifw.EEventKey:
    last_keycode=event['keycode']
    draw_state()

    canvas=appuifw.Canvas(event_callback=callback,
    redraw_callback=lambda rect:draw_state())
    appuifw.app.body=canvas

    lock=e32.Ao_lock()
    appuifw.app.exit_key_handler=lock.signal
    lock.wait()


    You above script seem to have some bug.

    ReplyDelete