diff --git a/CHANGELOG.md b/CHANGELOG.md index 7dde5dd..9b4a3b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ +## [1.1.0] +Add `trusted` option + ## [1.0.2] -- Improve README +Improve README ## [1.0.1] -- Preserve the user-defined hotkey in the error message +Preserve the user-defined hotkey in the error message diff --git a/README.md b/README.md index 6a48ec1..b16c44d 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Hotkey The `@ramstack/hotkey` package is a very small and lightweight library for handling hotkeys. -The library weighs around 1.32KB and approximately 750 bytes when gzipped. +The library weighs around 1.3KB and approximately 750 bytes when gzipped. ## Installation @@ -77,7 +77,7 @@ an entire group of elements at once. * @param {string} hotkey - The combination of keys for the hotkey, e.g., "Ctrl+Alt+Delete". * @param {(e: KeyboardEvent) => void} handler - The function to be called when the hotkey is triggered. * @param {string} [eventName="keydown"] - The name of the event to listen for (default is "keydown"). - * @param {AddEventListenerOptions | boolean | undefined} [options] - Additional options for the event listener. + * @param {HotkeyEventListenerOptions | boolean | undefined} [options] - Additional options for the event listener. * * @returns {() => void} - A function to unregister the hotkey. */ @@ -86,7 +86,7 @@ function registerHotkey( hotkey: string, handler: (e: KeyboardEvent) => void, eventName?: string, - options?: AddEventListenerOptions | boolean | undefined): () => void; + options?: HotkeyEventListenerOptions | boolean | undefined): () => void; ``` **Parameters** @@ -170,6 +170,28 @@ registerHotkey(window, "Ctrl + Up", e => { #### options (optional) Additional options for the event listener. See [Options](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#options). +```ts +/** + * Extended options for hotkey event listeners. + */ +export interface HotkeyEventListenerOptions extends AddEventListenerOptions { + /** + * If specified, ensures that only trusted events are handled. + */ + trusted?: boolean; +} +``` +The `trusted` option ensures that only events marked as trusted are handled. +This can help prevent synthetic or untrusted events from triggering hotkeys. + +```js +registerHotkey(window, "Ctrl + S", e => { + e.preventDefault(); + console.log("Saving..."); +}, "keydown", { trusted: true }); +``` +In this example, the hotkey will only trigger for events that are marked as trusted, preventing manually dispatched or synthetic events from interfering. + #### Returns A function that, when called, unregisters the hotkey. @@ -181,13 +203,9 @@ const cleanup = registerHotkey(...); cleanup(); ``` -## Changelog -#### [1.0.2] -- Improve README - -#### [1.0.1] -- Preserve the user-defined hotkey in the error message - +## Contributions +Bug reports and contributions are welcome. ## License -This package is released under the **MIT License**. +This package is released as open source under the **MIT License**. +See the [LICENSE](https://github.com/rameel/ramstack.hotkey.js/blob/main/LICENSE) file for more details. diff --git a/package.json b/package.json index 7a8f206..006208c 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ }, "repository": { "type": "git", - "url": "git+https://github.com/rameel/ramstack.hotkey.git" + "url": "git+https://github.com/rameel/ramstack.hotkey.js.git" }, "keywords": [ "hotkey",