Skip to content

Instantly share code, notes, and snippets.

@kvendrik
Created September 27, 2016 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kvendrik/88993e24394a9c0a9a206890eea012d4 to your computer and use it in GitHub Desktop.
Save kvendrik/88993e24394a9c0a9a206890eea012d4 to your computer and use it in GitHub Desktop.
Shortcuts Lib
class Hottie
constructor: (config) ->
@_bindings = config.bindings;
@_waitForInputTime = config.waitForInputTime;
@_pressedKeys = '';
#bind keyup event
document.addEventListener('keyup', @_handleKey.bind(this), false);
_resetWaitInputTimer: (callback) ->
#wait for user input for x amount of time
#if there is no user input
#reset the pressed keys register and trigger
#the optional callback
afterTimeCallback = =>
@_pressedKeys = ''
callback?()
clearTimeout(@_waitInputTimer)
@_waitInputTimer = setTimeout(afterTimeCallback, @_waitForInputTime);
_handleKey: (e) ->
bindings = @_bindings;
keyCode = e.keyCode;
#register pressed key
@_pressedKeys += "#{keyCode}"
#loop all key bindings and
#check if the register matches one of the codes
for details in bindings
codeStr = details.keyCodes.join('')
if @_pressedKeys == codeStr
#pressed keys match config code
#wait for next input
#if there is no next input, trigger callback
return @_resetWaitInputTimer(details.callback)
#reset timer so the pressed keys are
#reset if there is no more user input
@_resetWaitInputTimer();
root = exports ? this
root.Hottie = Hottie;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment