Link to home
Start Free TrialLog in
Avatar of indyng
indyng

asked on

How can I disable the Tab and Enter Key?

Hi Experts,

How can I disable the Tab and Enter Key?

Thanks
Avatar of kawas
kawas
Flag of United States of America image

add a key 'listener'

function myKeyListener(event) {
       if (!event) {
            event = window.event;
      }
      //alert (event.keyCode);

      if (event.keyCode == 13) {
            // enter key - do nothing
      }
       // add one for the tab key
}

then add this function to the onkeydown event for the page/textbox/etc
https://www.experts-exchange.com/questions/21382788/onkeydown-event-Firefox.html tries something similar
Avatar of indyng
indyng

ASKER

Is it possible to disable these keys onload of the "default.asp" page rather than onkeydown?

Thanks
on the onload event, you would have to add the function so that onkeydown of whatever item you want the keys disabled
Avatar of indyng

ASKER

So this function is only fired when the user types?
yes
Avatar of indyng

ASKER

I want to fire the function on load of the "default.asp" page.

Can you provide more specific code for this? I am sorry but I am not very good at Java script.

Thanks

Steps are:

1. <body onload="init()">

2. define init()

function init() {
   document.onkeypress = myKeyListener;
   if (document.layers) document.captureEvents(Event.KEYPRESS);
}
function myKeyListener(event) {
       if (!event) {
          event = window.event;
     }
     //alert (event.keyCode);

     if (event.keyCode == 13) {
          // enter key - do nothing
     }
       // add one for the tab key
}


or a shorter example
http://www.felgall.com/jstip43.htm
Avatar of Michel Plungjan
@kawas: A bit nonchalant to not kill the event, to leave the tab key as an exercise for the reader and to not handle other browsers than IE
Following diables tab and enter key for all fields on page
<script language="javascript">
function keyHandler(e) {
var pK = e ? e.which : window.event.keyCode;
if (pk == 13) { return false; }
if (pk == 9) { return false; }
}

function DisableEnterAndTab () {
      if (document.all) // IE
     {
     document.onkeypress = kH;
     }
      if (document.layers)  {
        document.captureEvents(Event.KEYPRESS);
     }
}
</script>

<body onload="DisableEnterAndTab();">
that disables on a textfield, not the whole page
@pravinsar :Please test your code. Especially when the asker tells you he is not great at JS - document.all is IE and document.layers is NS4 - what about mozilla and FF?

<script language="javascript">
function kH(e) {
var pK = e ? e.which : window.event.keyCode;
if (pk == 13) { alert('No enter'); return false; }
if (pk == 9) { alert('No tab'); return false; }
}

function DisableEnterAndTab () {
      if (document.all) // IE
     {
     document.onkeypress = kH;
     }
      if (document.layers)  {
        document.captureEvents(Event.KEYPRESS);
     }
}
</script>

<body onload="DisableEnterAndTab();">
<form onSubmit="alert('No you did not disable them'); return false">
<input type="text" name="t1" value="">
<input type="text" name="t2" value="">
<input type="submit">

</form>
Avatar of indyng

ASKER

This is what I have:

function init()
{
 document.onkeypress = myKeyListener;
 if (document.layers) document.captureEvents(Event.KEYPRESS);
}

function myKeyListener(event)
{
 if (!event)
 {
  event = window.event;
 }
 if (event.keyCode == 13)
 {

 }
}

I checked out http://www.felgall.com/jstip43.htm

I do not know how to incorporate this. Please be more specific and just provide me with the script.

Thanks
@experts: I do not have time to find the combo I wanted to find.

Please look here and test before presenting
http://www.webmasterworld.com/forum91/5129.htm

Here is a cool site though. the prototype.js has a lot of cross browser event handling

http://prototype.conio.net/
Avatar of indyng

ASKER

This is what I have now and it works but only for Enter key:

<SCRIPT Language="JavaScript">
function kH(e)
{
 var pK = e ? e.which : window.event.keyCode;
 return pK != 13;
}

function DisableEnterAndTab ()
{
 if (document.all) // IE
  {
  document.onkeypress = kH;
  }
 if (document.layers)  
  {
  document.captureEvents(Event.KEYPRESS);
  }
}
</SCRIPT>

How do I incorporate the Tab key?

Thanks
return pK != 13 && pK != 9;
ASKER CERTIFIED SOLUTION
Avatar of Pravin Asar
Pravin Asar
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hehe.. Just needed a little nudge

Michel
Michel

Thanks for movitating me to get a better solution.

My earlier solution does not work in all browser.

Please try it yourself

Avatar of indyng

ASKER

return pK != 13 && pK != 9;

This is not working. Thanks
Pravinsar: I think this only works in IE: evt.keyCode = 13;
Avatar of indyng

ASKER

mplungjan: What you gave me - "return pK != 13 && pK != 9;" only works for Enter key and not Tab.

Thanks
Possible. Try Pravinsar's script.
Trying to catch tab event is a tricky thing. First I thought the code posted earlier would work. Hence a new version.