Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disable Up Down keys 1

Status
Not open for further replies.

ZOR

Technical User
Joined
Jan 30, 2002
Messages
2,963
Location
GB
I have a form with many combo's on. Problem being the mousewheel chages value on scroll. I found and inserted a working piece of code which takes care of the mouse, however if a combo has focus, the up/down keys change the value. Is there anything I can add to disable them while on this page? Many thanks

<script type="text/javascript">
function stop()
{
return false;
}
document.onmousewheel=stop;
</script>
 
How about this?
Code:
<script>
if(window.Event) window.captureEvents(Event.KEYDOWN);

function keyDown(e){
  var n = (window.Event) ? e.which : e.keyCode;
  if(n==38 || n==40) return false;
}
</script>

<select onkeydown="return keyDown(event);">
<option>a
<option>b
<option>c
</select>

Adam
 
Also, you should limit disabling the mouse wheel to the <select> so they can still use it on the page.
Code:
<script>
if(window.Event) window.captureEvents(Event.KEYDOWN);

function keyDown(e){
  var n = (window.Event) ? e.which : e.keyCode;
  if(n==38 || n==40) return false;
}
</script>

<select onkeydown="return keyDown(event);" [red]onmousewheel="return false"[/red]>
<option>a
<option>b
<option>c
</select>

Adam
 
Thanks Adam for comming back. I have combo boxes with the PHP code:

<?
$val = $_SESSION['QTY']['10'];
for ( $i = 0; $i <= 2000; $i += 10 ) {
$selected = ($val == $i)?"selected":"";
echo "<option $selected value='" . $i . "'>" . $i . "</option>\n";
}

?>

The reason I'm disabling the mousewheel is if someone changes a value in a combo, and uses the wheel while the combo still has the focus, then they might not notice the selection they mad has changed. This was overcome by the code first posted, but then I found the up/down keys posed the same problem. Don't exactly know how to interface your code to try. Have no reason to make life difficult for people, its just to avoid any problems. Its only this page thats important. Thanks again.
 
Don't exactly know how to interface your code to try
I would think this would do it...
Code:
<script>
if(window.Event) window.captureEvents(Event.KEYDOWN);

function keyDown(e){
  var n = (window.Event) ? e.which : e.keyCode;
  if(n==38 || n==40) return false;
}
</script>

<select onkeydown="return keyDown(event);" onmousewheel="return false">
[blue]<?        
$val = $_SESSION['QTY']['10']; 
for ( $i = 0; $i <= 2000; $i += 10 ) {
    $selected = ($val == $i)?"selected":"";
    echo "<option $selected value='" . $i . "'>" . $i . "</option>\n";
}

?>[/blue]
</select>

Adam
 
Thanks for the try, I had tried that and it did not work. Just a shame my original bit of java for the mousewheel could not have included a stop for the keys. The code above opens the combos down the page, ie you can see 10,20,30,40 etc.
 
Just did a search and found this, but that does not want to work either. Is the code correct or missing anything? THANKS

<script language="JavaScript">
function disableKey()
{
if (window.event.keyCode == 38) window.event.keyCode = 0;
}

</script>
 
Thanjs - Internet Explorer 6.0 sp2
 
I suppose if you don't care about the mouse being disabled on the whole page, you could use this:
Code:
<script>
document.onmousewheel=function(){return false};
document.onkeydown=function(){return event.keyCode==38 || event.keyCode==40 ? false : true;}
</script>

Adam
 
Or even shorter:
Code:
<script>
document.onmousewheel=function(){return false};
document.onkeydown=function(){return event.keyCode!=38 && event.keyCode!=40}
</script>

Adam
 
Hey, thats done it. The longer code worked, shorter didn't. Excellent, many thanks for all the effort, have a star and good weekend. Regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top