Excercise: Use of Keyboard keys

• Program an application that uses the keyboard keys to trigger pop-up notes telling which
key had been pressed.

 

1. Use the keys module that provide the class for handing key events.

from key_codes import *

2. Use canvas type from the appuifw module as appliation body.
Canvas is a UI control that provides a drawable area on the screen and support for handling raw key events.
Canvas is needed to scan/detect key pressing events (event_callback=keyboard.handle_event, ...).
Therefore put:

canvas=appuifw.Canvas(event_callback=keyboard.handle_event, redraw_callback=None)

appuifw.app.body=canvas

3. Create a loop that constanly scans a key that is pressed.

while running:

    if keyboard.pressed(EScancodeLeftArrow):
        appuifw.note(u"Arrow left", "info")

4. Exit the loop by pressing the exit key. Put therefore:

app.exit_key_handler=quit

def quit():
    global running
    running=0
    appuifw.app.set_exit()

 

Example file: ex_use_of_keys.py

Description: ex_use_of_keys_descr.py

IMORTANT: when running the script, you have to press the navigation key left, right, up or down to see the result.

 

NOTE: check the "info: use of keyboard keys" site of this tutorial for checking the Key codes of all the different keys of the keyboard available or the API_Reference_for_Python.pdf.

 

 

 

If you are bored:

use some of the previous spript and modify it so, that instead of using an "application menu" to trigger functions, it ues keyboard keys this time.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Previous | Next | Menu     Copyright (c) 2006 Jurgen Scheible