keithamus / jwerty

⌨ Awesome handling of keyboard events
http://keithamus.github.io/jwerty
Other
1.21k stars 109 forks source link

A way how to detect/scan pressed shortcuts #28

Closed darsain closed 11 years ago

darsain commented 11 years ago

Let's say I want a users to be able to change their shortcuts, so I need a way how to detect what are they pressing. Something like:

var newShortcut = oldShortcut;
var scanner = jwerty.scan(function (shortcut) {
    // 'shortcut' in a format usabe by jwerty.key();
    console.log(shortcut); // ctrl+shift+g
    newShortcut = shortcut;

    // disables default action & bubbling
    // no need for that when changing shortcuts
    // previously bound shortcuts should also not fire
});

// When user is finished, he saves the shortcut
$('#save-shortcut-button').on('click', function () {
    shortcuts.change('some_action', newShortcut);
    scanner.stop();
});

That's just what my mind vomited in last 30 seconds, I hope you catch my drift :)

keithamus commented 11 years ago

This issue is pretty old, but I'm pretty sure you can do this in the existing version of Jwerty like so:

var shortcut;
$(body).on('keyup', function (event) {
    if (jwerty.is(shortcut, event)) {
        // do stuff
    }
});

// When user is finished, he saves the shortcut
$('#save-shortcut-button').on('click', function () {
    shortcut = $('#shortcut-input').val();
});

If this doesn't work for you, could you please raise a new issue with a more detailed explanation as to why my above solution wont work, and what you feel you need from jwerty.