jamiebuilds/tinykeys là dự án TypeScript với 4.1k sao. A tiny (~650 B) & modern library for keybindings.
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
A tiny (~1KB) & modern library for keybindings.
See Demo
Install
npm install --save tinykeys
Or for a CDN version, you can use it on unpkg.com
Usage
import { tinykeys } from "tinykeys" // Or `window.tinykeys` using the CDN version
tinykeys(window, {
"Shift+D": () => {
alert("The 'Shift' and 'd' keys were pressed at the same time")
},
"y e e t": () => {
alert("The keys 'y', 'e', 'e', and 't' were pressed in order")
},
"$mod+([0-9])": event => {
event.preventDefault()
alert(`Either 'Control+${event.key}' or 'Meta+${event.key}' were pressed`)
},
})
Alternatively, if you want to only create the keybinding handler, and register
it as an event listener yourself:
import { createKeybindingsHandler } from "tinykeys"
let handler = createKeybindingsHandler({
"Shift+D": () => {
alert("The 'Shift' and 'd' keys were pressed at the same time")
},
"y e e t": () => {
alert("The keys 'y', 'e', 'e', and 't' were pressed in order")
},
"$mod+KeyD": event => {
event.preventDefault()
alert("Either 'Control+d' or 'Meta+d' were pressed")
},
})
window.addEventListener("keydown", handler)
React Hooks Example
If you're using tinykeys within a component, you should also make use of the
returned unsubscribe() function.
Keybindings will be matched against
KeyboardEvent.key
andKeyboardEvent.code
which may have some names you don't expect.
Windows
macOS
key
code
N/A
Command / ⌘
Meta
MetaLeft / MetaRight
Alt
Option / ⌥
Alt
AltLeft / AltRight
Control
Control / ^
Control
ControlLeft / ControlRight
Shift
Shift
Shift
ShiftLeft / ShiftRight
Space
Space
N/A
Space
Enter
Return
Enter
Enter
Esc
Esc
Escape
Escape
1, 2, etc
1, 2, etc
1, 2, etc
Digit1, Digit2, etc
a, b, etc
a, b, etc
a, b, etc
KeyA, KeyB, etc
-
-
-
Minus
=
=
=
Equal
+
+
+
Equal*
Something missing? Check out the key logger on the
demo website
* Some keys will have the same code as others because they appear on the
same key on the keyboard. Keep in mind how this is affected by international
keyboards which may have different layouts.
Key aliases
In some instances, tinykeys will alias keys depending on the platform to
simplify cross-platform keybindings on international keyboards.
AltGraph (modifier)
On Windows, on many non-US standard keyboard layouts, there is a key named
Alt Gr or AltGraph in the browser, in some browsers, pressing Control+Alt
will report AltGraph as being pressed instead.
Similarly on macOS, the Alt (Option) key will sometimes be reported as the
AltGraph key.
Note: The purpose of the Alt Gr key is to type "Alternate Graphics" so you
will often want to use the event.code (KeyS) for letters instead of
event.key (S)
tinykeys(window, {
"Control+Alt+KeyS": event => {
// macOS: `Control+Alt+S` or `Control+AltGraph+S`
// Windows: `Control+Alt+S` or `Control+AltGraph+S` or `AltGraph+S`
},
"$mod+Alt+KeyS": event => {
// macOS: `Meta+Alt+S` or `Meta+AltGraph+S`
// Windows: `Control+Alt+S` or `Control+AltGraph+S` or `AltGraph+S`
},
})
Keybinding Syntax
Keybindings are made up of a sequence of presses.
A press can be as simple as a single key which matches against
KeyboardEvent.code
and
KeyboardEvent.key
(case-insensitive).
Keybindings can also consist of several key presses in a row:
"g i" // i.e. "Go to Inbox"
"g a" // i.e. "Go to Archive"
"ArrowUp ArrowUp ArrowDown ArrowDown ArrowLeft ArrowRight ArrowLeft ArrowRight B A"
Each press can optionally be prefixed with modifier keys:
"$mod+K $mod+1" // i.e. "Toggle Level 1"
"$mod+K $mod+2" // i.e. "Toggle Level 2"
"$mod+K $mod+3" // i.e. "Toggle Level 3"
Each press in the sequence must be pressed within 1000ms of the last.
Keybinding Priorities
It should rarely come up, but the order of your keybindings matters. So if you
were to create two keybindings that would match the same event, only the first
will fire.
This mainly comes up when you create bindings for both KeyboardEvent#key and
KeyboardEvent#code in order to support more keyboard layouts:
In this case, if the user types g a it will only log "show goto indicator"
(after the first g)
Keybinding Sequence Conflicts
In some circumstances, overlapping keybindings can cause "conflicts" where a
keybinding has been completely typed, but there is another keybinding declared
earlier that might still match.
tinykeys(window, {
"a b c": () => console.log("abc"),
"a b": () => console.log("ab"),
})
In this case, if the user types a b it will not trigger either keybinding,
but it will print this warning to the console:
warning: tinykeys: Conflict found, "a b" did not fire, waiting for: ["a b c"]
Display the keyboard sequence
You can use the parseKeybinding method to get a structured representation of a
keyboard shortcut. It can be useful when you want to display it in a fancier way
than a plain string.
import { parseKeybinding } from "tinykeys"
let parsedShortcut = parseKeybinding("$mod+Shift+K $mod+1")
Results into:
[
[["Meta", "Shift"], "K"],
[["Meta"], "1"],
]
Additional Configuration Options
You can configure the behavior of tinykeys in a couple ways using a third
options parameter.
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/jamiebuilds/tinykeys là nguồn chính thức.
jamiebuilds/tinykeys có phải mã nguồn mở không?
Có — jamiebuilds/tinykeys phát hành theo license MIT, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/jamiebuilds/tinykeys.
jamiebuilds/tinykeys có trang demo không?
Dự án có trang chủ ở https://jamiebuilds.github.io/tinykeys/. Tab "Readme" ở trang này thường có ảnh chụp và hướng dẫn bắt đầu nhanh.
jamiebuilds/tinykeys dùng license gì?
jamiebuilds/tinykeys phát hành theo license MIT. Nên mở file LICENSE trên GitHub để xác nhận — license metadata đôi khi lệch với thực tế dự án.
jamiebuilds/tinykeys là gì?
jamiebuilds/tinykeys (jamiebuilds/tinykeys) là dự án TypeScript trên GitHub. Theo mô tả gốc: A tiny (~650 B) & modern library for keybindings.
Đọc đầy đủ README ở tab phía trên.
Vẫn đang phân vân về tinykeys?
Một cú bấm sẽ gửi câu hỏi kèm trang này cho AI — xem AI nói gì về tinykeys.