Keywatch JavaScript Library Examples

A JavaScript library for watching/listening to keyboard events.

To watch for the Enter key you would have to call

Keywatch.watch('Enter', doSomething)

where doSomething is the handler function that does stuff for you.

We can specify keyboard modifiers to further adjust if we only want to listen to key combinations. These are Shift, Alt, Control and Meta. The latter is Command on Macs and Win on Windows. Each of them has a us-ascii short representation: +#^@, and even accept the usual mac-keys ⇧⌥⌘

The following examples will trigger for the same actions

Shift watch("Shift KeyA", action) watch("+a", action) watch("⇧a", action)
Alt watch("Alt KeyS", action) watch("#s", action) watch("⌥s", action)
Control watch("Control KeyD", action) watch("ctrl d", action) watch("^d", action)
{{ metaName }} watch("Meta KeyF", action) watch("@f", action) watch("⌘f", action)

Give these key-combinations a try: {{ keyLessonsDoneCount }} / 4

You may have noticed, that Control-D or {{ metaName }}-F may have triggered a default browser action.

In order to prevent these we have to prefix the key definition with a NoDefault (short !)

Control-D watch("NoDefault Control KeyD", action) watch("!^d", action)
{{ metaName }}-F watch("NoDefault {{metaName}} KeyF", action) watch("[email protected]", action)
Depending on your browser or system this might not always work!

You can also use more than one Modifier for a Key. Just keep in mind that the key definition is always exact. This means that Shift KeyA will not trigger on Shift Alt KeyA!

Shift-E watch("Shift KeyE", action) watch("+KeyE", action)
Shift-Alt-E watch("Shift Alt KeyE", action) watch("+#KeyE", action)
Shift-Control-E watch("Shift Control KeyE", action) watch("+^KeyE", action)
That's all for now, but I hope it will receive an update shortly.