Skip to content

Instantly share code, notes, and snippets.

@joshuabaker
Last active August 29, 2015 13:57
Show Gist options
  • Save joshuabaker/9781984 to your computer and use it in GitHub Desktop.
Save joshuabaker/9781984 to your computer and use it in GitHub Desktop.
(function($, undefined)
{
if (window.patatapSequenceIntervals)
{
// Clear
$.each(window.patatapSequenceIntervals, function(index, interval)
{
clearInterval(interval);
});
}
window.patatapSequenceIntervals = [];
var $window = $(window);
var startTime;
var keySequence;
var sequenceWait;
var recordKeys = function(event)
{
if ( ! startTime)
{
startTime = Date.now();
}
keySequence.push(
{
keyCode: event.keyCode,
wait: Date.now() - startTime
});
};
var startRecordingKeys = function()
{
keySequence = [];
$window.on('keydown', recordKeys);
};
var stopRecordingKeys = function()
{
$window.off('keydown', recordKeys);
sequenceWait = Date.now() - startTime;
startTime = false;
};
var pressKey = function(keyCode)
{
var event = $.Event('keydown',
{
which: keyCode
});
$window.trigger(event);
};
var initSequence = function()
{
var keySequenceClone = keySequence,
intervalFunction = function()
{
$.each(keySequenceClone, function(index, value)
{
setTimeout(function()
{
pressKey(value.keyCode);
}, value.wait);
});
};
var interval = setInterval(function()
{
intervalFunction();
}, sequenceWait);
intervalFunction();
window.patatapSequenceIntervals.push(interval);
};
$window
.on('keydown', function(event)
{
if (event.keyCode == 13)
{
startRecordingKeys();
}
})
.on('keyup', function(event)
{
if (event.keyCode == 13)
{
stopRecordingKeys();
initSequence();
}
})
.on('blur', function()
{
$.each(window.patatapSequenceIntervals, function(index, interval)
{
clearInterval(interval);
});
});
var defaultHint = $('#hint'),
delay = defaultHint.is(':visible') ? 1000 : 0,
hint = $('<div id="hint">'),
message = $('<div class="message">');
$('body').append(hint);
hint.append(message);
message.text('Hold enter key to queue; Let go to repeat');
if (delay)
{
defaultHint.fadeOut('slow');
}
hint.hide().delay(delay).fadeIn('slow', function()
{
setTimeout(function()
{
hint.fadeOut('slow');
}, 4000);
});
}(window.jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment