HighDots Forums  

a tasto premuto ottieni...

Javascript (Italian) Il linguaggio JavaScript (it.comp.lang.javascript)


Discuss a tasto premuto ottieni... in the Javascript (Italian) forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Antonio
 
Posts: n/a

Default a tasto premuto ottieni... - 02-07-2004 , 01:06 PM






Scusate se può sembrar banale, ma avrei bisogno di un aiutino:
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?
Ringrazio anticipatamente.
Antonio



Reply With Quote
  #2  
Old   
-Ema-
 
Posts: n/a

Default Re: a tasto premuto ottieni... - 02-07-2004 , 04:40 PM






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-


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.