Skip to content

Commit

Permalink
Merge pull request #119 from anoadragon453/anoa/unregister_keys
Browse files Browse the repository at this point in the history
Ability to unregister a shortcut via the shortcut's keys
  • Loading branch information
Djiit authored Nov 3, 2018
2 parents bcd5677 + 1fa1c59 commit 4e5c003
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
10 changes: 9 additions & 1 deletion docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,17 @@ You can unregister shortcut by using shortcutId returned by `registerShortcut()`
ioHook.unregisterShortcut(id);
```

### unregisterShortcutByKeys(keys)

You can unregister shortcut by using the keys codes passed to `registerShortcut()`. Passing codes in the same order as during registration is not required.

```js
ioHook.unregisterShortcutByKeys(keys);
```

### unregisterAllShortcuts()

You can also unregister all shortcuts
You can also unregister all shortcuts.
```js
ioHook.unregisterAllShortcuts();
```
Expand Down
6 changes: 6 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ declare class IOHook extends EventEmitter {
*/
unregisterShortcut(shortcutId: number): void

/**
* Unregister shortcut via its key codes
* @param {Array<string|number>} keys
*/
unregisterShortcut(keys: Array<string|number>): void

/**
* Unregister all shortcuts
*/
Expand Down
41 changes: 41 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,47 @@ class IOHook extends EventEmitter {
});
}

/**
* Unregister shortcut via its key codes
* @param {string} keyCodes Keyboard keys matching the shortcut that should be unregistered
*/
unregisterShortcutByKeys(keyCodes) {
// A traditional loop is used in order to access `this` from inside
for (let i = 0; i < this.shortcuts.length; i++) {
let shortcut = this.shortcuts[i];

// Convert any keycode numbers to strings
keyCodes.forEach((key, index) => {
if (typeof key !== 'string' && !(key instanceof String)) {
// Convert to string
keyCodes[index] = key.toString();
}
})

// Check if this is our shortcut
Object.keys(shortcut).every(key => {
if (key === 'callback' || key === 'id') return;

// Remove all given keys from keyCodes
// If any are not in this shortcut, then this shortcut does not match
// If at the end we have eliminated all codes in keyCodes, then we have succeeded
let index = keyCodes.indexOf(key);
if (index === -1) return false; // break

// Remove this key from the given keyCodes array
keyCodes.splice(index, 1);
return true;
});

// Is this the shortcut we want to remove?
if (keyCodes.length === 0) {
// Unregister this shortcut
this.shortcuts.splice(i, 1);
return;
}
}
}

/**
* Unregister all shortcuts
*/
Expand Down
23 changes: 23 additions & 0 deletions test/specs/keyboard.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,27 @@ describe('Keyboard events', () => {
robot.keyToggle('shift', 'up');
}, 50);
});

it('can unregister a shortcut via its keycodes', (done) => {
expect.assertions(0);

let shortcut = [42, 30];

// Register the shortcut
ioHook.registerShortcut(shortcut, (event) => {
// We're unregistering this shortcut. It should not have been called
expect.not.toHaveBeenCalled();
});

// Unregister the shortcut
ioHook.unregisterShortcutByKeys(shortcut);

ioHook.start();

setTimeout(() => { // Make sure ioHook starts before anything gets typed
robot.keyToggle('shift', 'down');
robot.keyTap('a');
robot.keyToggle('shift', 'up');
}, 50);
});
});

0 comments on commit 4e5c003

Please sign in to comment.