0

I currently have buttons that have accesskey='whateverKey' I have a jQuery that is ran when the document is ready. I want to be able to disable the alt key and only have it highlight the button instead of submit it. Which can be done if you put in a onClick='if(checkIfAlt())gotoPage' on every single button. is it possible to do that in a .ready? without having every button use the onClick?

I need it ran in this....

jQuery( document ).ready({ } 

I have tried all of these

jQuery(document).on('button', 'form', function(event) { alert('clicked'); event.preventDefault(); }); jQuery(document).bind("keydown", function(event){ event.preventDefault(); }); $("button").click(function(evt) { if (evt.ctrlKey) alert('Ctrl down'); if (evt.altKey) alert('Alt down'); }); 

Some of these are out there but I was just trying a bunch of stuff trying to figure out how to get this to work. Is this possible?

Thanks a lot!!

1 Answer 1

0

To disable the alt and/or the control keys you can do this - http://jsfiddle.net/jayblanchard/uwPrV/

$('body').keydown(function(e) { if(e.keyCode == 17 || e.keyCode == 18) { e.preventDefault(); } else { console.log(e.keyCode); } }); 
Sign up to request clarification or add additional context in comments.

4 Comments

I tried this too, even replaced 'body' with document and it doesnt prevent it. I did an alert() and it does alert me but doesnt prevent the (altKey + accessKey) submitting the button.
I tested this in a couple of browsers and both the alt and control keys were disabled, they did not provided anything on keydown. Can you provide a fiddle showing that these keys are not disabled when using this code?
Actually it prevents the CTRL key, but shows the keydown of the 'h' key. If you are using key combo's you'll always get 2 keycodes (or more if there is a complex sequence, like CTRL + ALT + DEL). So the CTRL key is properly disabled while the 'h' key is still enabled - as it should be. Did you expect to get a different keycode back when pressing two keys? Watch the console as you press.
You may to use keypress() depending on the browser. Here is more info - stackoverflow.com/questions/8269274/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.