# keyPressed() only works sometimes

**URL:** <https://discourse.processing.org/t/keypressed-only-works-sometimes/22340>\
**Category:** Coding Questions\
**Created:** [July 2, 2020, 9:58pm UTC](https://discourse.processing.org/t/keypressed-only-works-sometimes/22340 "2020-07-02T21:58:21Z")\
**Posts on this page:** 1\
**Showing post:** 5

<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:** [April 23, 2026, 4:59pm UTC](https://discourse.processing.org/t/keypressed-only-works-sometimes/22340/5 "2026-04-23T16:59:13Z")

</div>

> [@beckerc](#):
>
> Is there a way to select the window with code so that a user doesn’t have to click the mouse on the window?

Yes!

An additional way:

> **[Robot (Java SE 17 & JDK 17)](https://docs.oracle.com/en/java/javase/17/docs/api/java.desktop/java/awt/Robot.html)**
>
> declaration: module: java.desktop, package: java.awt, class: Robot

Also see:  
_[Keyboard Entry Seems To Need Mouse Click To Work](https://discourse.processing.org/t/keyboard-entry-seems-to-need-mouse-click-to-work/48454)_

`:)`

---

_[View the full topic](https://discourse.processing.org/t/keypressed-only-works-sometimes/22340)._
