From 78de7a570d2d2fdb297fb61897040bac380aaa53 Mon Sep 17 00:00:00 2001 From: Daniel Tar Date: Sun, 20 Jan 2013 15:15:09 +0100 Subject: [PATCH] Mousetrap TypeScript definition files --- mousetrap/mousetrap-tests.ts | 43 ++++++++++++++++++++++++++++++++++++ mousetrap/mousetrap.d.ts | 21 ++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 mousetrap/mousetrap-tests.ts create mode 100644 mousetrap/mousetrap.d.ts diff --git a/mousetrap/mousetrap-tests.ts b/mousetrap/mousetrap-tests.ts new file mode 100644 index 0000000000..3eac1eef16 --- /dev/null +++ b/mousetrap/mousetrap-tests.ts @@ -0,0 +1,43 @@ +/// +/// + +Mousetrap.bind('4', function() { console.log('4'); }); +Mousetrap.bind("?", function() { console.log('show shortcuts!'); }); +Mousetrap.bind('esc', function() { console.log('escape'); }, 'keyup'); + +// combinations +Mousetrap.bind('command+shift+K', function() { console.log('command shift k'); }); + +// map multiple combinations to the same callback +Mousetrap.bind(['command+k', 'ctrl+k'], function() { + console.log('command k or control k'); + + // return false to prevent default browser behavior + // and stop event from bubbling + return false; +}); + +// gmail style sequences +Mousetrap.bind('g i', function() { console.log('go to inbox'); }); +Mousetrap.bind('* a', function() { console.log('select all'); }); + +// konami code! +Mousetrap.bind('up up down down left right left right b a enter', function() { + console.log('konami code'); +}); + +Mousetrap.bind(['ctrl+s', 'meta+s'], (e, combo) => { + if (e.preventDefault) { + e.preventDefault(); + } else { + // internet explorer + e.returnValue = false; + } +}); + +Mousetrap.unbind('?'); + +Mousetrap.trigger('esc'); +Mousetrap.trigger('esc', 'keyup'); + +Mousetrap.reset(); diff --git a/mousetrap/mousetrap.d.ts b/mousetrap/mousetrap.d.ts new file mode 100644 index 0000000000..d404eeca74 --- /dev/null +++ b/mousetrap/mousetrap.d.ts @@ -0,0 +1,21 @@ +// Type definitions for Mousetrap +// Project: http://craig.is/killing/mice +// Definitions by: Dániel Tar https://github.com/qcz +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +interface ExtendedKeyboardEvent extends KeyboardEvent { + returnValue: bool; // IE returnValue +} + +interface MousetrapStatic { + stopCallback: (e: KeyboardEvent, element: Element, combo: string) => bool; + + bind(keys: string, callback: (e: ExtendedKeyboardEvent, combo: string) => any, action?: string): void; + bind(keyArray: string[], callback: (e: ExtendedKeyboardEvent, combo: string) => any, action?: string): void; + unbind(keys: string, action?: string): void; + unbind(keyArray: string[], action?: string): void; + trigger(keys: string, action?: string): void; + reset(): void; +} + +var Mousetrap: MousetrapStatic;