avoiding beep sound if(event.altKey)

I

Ivo

In an Win/IE-only application, some checkboxes and the like have been
assigned accesskeys, so pressing alt+w focuses a checkbox, alt+b another.
This is very convenient. However, when the checkboxes are hidden, for
example because they are in a div styled "display:none", pressing the magic
key combination does not work of course, but instead Windows generates an
warning beep, a sound. Now I have written a function that will toggle the
elements for me when they are out of view, but the beep still occurs.
Does anyone have a bright idea how to evade the warning?

function keyd(e) {
e=e||window.event; var k=e.keyCode;
k=String.fromCharCode(k).toLowerCase();
if( k=="w" && e.altKey) { if( rip('thediv') ) {
var i=document.f.wrp; i.checked=!i.checked; wrapta(); } return false;
}
if( k=="b" && e.altKey) { if( rip('thediv') ) {
var i=document.f.bld; i.checked=!i.checked; } return false;
}
}
function rip(n){
return document.getElementById(n).className=='hid'?1:0;
}

Thanks
Ivo
 
G

Grant Wagner

Ivo said:
In an Win/IE-only application, some checkboxes and the like have been
assigned accesskeys, so pressing alt+w focuses a checkbox, alt+b another.
This is very convenient. However, when the checkboxes are hidden, for
example because they are in a div styled "display:none", pressing the magic
key combination does not work of course, but instead Windows generates an
warning beep, a sound. Now I have written a function that will toggle the
elements for me when they are out of view, but the beep still occurs.
Does anyone have a bright idea how to evade the warning?

There is no way to stop the beep. IE/Windows is recognizing that there is no
ALT key combination matching what the user is pressing prior to your code ever
being run.
function keyd(e) {
e=e||window.event; var k=e.keyCode;
k=String.fromCharCode(k).toLowerCase();
if( k=="w" && e.altKey) { if( rip('thediv') ) {
var i=document.f.wrp; i.checked=!i.checked; wrapta(); } return false;
}
if( k=="b" && e.altKey) { if( rip('thediv') ) {
var i=document.f.bld; i.checked=!i.checked; } return false;
}
}
function rip(n){
return document.getElementById(n).className=='hid'?1:0;

You want rip() to return a boolean (true or false), not 1 or 0 (which then have
to be evaluated to true or false). For functions like this:

return (document.getElementById(n).className == 'hid');

is sufficient. The test evaluates to a boolean (true or false). That boolean
value is returned and the if (...). It's sort of silly to test a boolean
condition, return an integer and force JavaScript to evaluate that integer
value back into a boolean.

This has nothing to do with your problem, but it's just better code design.
 
I

Ivo

There is no way to stop the beep. IE/Windows is recognizing that there is no
ALT key combination matching what the user is pressing prior to your code ever
being run.

Was afraid so. Thanks for the confirmation. I have been experimenting with
dynamically assigning the keys to an arbitrary element that is visible, but
whatever I try, the beep comes before the code. On a sidenote, I find it
strange that you cannot have multiple accesskeys on an element. For example,
<input accesskey="z" accesskey="k">
will make alt+z but not alt+k focus the element (tested in IE and Moz). This
is clearly an unnecessary restriction unless I 'm missing something (again).

You want rip() to return a boolean (true or false), not 1 or 0 (which then have
to be evaluated to true or false). For functions like this:

return (document.getElementById(n).className == 'hid');

is sufficient. The test evaluates to a boolean (true or false). That boolean
value is returned and the if (...). It's sort of silly to test a boolean
condition, return an integer and force JavaScript to evaluate that integer
value back into a boolean.

This has nothing to do with your problem, but it's just better code
design.

Very true, thanks a lot.
Ivo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top