Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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