# keyPressed()

## Example

<figure><img src="https://768248463-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fqbs7pgU3AVBY06Rl9M31%2Fuploads%2FAwDsJdsFpUljES0xEvJC%2FkeyPressed%20example.gif?alt=media&#x26;token=b43fe0a6-b272-4c31-ac94-93811beae859" alt=""><figcaption></figcaption></figure>

```python
def keyPressed():
  ...
  text(key,200,200)
```

## Syntax

```python
def keyPressed():
    # execute this code when any key is pressed
    ...
```

<figure><img src="https://768248463-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fqbs7pgU3AVBY06Rl9M31%2Fuploads%2FMUeAdcH2Lf4RVtGLPIyt%2Fkeypressed2.gif?alt=media&#x26;token=00b75d10-0143-49e3-88a0-06af2737b06a" alt=""><figcaption></figcaption></figure>

<details>

<summary>Full Code Example</summary>

```python
def setup():
  createCanvas(300, 300)
  frameRate(5)

  fill(255, 164, 28)
  stroke(44, 129, 237)
  strokeWeight(2)
  
w = 150
h = 150

def draw():
  background(255, 79, 132)
  
  ellipse(150, 150, w, h)

def keyPressed():
  global w, h
  
  if keyCode == 37:
    w -= 30
  elif keyCode == 38:
    h += 30
  elif keyCode == 39:
    w += 30
  elif keyCode == 40:
    h -= 30ython
```

</details>

## Description

Like [draw()](https://learnpython.strivemath.com/p5-python-web/reference/controlling-the-environment/draw) and [setup()](https://learnpython.strivemath.com/p5-python-web/reference/controlling-the-environment/setup) functions, this function is a built-in function that we override. That is, it **knows when** to run, but it **doesn't know what** do? That's why in the syntax, we just seem to define it, but not call it later.

So, this function runs **when a key is pressed (once it's hit, regardless if you hold it or release it later)**.

Built-in variables like [`keyCode`](https://learnpython.strivemath.com/p5-python-web/built-in-variables/keyboard#keycode) and [`key`](https://learnpython.strivemath.com/p5-python-web/built-in-variables/keyboard#key) can be used to know which key was pressed.

{% hint style="info" %}
Find these documents helpful? Let the people who made them help your child learn to code at [**Strivemath**](https://www.strivemath.com/)<mark style="color:blue;">**!**</mark>

We'd love to hear your Feedback/Comments [here](https://docs.google.com/forms/d/e/1FAIpQLSeqorBAGTya-YBRI-VFjJxtgQtCz3ucGDI96K96sNyuaGuvdw/viewform?usp=sf_link).
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://learnpython.strivemath.com/p5-python-web/reference/events/keyboard/keyboard.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
