# Can Racket send simulated keystrokes to the OS?

**URL:** <https://racket.discourse.group/t/can-racket-send-simulated-keystrokes-to-the-os/490>\
**Category:** Questions & Answers\
**Created:** [December 29, 2021, 5:42pm UTC](https://racket.discourse.group/t/can-racket-send-simulated-keystrokes-to-the-os/490 "2021-12-29T17:42:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![joeld](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/joeld/32/38_2.png) [@joeld](https://racket.discourse.group/u/joeld)\
**Post date:** [December 29, 2021, 5:42pm UTC](https://racket.discourse.group/t/can-racket-send-simulated-keystrokes-to-the-os/490/1 "2021-12-29T17:42:37Z")

</div>

Is it possible for a Racket program to send keystrokes to the current app in a desktop environment?

Wondering if I can substitute a Racket program/DSL for for [Autohotkey](https://www.autohotkey.com/), which is atrocious.

---

<div class="post-metadata">

**Author:** ![OldDutchCap](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/olddutchcap/32/2037_2.png) [@OldDutchCap](https://racket.discourse.group/u/OldDutchCap)\
**Post date:** [March 11, 2025, 6:39pm UTC](https://racket.discourse.group/t/can-racket-send-simulated-keystrokes-to-the-os/490/2 "2025-03-11T18:39:03Z")

</div>

I realize I may be resurrecting something that has no great answer but I've run into the exact same use case as @joeld. That is I want to send keystrokes to a windows app (not one I've created with Racket GUI) and Autohotkey isn't well suited to that purpose.

If I could somehow figure out a way to get the HWND of the app I want to send the keystrokes to is there a way (short of FFI to Windows OS) to send keystrokes?

It feels as if it's so close but nothing I'm finding in the docs is giving me an answer and my own trial fumbling isn't working as I would have hoped either. I mean subprocess will hand back a pid for me but that doesn't do the trick.

Any ideas or suggestions? Even though I would vastly prefer something in Racket I'm happy with anything anyone can suggest for this one.

---

<div class="post-metadata">

**Author:** ![LiberalArtist](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/liberalartist/32/151_2.png) [@LiberalArtist](https://racket.discourse.group/u/LiberalArtist)\
**Post date:** [March 11, 2025, 8:17pm UTC](https://racket.discourse.group/t/can-racket-send-simulated-keystrokes-to-the-os/490/3 "2025-03-11T20:17:49Z")

</div>

> [@OldDutchCap](#):
>
> is there a way (short of FFI to Windows OS)

Is there a reason you don't want to use the FFI?

I don't know how to do this on Windows with or without Racket, but I'd expect it would mean calling some kind of system API, and the FFI is the best way to do that in general.

---

<div class="post-metadata">

**Author:** ![OldDutchCap](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/olddutchcap/32/2037_2.png) [@OldDutchCap](https://racket.discourse.group/u/OldDutchCap)\
**Post date:** [March 11, 2025, 8:38pm UTC](https://racket.discourse.group/t/can-racket-send-simulated-keystrokes-to-the-os/490/4 "2025-03-11T20:38:10Z")

</div>

No good reason not to use the FFI. I wanted to avoid it to try to keep it platform agnostic but I think that's just not an option in this case.

---

<div class="post-metadata">

**Author:** ![LiberalArtist](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/liberalartist/32/151_2.png) [@LiberalArtist](https://racket.discourse.group/u/LiberalArtist)\
**Post date:** [March 12, 2025, 1:54am UTC](https://racket.discourse.group/t/can-racket-send-simulated-keystrokes-to-the-os/490/5 "2025-03-12T01:54:38Z")

</div>

Being cross-platform is great! Making a feature like this work across platforms typically means analyzing what the platform-specific features are available on all platforms of interest (e.g. [`uinput`](https://www.kernel.org/doc/html/v4.12/input/uinput.html) from the Linux kernel) and designing a cross-platform interface to all of the platform-specific backend implementations.

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [March 12, 2025, 9:15am UTC](https://racket.discourse.group/t/can-racket-send-simulated-keystrokes-to-the-os/490/6 "2025-03-12T09:15:46Z")

</div>

I am reminded by the following code:

> <https://gist.github.com/soegaard/4191317>

It contains code that uses the FFI to take screenshots on macOS.  
The part that finds the displays and active windows might be usable in a "send key event to window" program.
