$$ \newcommand{\floor}[1]{\left\lfloor{#1}\right\rfloor} \newcommand{\ceil}[1]{\left\lceil{#1}\right\rceil} \renewcommand{\mod}{\,\mathrm{mod}\,} \renewcommand{\div}{\,\mathrm{div}\,} \newcommand{\metar}{\,\mathrm{m}} \newcommand{\cm}{\,\mathrm{cm}} \newcommand{\dm}{\,\mathrm{dm}} \newcommand{\litar}{\,\mathrm{l}} \newcommand{\km}{\,\mathrm{km}} \newcommand{\s}{\,\mathrm{s}} \newcommand{\h}{\,\mathrm{h}} \newcommand{\minut}{\,\mathrm{min}} \newcommand{\kmh}{\,\mathrm{\frac{km}{h}}} \newcommand{\ms}{\,\mathrm{\frac{m}{s}}} \newcommand{\mss}{\,\mathrm{\frac{m}{s^2}}} \newcommand{\mmin}{\,\mathrm{\frac{m}{min}}} \newcommand{\smin}{\,\mathrm{\frac{s}{min}}} $$

Prijavi problem

Obeleži sve kategorije koje odgovaraju problemu

Još detalja - opišite nam problem

Uspešno ste prijavili problem!
Status problema i sve dodatne informacije možete pratiti klikom na link.
Nažalost nismo trenutno u mogućnosti da obradimo vaš zahtev.
Molimo vas da pokušate kasnije.

Keyboard events

Of the keyboard-generated events, the most important is the key pressing event, so we will focus on that one the most. When pressing keys on the keyboard, in the event object that represents an event in our programs, the value of event.type will be pg.KEYDOWN.

When pressing a keyboard key is registered, we almost always want to know which key it is. This we can find out by testing the value of event.key. As we already mentioned in the lesson on reading the keyboard state, that there is a constant for each key corresponding to that key. Let us recall the names of these constants for some frequently tested keys (you can see a complete list of these constants here ):




left arrow


right arrow


up arrow


down arrow


space bar


a key


b key


c key

The value of the event.key field tells us which physical key is pressed, and this value is suitable for keys such as arrow keys, Ctrl, Shift, Alt, Home, End and the like. When it comes to text, it may be more convenient to use the value of the event.unicode field, which contains the character that was just typed (as a single character string).

The following example illustrates testing a keystroke, or a pg.KEYDOWN event.

Example - crossword

In this example, the user can use the arrow keys on the keyboard to move the frame and enter letters into boxes.

Note the handle_event function in which the event type check occurs, and then, if it is a key press, additional event information, stored in the event.key and event.unicode fields, is checked.

Additionally, you can try to put together some interesting crosswords.

Let’s compare this program to the “Navigation” program from the keyboard status reading lesson:

In the “Navigation” program

In the “Crossword” program

the yellow circle was moving by one pixel - it was gliding

the frame moves by one field - it jumps

we had no precise control of where the circle would stop

we can precisely control where the frame will stop

it didn’t matter where exactly the circle would stop

it matters where exactly the frame will stop

we were reading the status of the keybard keys (down or up)

we are using an event (pressing a key)

Task - navigation through fields

Write a program that works as shown in the example(“Play task” button).

The user can use the arrow keys on the keyboard to guide the character represented with a yellow circle. The character moves around the fields with white dots.

Task - making a maze

Write a program that makes a maze. The red frame should be controlled using the arrows, and pressing the space bar should change the color of a square (from black to white and vice versa).

When you complete the task, try the following:

  • click on the “Play task” button (this time the example program does more than what we asked of you)

  • make the maze the way you want

  • press S for start and watch

  • press P to turn pause mode on and off, that is to stop or resume searching

Bonus - a typing practice program

The program below is intended for typing practice. The program is long, but you should be able to understand a big part of it.

You might want (without obligation) to tailor the program to your needs, especially at the beginning. For example:

  • to slow down (or later accelerate) the falling of the letters

  • not to lose points when you make a mistake

  • when you master the letters that fall the first, to remove them from the list of letters to practice

  • to change the set of falling characters to numbers and operation signs only (if you want to practice typing only on the numeric keypad on the right side)

  • to have pressing the spacebar delete the lowest letter (with somewhat reducing the score)

or anything else you can think of.

When you have time, you are encouraged to also try to get as high a score as you can (without cheating).