Introduction to Event-Driven Programming

Have you ever wondered how video games respond when you press a button? Or how websites react when you click a link? This is all thanks to event-driven programming!

If you’ve used Scratch, you’ve already encountered event-driven programming with blocks like When key is pressed or When I receive. In this lesson, we’ll explore how this concept works in Python and JavaScript, two popular programming languages used for event-driven applications.


What is Event-Driven Programming?

Event-driven programming is a style of programming where the flow of the program is determined by events such as:

  • A user clicking a button.
  • A key being pressed.
  • A message being received from another part of the program.

Instead of executing code line by line, the program waits for specific events and responds when they occur.

How Scratch Uses Events

In Scratch, events trigger code blocks to run. For example:

  • When green flag clicked → Starts the program.
  • When space key pressed → Moves a character when a key is pressed.
  • When I receive message → Runs a block of code when another script sends a message.

Let’s see how we can apply these concepts to real-world programming languages.


Event-Driven Programming in JavaScript

JavaScript is widely used for web development, where almost everything is event-driven. Here’s an example of handling a button click event in JavaScript:

Example: Button Click Event

// Select the button element
let button = document.getElementById("myButton");

// Add an event listener for clicks
button.addEventListener("click", function () {
  alert("Button was clicked!");
});

What Happens Here?

  • The program waits for the button to be clicked.
  • When the event happens, the function runs and shows an alert.

This is just like Scratch’s When button clicked block!


Event-Driven Programming in Python

Python supports event-driven programming using libraries like tkinter (for GUI applications) or pygame (for games). Here’s an example of detecting a key press in Python:

Example: Detecting Key Press in Pygame

import pygame

pygame.init()
screen = pygame.display.set_mode((400, 300))

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                print("Space key was pressed!")

pygame.quit()

What Happens Here?

  • The program runs in a loop, waiting for events.
  • When the space key is pressed, it prints a message.
  • If the window is closed, the program stops.

This is similar to Scratch’s When space key pressed event block!


Why Use Event-Driven Programming?

Event-driven programming is used in:

  • Games (Reacting to player input)
  • Websites (Handling button clicks, form submissions)
  • Robotics (Reacting to sensor input)
  • Mobile apps (Responding to touch gestures)

By using events, programs only run code when necessary, making them more efficient and interactive.


Summary

  • Events trigger code execution instead of running top-to-bottom like traditional programs.
  • Scratch uses event-driven programming with “When clicked” and “When key pressed” blocks.
  • JavaScript uses addEventListener() to respond to user actions.
  • Python uses event loops (e.g., pygame.event.get()) to detect user interactions.

Next Steps

Try creating a small event-driven program! Start with a simple webpage button in JavaScript or a keyboard-controlled game in Python.

What events have you used in programming before? Share your experience in the comments!

Happy coding!