On Sat, 07 Feb 2004 18:06:47 GMT, Antonio wrote:
Quote:
Vorrei che mi riconoscesse il tasto che premo ad esempio con un alert:
es
script
if(keypress="a") alert('Hai premuto a')
if(keypress="b") alert('Hai premuto b')
/scritp
ma così non funziona,
come si potrebbe fare? |
1. L'operatore di confronto Equal e' == e non =. (=== per strict equal)
2. La pressione di un tasto scatena un evento. Gli eventi hanno un target
(tipicamente un elemento, un tag). Devi pertanto dichiarare l'event handler
in un elemento della tua pagina, e solo quando questo ha il focus ricevera'
effettivamente l'imput.
Esempio preso dal javascript reference 1.3 di Netscape:
In this example, the captureEvents method catches keyboard input and the
onKeyPress handler calls the blockA function to examine the keystrokes. If
the keystrokes are "a" or "z", the function scrolls the Navigator window.
-------------------------------------------------------
function blockA(e) {
var keyChar = String.fromCharCode(e.which);
if (keyChar == 'A' || keyChar == 'a')
self.scrollBy(10,10);
else if(keyChar == 'Z' || keyChar == 'z')
self.scrollBy(-10,-10);
else return false;
}
document.captureEvents(Event.KEYPRESS);
document.onkeypress = blockA;
-------------------------------------------------------
Ciao.
-Ema-