Scott Yu - builds stuff
Scott Yu - builds stuff

Reputation: 11763

Jquery js-hotkeys plugin adding key pressed into input field

I am using js-hotkeys and when I press a hotkey like "f", it adds an f to the input field that I pulled up and focus on.

Is there anyway for it not to do that?

I also tried this other plugin http://rikrikrik.com/jquery/shortkeys/#download and it doesnt have that issue but then that plugin had other issue.

Thanks

Upvotes: 1

Views: 1419

Answers (1)

Nick Craver
Nick Craver

Reputation: 630389

You can prevent the default action using event.preventDefault(), in this case that default action is adding the letter to the textbox, for example:

$(document).bind('keydown', 'f', function(e) {
  $("input").focus();
  e.preventDefault();
});

Note: For those of you going WTF?: This is the syntax the plugin adds, it's not the normal .bind() jQuery core syntax.

I suggest you take a look at John Resig's re-write of the jQuery hotkeys plugin here, it's a much more efficient and 1.4.2+ compatible plugin, you can find the readme here.

See it in action here.

Upvotes: 4

Related Questions