Skip to content

Instantly share code, notes, and snippets.

@JimmyHoffa
Created October 15, 2013 17: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 JimmyHoffa/6995513 to your computer and use it in GitHub Desktop.
Save JimmyHoffa/6995513 to your computer and use it in GitHub Desktop.
Something like this... just off the top of my head
KbdButton.prototype = {
installHotkey: function() {
// Install a single anchor in the body to catch our accessor.
var accessor = $( "<a accesskey='k' style='display:none; position:absolute;'>" );
$( "body" ).append( accessor );
var whichKey = 0;
var keyCaptureDialog = $("body").append('<div id="captureKeys"></div>').dialog({
resizable: false,
height:140,
modal: true,
buttons: {
Cancel: function() {
$( this ).dialog( "close" );
}
});
keyCaptureDialog.on("keydown" function( event ) {
if( event.altKey ) {
keyCaptureDialog.dialog({buttons: "Alt+" + String.fromCharCode(event.which): function() {
whichKey = event.which;
$( this ).dialog( "close" );
},
Cancel: function() {
$( this ).dialog( "close" );
}
}}});
$( document ).on( "keydown", function( event ) {
// If Alt+K was pressed...
if( event.altKey && event.which == whichKey ) {
// ...find the parent KBD toggle button.
var kbdToggle = $( document.activeElement ).parents( ".wmd-container" ).find( "li.kbd-button" );
if( kbdToggle.length > 0 ) {
kbdToggle.click();
event.preventDefault();
}
}
} );
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment