npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

svelte-keystroke

v1.0.3

Published

A key-stroke observer for Svelte, focussed on simplicity

Downloads

32

Readme

Issues Badge

svelte-keystroke

A key-stroke observer for Svelte, focussed on simplicity

<script>
  import Keystroke from "svelte-keystroke";
</script>

<Keystroke on:stroke={ ({ detail }) => console.log(detail) } />

Want to try it out real quick? Here's a basic demo, and here's a busier, comprehensive demo.

Summary

Keystroke offers four ways to respond to keyboard input, all through Svelte's on:<foo> events. The first three are pretty straightfoward.

  • on:stroke Reports single, printable characters
  • on:combo Only reports combinations involving modifier keys (Shift, Control, Alt, and Meta)
  • on:heldKeys For when you need raw details about which keys are currently depressed

The third option — let's call it "Binding Mode" lets you bind a function to whatever character you like.

  • on:<key> Runs the associated function every time the given key is pressed, regardless of its involvement as part of a stroke or combo

For examples, see on:Enter, on:Escape, on:q, & on:Q in the comprehensive demo.

Where's Meta / Command❓

svelte-keystroke pauses whenever the Meta key (⌘ on macOS) is depressed.

This is intentional. Why?

Well, it turns out that the key — reported as "Meta" by keydown and keyup events, exibits some funky behaviors, that make it rather torturous to work with. In this first release (and perhaps indefinitely), svelte-keystroke "handles" this weirdness by simply pausing operations until the ⌘/Meta key is lifted again.

How is svelte-keystroke different?

Built for svelte

As compared with most js keystroke-handling libraries, svelte-keystroke's most obvious feature is that it's a Svelte* component.

*If you're not using Svelte, stop reading now and go play for a few minutes!.

Comparison with svelte-keydown

svelte-keystroke owes its basic design to svelte-keydown, and there's significant overlap between the two.

Same

  • Arbitrary single-key binding via on:<key>…e.g., on:Enter, on:Tab

Similar

These events behave in similar ways, with the differences best observed by experimenting in the REPL:

  • keydown: on:key ≈ keystroke: on:stroke
  • keydown: on:combo ≈ keystroke; on:combo

Unique to svelte-keystroke

  • keystroke; on:heldKeys

Brief History

Why svelte-keystroke? > TL;DR — It does exactly what I need it to, no more, no less.

I was working on a Svelte app (link TBD) — a typing trainer, with the need to directly manage basic ASCII input (letters & basic punctuation), as well as keyboard combinations like Control+R. svelte-keydown seemed perfect at first, but there were subtle issues that made it unsuitable for my specific needs. (Don't ask what issues…I've since forgotten.) But it was close!

Luckily, svelte-keydown is simple enough that it was fairly trivial to create my own component and make it do exactly what I want. Now, as I prepare to for the first release of that typing app, I'm pausing to separate out this generic component. It works for me, but ymmv. Enjoy!

Other Key Handling Libraries

Svelte

JS


Title image — "Alone on the keyboard" — slightly modified from the original