Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
svelte-keydown
Advanced tools
Changelog
0.7.0 - 2024-03-14
Breaking Changes
.svelte
codeFixes
exports
field to package.json
Readme
Utility to listen for keyboard events.
Utility component leveraging the svelte:body API to listen for keydown events.
Use Cases
Yarn
yarn add -D svelte-keydown
NPM
npm i -D svelte-keydown
pnpm
pnpm i -D svelte-keydown
<script>
import Keydown from "svelte-keydown";
let events = [];
</script>
<Keydown
on:Enter={() => {
events = [...events, "enter"];
}}
/>
Press "enter": {events.join(", ")}
Set pauseOnInput
to prevent the utility from capturing keydown events on input events.
<script>
import Keydown from "svelte-keydown";
let evt = [];
</script>
<Keydown
pauseOnInput
on:key={(e) => {
evt = [...evt, e.detail];
}}
/>
<input placeholder="Type here..." />
{evt.join("")}
This component forward the on:keyup
and on:keydown
events.
You can use on:keydown
to prevent the default behavior for certain keys.
In the following example, pressing "Space" should not cause the browser page to scroll.
<Keydown
on:keydown={(e) => {
if (e.key === " ") e.preventDefault();
}}
on:Space={(e) => {
console.log("key", "Space");
}}
/>
In this use case, keydown events are paused if the modal is not open.
<script>
import Keydown from "svelte-keydown";
let showModal = true;
</script>
<Keydown paused={!showModal} on:Escape={() => (showModal = false)} />
<button on:click={() => (showModal = !showModal)}>Toggled? {showModal}</button>
on:combo
Use the combo
dispatched event to listen for a combination of keys.
<script>
import Keydown from "svelte-keydown";
let combo = [];
</script>
<Keydown on:combo={(e) => (combo = [...combo, e.detail])} />
{combo.join(", ")}
separator
Use the separator
property to customize the separating key between multiple keys.
<script>
import Keydown from "svelte-keydown";
let combo = [];
</script>
<Keydown separator="+" on:combo={(e) => (combo = [...combo, e.detail])} />
{combo.join(", ")}
Prop | Type | Default value |
---|---|---|
paused | boolean | false |
pauseOnInput | boolean | false |
separator | string | - |
on:keyup
on:keydown
on:[Key]
Example:
<Keydown on:Enter />
<Keydown on:Escape />
<Keydown on:Space />
on:key
Alternative API to on:[Key]
.
Example:
<script>
import Keydown from "svelte-keydown";
let keys = [];
</script>
<Keydown on:key={({ detail }) => (keys = [...keys, detail])} />
<pre>{JSON.stringify(keys, null, 2)}</pre>
on:combo
Triggered when a sequence of keys are pressed and cleared when a keyup event is fired.
Examples:
<script>
import Keydown from "svelte-keydown";
let combos = [];
</script>
<Keydown on:combo={({ detail }) => (combos = [...combos, detail])} />
<pre>{JSON.stringify(combos, null, 2)}</pre>
Svelte version 3.31 or greater is required to use this component with TypeScript.
TypeScript definitions are located in the types folder.
FAQs
Utility to listen for keyboard events
The npm package svelte-keydown receives a total of 255 weekly downloads. As such, svelte-keydown popularity was classified as not popular.
We found that svelte-keydown demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.