[Tutor] program to count keys with timer display and clock display

Paul Sidorsky paulsid@shaw.ca
Mon, 22 Apr 2002 10:35:47 -0600

Lloyd Kvam wrote:

> My son (age 18) is looking to write a program that would count keypresses while
> displaying a 15 minute countdown timer and the current time.  He wants this to
> run under Linux and Windows.  He wants to be able to distinguish keys at a relatively
> low level e.g. RightShift-a, LeftShift-a.

> I (of course) recommended learning some Python to do this, but then started checking.
> The curses module would work for Linux, but not Windows.
> The WConio module would work for Windows, but is fairly different from curses.
> PyGame seems to support timers and keypresses, but looks to be more complex.

I think pygame would be a much better solution than writing two
programs.  It's not anywhere near as hard as it looks.  In fact it might
even be easier to use than the other two.  

Here's a very small, very simple, _working_ pygame program that shows
how to handle keypresses:


import pygame
from pygame.locals import *

screen = pygame.display.set_mode((300, 300), 0, 16)

while 1:
    event = pygame.event.poll()
    if event.type == QUIT:
    if event.type == KEYDOWN:
        if event.key == K_ESCAPE:
        if event.key == K_RETURN:
            screen.fill((255, 255, 255))
        if event.key == K_SPACE:
            screen.fill((0, 0, 0))


Not bad for under 20 lines, eh?  Another benefit is he could use
pygame's graphics stuff to dress up the countdown timer, like displaying
it in a nice big font or something.

Paul Sidorsky                                          Calgary, Canada
paulsid@shaw.ca                        http://members.shaw.ca/paulsid/