Skip to content

Instantly share code, notes, and snippets.

@peaBerberian
Last active September 20, 2016 12:58
Show Gist options
  • Save peaBerberian/e3a1ab3d0c923a3241d928869052c2a5 to your computer and use it in GitHub Desktop.
Save peaBerberian/e3a1ab3d0c923a3241d928869052c2a5 to your computer and use it in GitHub Desktop.
import { RKeyboard } from 'rkeyboard';

// creating a SimpleKeyboard
const kb = RKeyboard.create();

// simply listening to the 'Enter', 'Up' and 'a' keys
kb('Enter', 'Up', 'a', pushCallback, releaseCallback);

// listen to key press event on Up key configured with `after` and
// `interval` options
kb('Up', { press: { after: 1000, interval: 2000 } },
  pushCallback,
  pressCallback, // note this was added here
  releaseCallback
);

// stop listening
kb('Up', { press: { after: 1000, interval: 2000 } },
  pushCallback,
  pressCallback, // note this was added here
  releaseCallback
);

// stop listening
const stopListening = kb(pushCallback, releaseCallback);
stopListening();
import { SimpleKeyboard } from 'keyboardjs';

const kb = SimpleKeyboard.create();

const myKeys = kb('Enter', 'Up', 'Down', { press: { after: 200 } });

// catch 'push' events only (first keydown)
myKeys.onPush = (e) => {
  // 'push' here
  console.log(e.event);

  // 'Enter', 'Up' or 'Down' as they are the only keys listened to
  console.log(e.keyName);

  // Time the key has been pushed in ms (0 for 'push' event)
  console.log(e.timepress);

  // ...
};

// catch 'press' events (here, after 200ms of press)
myKeys.onPress = (e) => {
  // ...
};

// catch 'release' events (keyup)
myKeys.onRelease = (e) => {
  // ...
};

// catch every events (all 3 of them)
myKeys.onEvent = (e) => {
  // ...
};

// catch both push and press events
myKeys.onDown = (e) => {
  // ...
};

// stop listening to these keys
myKeys.stopListening();
import { MinimalKeyboard } from 'keyboardJs';

const kb = MinimalKeyboard.create();

// simply listening to the 'Enter', 'Up' and 'Down' keys
kb('Enter', 'Up', 'Down', (e) => {
  // either 'push' or 'release'
  console.log(e.event);

  // 'Enter', 'Up' or 'Down' as they are the only keys listened to
  console.log(e.keyName);

  // Time the key has been pushed in ms (0 for 'push' event)
  console.log(e.timepress);

  // ...
});

// listen to key press event on Up key configured with `after` and
// `interval` options
kb('Up', { press: { after: 1000, interval: 2000 }, (e) => {
  // either 'push', 'release' or 'press'
  console.log(e.event);

  // ...
});

// stop listening
const stopListening = kb('Left', (e) => {
  // ...
});

stopListening();
@augustinr
Copy link

kb([ 'up', ... ] , onKeyDown)

kb([ 'up', ... ] , {
  onKeyDown,
  onKeyPress
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment