1

I need to disable shift keypress event in my site by using JavaScript or any other method. Below is my code:

$(document).ready(function() { document.onkeydown = checkKeycode function checkKeycode(e) { var keycode; if (window.event) { keycode = window.event.keyCode; } else if (e) { keycode = e.which; } //alert(keycode); if (keycode == 16) { alert(keycode); return false; } } }); 
2
  • 1
    This is not a code request forum. Explain what you tried...add some code, show examples and explain the problem. Commented Nov 25, 2011 at 12:43
  • 1
    @Neon i need to be briefed.... Commented Nov 25, 2011 at 12:58

4 Answers 4

7
// bind an event listener to the keydown event on the window window.addEventListener('keydown', function (event) { // if the keyCode is 16 ( shift key was pressed ) if (event.keyCode === 16) { // prevent default behaviour event.preventDefault(); return false; } }); 
Sign up to request clarification or add additional context in comments.

1 Comment

@Macraze it really depends on a lot of things, is the event delegated correctly ? does any other piece of code over rights the event ? how are you debugging ? what is the exact expected behavior ? etc.
3

http://api.jquery.com/keypress/

In addition, modifier keys (such as Shift) trigger keydown events but not keypress events.

Try this

$('#target').keydown(function(event) { if (event.shiftKey) { event.preventDefault(); } } 

Comments

1
document.onkeydown = function (e) { var e = e || event; if (e.shiftKey === true) { return false; } }; 

1 Comment

Works well for me in Firefox 8.0.1 jsfiddle.net/1stein/hvcQ7 Do you really want to disable Shift key?
0

You may try this:

jQuery(document).keydown(function(e){ if(e.which === 16) { e.preventDefault(); return; } console.log(e.which); }); 

See demo.

Use Firebug and check console output.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.