# What are the advantages of using predefined functions over keywords?

**URL:** https://discourse.processing.org/t/what-are-the-advantages-of-using-predefined-functions-over-keywords/17897
**Category:** Beginners
**Created:** [February 17, 2020, 11:36pm UTC](https://discourse.processing.org/t/what-are-the-advantages-of-using-predefined-functions-over-keywords/17897 "2020-02-17T23:36:30Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![anon94203042](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@anon94203042](https://discourse.processing.org/u/anon94203042)
#### Post date: [February 17, 2020, 11:36pm UTC](https://discourse.processing.org/t/what-are-the-advantages-of-using-predefined-functions-over-keywords/17897/1 "2020-02-17T23:36:30Z")

</div>

Are there any benefits to using `void keyPressed() {}` instead of `if (keyPressed)`? Or `void mousePressed() {}` instead of `if (mousePressed)`. Or something like that.

And in which situations would you use one over the other?

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [February 18, 2020, 12:07am UTC](https://discourse.processing.org/t/what-are-the-advantages-of-using-predefined-functions-over-keywords/17897/2 "2020-02-18T00:07:03Z")

</div>

The [Processing](https://processing.org/) website has references for these functions.

I suggest going through the references and examples and make a list based on what you observe.  
Feel free to modify the code as well to better understand how these functions work.

Insert _println()_ statements in the code (with meaningful data\messages) and monitor the console; this is a useful tool for working through code.

References:  
[https://processing.org/reference/println\_.html](https://processing.org/reference/println_.html)

’ 🙂 ’

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [February 18, 2020, 2:48am UTC](https://discourse.processing.org/t/what-are-the-advantages-of-using-predefined-functions-over-keywords/17897/3 "2020-02-18T02:48:53Z")

</div>

The fields _keyPressed_ & _mousePressed_ are for when we need some state to be active as long as a key or mouse is being held down.

The callback versions are for when we need something to trigger once for each press or click.

---

<div class="post-metadata">

### Author: ![anon94203042](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@anon94203042](https://discourse.processing.org/u/anon94203042)
#### Post date: [February 19, 2020, 2:13am UTC](https://discourse.processing.org/t/what-are-the-advantages-of-using-predefined-functions-over-keywords/17897/4 "2020-02-19T02:13:37Z")

</div>

@GoToLoop Oh, so keyPressed() makes it so that you can hold down a key, while keyPressed makes it so that you can just tap a key?

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [February 19, 2020, 2:25am UTC](https://discourse.processing.org/t/what-are-the-advantages-of-using-predefined-functions-over-keywords/17897/5 "2020-02-19T02:25:19Z")

</div>

It’s the opposite. 😳

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [February 19, 2020, 5:42am UTC](https://discourse.processing.org/t/what-are-the-advantages-of-using-predefined-functions-over-keywords/17897/6 "2020-02-19T05:42:18Z")

</div>

There are a number of differences. @GoToLoop already give you an accurate summary, but here, to unpack a bit:

One is that `keyPressed()` and `keyReleased()` are transition events, and `keyPressed` is a state.

One is that `if(keyPressed)` isn’t guaranteed to catch your key event. The key has to be in that state when draw executes. On the other hand, `keyPressed()` will get the event – regardless of the exact moment that it happens, lag, etc. – because it the event is queued and then processed on during the frame.

From this, another is that `keyPressed()` can handle multiple key inputs per frame. If your user is typing and you try to handle it in draw with `if(keyPressed) input.append(key)` then you are probably going to be dropping key presses.

If you mash a bunch of different keys down over time: asdfghlaseuhsvasdkvcxvcmfnlfahe then `keyPressed` will keep reporting each frame that _some_ key is down. You can even rapidly press the same key aaaaaaaaaaa and have keyPressed be true on multiple consecutive frames, even though the key was up inbetween. If you really want to check for continuous uninterrupted holding down of a key, use the event functions.

```auto
void setup(){
  frameRate(1);
}
void draw(){
  if(keyPressed){
    println("draw-press", key);
  }
  println("");
}
void keyPressed() {
  println("func-press", key);
}
void keyReleased() {
  println("func-release", key);
}

```

---

<div class="post-metadata">

### Author: ![anon94203042](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@anon94203042](https://discourse.processing.org/u/anon94203042)
#### Post date: [February 19, 2020, 10:56pm UTC](https://discourse.processing.org/t/what-are-the-advantages-of-using-predefined-functions-over-keywords/17897/7 "2020-02-19T22:56:46Z")

</div>

Got it, thanks guys!
