DEV Community

Havey Bachand
Havey Bachand

Posted on

How keystroke-reactive animation works in a desktop pet

A desktop pet that "reacts to typing" sounds like a sprite problem: draw a look-up frame, play it on keydown. That only works in a normal app.

A desktop pet is not a normal app. If the design is working, its window almost never has focus. The user is typing into a browser or an editor. Your process sits in a transparent, click-through window on top of that work, and it must not steal focus just to notice the typing. The interesting problem is upstream of animation: get a trustworthy "the human is typing" signal, then turn it into motion that feels alive instead of twitchy.

I ran into this while building a desktop cat for Mac and Windows. What follows is the design, not a source dump — the app is closed source — but the constraints are the same for any companion that lives on the desktop all day.

You want presence, not keystrokes

The first rule is privacy, and it also simplifies the technical work. The pet does not need to know which keys were pressed, or to build a log of what was typed. It needs a presence bit: is the person actively using the keyboard right now?

That sounds like a small distinction. It is not. A hook that records key identities is a keylogger with a cat painted on it. A hook that only updates "last typing activity was N milliseconds ago" is the only version I am willing to ship. Mouse tracking follows the same rule. On macOS the app asks for permission so the cat can react to clicks and cursor position; the download page states those coordinates are used only for animation, and are never stored or sent anywhere. The app has no usage telemetry.

If you are building something in this family, make the data model match the promise. Keep a timestamp, or a short decaying "typing energy" value. Do not keep a buffer of keys.

The signal has to come from the OS, not from your window

Because the pet window is unfocused on purpose, toolkit events will not save you. You need some form of OS-level input observation that works while other applications are in front.

The exact API differs by platform, and so do the permission prompts. macOS is blunt: global mouse tracking shows a system dialog, and people are right to read it carefully. Windows is less theatrical, which is not an excuse to collect more than you need. In both cases I treat the hook as a narrow sensor. It answers "was there keyboard activity?" and "where is the cursor?" It does not get to start a side channel.

Two implementation details matter more than the hook itself:

  1. Coalesce, do not enqueue. A fast typist can produce more events than you want to wake up for. Fold them into a single "activity seen" update on an animation-friendly interval. I would rather be a few milliseconds late than wake the process on every repeat key.
  2. Hysteresis. One key should not yank the cat out of a deep sleep into a full typing cycle and back again. I use a short start delay so a single accidental press is ignored, a sustain window while keys keep arriving, and a longer fade after the last key so the cat can look, settle, and only later go back to roaming or napping.

The state machine is closer to a street cat than to a progress spinner:

idle / nap  --(keys for ~N ms)-->  notice
notice      --(keys continue)-->   watch / "you are busy"
watch       --(quiet for ~M ms)--> settle
settle      --(still quiet)-->     roam or nap
Enter fullscreen mode Exit fullscreen mode

The timings are a taste question. Too tight, and the cat looks nervous. Too loose, and it fails to notice you.

Animation is a reaction, not a transcript

Once you have a stable "typing" state, the sprite work is almost secondary — which is good, because you can keep the loop cheap.

I do not try to match animation to the rhythm of individual keys. That costs CPU and makes the character chatter when you hold backspace. The pet gets a small set of reactions: glance up, watch, maybe a short fidget, then settle. Narrow randomness helps, so the same burst of typing does not always play the same two frames.

Cursor position is a second, quieter input. If the pointer is far away and the user is typing, looking toward the focused window is enough. If the pointer passes close to the cat, that is more like being walked up to than being ignored. Mixing the two signals keeps the behavior from feeling like an on/off lamp.

Do not let the sensor keep the process hot

A pet that is charming for five minutes and warm on your lap after an hour has failed. The keystroke path has to respect the same budget as everything else.

When no keys have arrived and the cursor is still, there is nothing to do. Arm a "someone typed" wakeup, then go back to a slow idle. Do not run a 16 ms timer just in case. When the cat is asleep, a lower animation rate is fine.

This is also why I use Tauri as the shell rather than shipping a full browser runtime for a sprite. The runtime is only part of the cost. The rest is discipline: no redraws when nothing changed, no hidden windows, no extra work in the input path.

What I still do not know

I have not solved this, only shipped a version I can leave running. Multi-monitor setups, full-screen presentations, and "the cat noticed my password manager" unease are still live issues — the last one is why the presence-only model matters. If you have built a global-input animation loop that stays honest and cheap, I would like to hear what you settled on for the start and fade timings.

Top comments (0)