Week 4: Keyboard Input

Like week 3, week 4 was always going to be a good one in terms of student reaction. This, of course, is one of the main reasons we use Processing. Unlike other languages (eg Java, or even Python/Ruby/etc), it's possible to very quickly progress to user input in Processing.

As a teacher it's gratifying to be able to reliably show a demo at the start of class and have students (literally!) gasping with astonishment and excitement. (At least, one hopes that gasping with boredom hasn't quietly become a thing without any of us noticing?).

That said, this week was also a tough one for many students, as they had to deal with the idea of "nesting" much more than before. Opening and closing brackets/braces isn't really a skill normally all that necessary, and when necessary it doesn't tend to be terribly difficult. In the code example below, which is perhaps on the high end in terms of number of nestings*, there are 6 separate pairs of curly brackets, all of which must be matched correctly in order for the code to function. Not all students managed to get to this level of complexity this week, but they all will!

void setup() {

void draw() {
  if (mousePressed) {
    if (mouseButton == LEFT) {
      if (keyPressed) {
        if (key == 'c') {
          ellipse(mouseX, mouseY, 50, 50);

You can see in the code above much of what we covered this week, most notably if (keyPressed) and if (key == 'c'). For those students who are interested in making games (which I think probably covers most if not all of them!) this was obviously exciting, as they got to see how easy it potentially is to take a user's input and do something with it in code.

In addition to key presses we also introduced students to variables this week. Variables allow a programmer to store something (a number, letter, word, etc) and refer back to it later. For example, we might set a circleSize variable at the start of our program, and make all subsequent circles with circleSize as a diameter. If we want to change the size of all our circles we then only have to change the number in one place. This also allows us to do interesting things like incrementing or decrementing the circle size based on mouse clicks or button presses. 

As you can see, and as the students are seeing, even in the relatively small number of classes we've had so far they have built up a formidable array of tools with which to tackle programming challenges. Next week will see revision of bracket discipline, as well as a some quickfire revision of the various functions we've used to date, and more opportunities to play with the user input features introduced these past two weeks.

More advanced students (mostly those who have previously attended a course with us) will also be moving on to bigger and more complex problems, requiring higher level thinking and more complex structures. Something for us all to get our teeth into then, and plenty to look forward to for all our students!


*For those programmers in the audience: don't worry, user-defined functions are coming up soon. We won't be encouraging quadruple nested if statements as a standard programming practice!