Created
September 27, 2016 15:40
-
-
Save kvendrik/88993e24394a9c0a9a206890eea012d4 to your computer and use it in GitHub Desktop.
Shortcuts Lib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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