Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A JavaScript library for binding keyboard combos without the pain of key codes and key combo conflicts.

License

NotificationsYou must be signed in to change notification settings

RobertWHurst/KeyboardJS

Repository files navigation


NotePlease Note: I've create a new libary calledKeystrokes which serves the same purposeas KeyboardJS, but is more refined, and takes full advantage of modern browsers. If you areconsidering using KeyboardJS in a new project, I recommend checking outKeystrokes first.


Build StatusNPM VersionDownloads This WeekLicense

KeyboardJS is a library for use in the browser (node.js compatible). It Allowsdevelopers to easily setup key bindings. Use key combos to setup complexbindings. KeyboardJS also provides contexts. Contexts are great for single pageapplications. They allow you to scope your bindings to various parts of yourapplication. Out of the box keyboardJS uses a US keyboard locale. If you needsupport for a different type of keyboard KeyboardJS provides custom localesupport so you can create with a locale that better matches your needs.

KeyboardJS is available as a NPM module. If you're not using a build systemlike webpack, simply addkeyboard.jsorkeyboard.min.jsfrom the dist folder in this repo to your project via a script tag.

npm install keyboardjs

Note that all key names can be found in./locales/us.js.

Setting up bindings is easy

keyboardJS.bind('a',(e)=>{console.log('a is pressed');});keyboardJS.bind('a + b',(e)=>{console.log('a and b is pressed');});keyboardJS.bind('a + b > c',(e)=>{console.log('a and b then c is pressed');});keyboardJS.bind(['a + b > c','z + y > z'],(e)=>{console.log('a and b then c or z and y then z is pressed');});keyboardJS.bind('',(e)=>{console.log('any key was pressed');});//alt, shift, ctrl, etc must be lowercasekeyboardJS.bind('alt + shift > a',(e)=>{console.log('alt, shift and a is pressed');});// keyboardJS.bind === keyboardJS.on === keyboardJS.addListener

keydown vs a keyup

keyboardJS.bind('a',(e)=>{console.log('a is pressed');},(e)=>{console.log('a is released');});keyboardJS.bind('a',null,(e)=>{console.log('a is released');});

Prevent keydown repeat

keyboardJS.bind('a',(e)=>{// this will run once even if a is helde.preventRepeat();console.log('a is pressed');});

Unbind things

keyboardJS.unbind('a',previouslyBoundHandler);// keyboardJS.unbind === keyboardJS.off === keyboardJS.removeListener

Using contexts

// these will execute in all contextskeyboardJS.bind('a',(e)=>{});keyboardJS.bind('b',(e)=>{});keyboardJS.bind('c',(e)=>{});// these will execute in the index contextkeyboardJS.setContext('index');keyboardJS.bind('1',(e)=>{});keyboardJS.bind('2',(e)=>{});keyboardJS.bind('3',(e)=>{});// these will execute in the foo contextkeyboardJS.setContext('foo');keyboardJS.bind('x',(e)=>{});keyboardJS.bind('y',(e)=>{});keyboardJS.bind('z',(e)=>{});// if we have a router we can activate these contexts when appropriatemyRouter.on('/',(e)=>{keyboardJS.setContext('index');});myRouter.on('/foo',(e)=>{keyboardJS.setContext('foo');});// you can always figure out your context tooconstcontextName=keyboardJS.getContext();// you can also set up handlers for a context without losing the current contextkeyboardJS.withContext('bar',()=>{// these will execute in the bar contextkeyboardJS.bind('7',(e)=>{});keyboardJS.bind('8',(e)=>{});keyboardJS.bind('9',(e)=>{});});

pause, resume, and reset

// the keyboard will no longer trigger bindingskeyboardJS.pause();// the keyboard will once again trigger bindingskeyboardJS.resume();// all active bindings will released and unbound,// pressed keys will be clearedkeyboardJS.reset();

pressKey, releaseKey, and releaseAllKeys

// pressKeykeyboardJS.pressKey('a');// orkeyboardJS.pressKey(65);// releaseKeykeyboardJS.releaseKey('a');// orkeyboardJS.releaseKey(65);// releaseAllKeyskeyboardJS.releaseAllKeys();

watch and stop

// bind to the window and document in the current windowkeyboardJS.watch();// or pass your own window and documentkeyboardJS.watch(myDoc);keyboardJS.watch(myWin,myDoc);// or scope to a specific elementkeyboardJS.watch(myForm);keyboardJS.watch(myWin,myForm);// detach KeyboardJS from the window and document/elementkeyboardJS.stop();

About

A JavaScript library for binding keyboard combos without the pain of key codes and key combo conflicts.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors31


[8]ページ先頭

©2009-2025 Movatter.jp