# Moving objects using mouse pressed and key pressed

**URL:** <https://discourse.processing.org/t/moving-objects-using-mouse-pressed-and-key-pressed/39035>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [October 1, 2022, 6:42pm UTC](https://discourse.processing.org/t/moving-objects-using-mouse-pressed-and-key-pressed/39035 "2022-10-01T18:42:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![turtle1](https://avatars.discourse-cdn.com/v4/letter/t/5fc32e/32.png) [@turtle1](https://discourse.processing.org/u/turtle1)\
**Post date:** [October 1, 2022, 6:42pm UTC](https://discourse.processing.org/t/moving-objects-using-mouse-pressed-and-key-pressed/39035/1 "2022-10-01T18:42:55Z")

</div>

Hi, I want to move a square down by 3 pixels using mouse pressed and and 3 pixels up by using key pressed. How would I do this? I’ve already coded the square in the middle of the 100 x 100 canvas.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 1, 2022, 7:43pm UTC](https://discourse.processing.org/t/moving-objects-using-mouse-pressed-and-key-pressed/39035/2 "2022-10-01T19:43:06Z")

</div>

Store the position in a x variable

Then look at functions mousePressed () and keyPressed ()

MUST be with the ()

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [October 1, 2022, 9:18pm UTC](https://discourse.processing.org/t/moving-objects-using-mouse-pressed-and-key-pressed/39035/3 "2022-10-01T21:18:17Z")

</div>

Hi

> **[Reference](https://processing.org/reference/mouseClicked_.html)**
>
> The mouseClicked() function is called after a mouse button has been pressed and then released. Mouse and keyboard events only work when a program has draw(). Without…

> **[Reference](https://processing.org/reference/keyPressed_.html)**
>
> The keyPressed() function is called once every time a key is pressed. The key that was pressed is stored in the key variable. For non-ASCII keys, use the keyCode var…
