Avatar billede dreamcode Nybegynder
08. maj 2001 - 11:40 Der er 4 kommentarer og
1 løsning

Detect et keyboard tryk

hvordan detecter jeg et CTRL + F4 tryk i javascript ?
Avatar billede Slettet bruger
08. maj 2001 - 12:05 #1
The following script allows your visitors to reload your page simply by pressing \"r\":

<script language=\"JavaScript1.2\">

///if the user is using NS 4+
if (document.layers)
document.captureEvents(Event.KEYPRESS)
function reload_window(e){
if(String.fromCharCode(e.which)==\"r\")
window.location.reload()
}

///if the user is using NS 4+
if (document.layers)
document.onkeypress=reload_window

</script>

Nothing too out of the ordinary here- we use e.which in combination with String.fromCharCode() to detect when \"r\" is pressed, and reload the window when that happens. Notice the object detection code document.layers, which is present to screen out NS 4+ browsers.

-Launch navbar example

As an even more practical example of the keyboard interacting with JavaScript, the following launches a navigational window (aka: remote) when the user presses \"a\" or \"A\":

<script language=\"JavaScript1.2\">

if (document.layers)
document.captureEvents(Event.KEYPRESS)
function reload_window(e){
if(String.fromCharCode(e.which)==\"a\"||
String.fromCharCode(e.which)==\"A\")
navwin=window.open(\'nav.htm\',\'\',\'width=190,height=390\')
}

if (document.layers)
document.onkeypress=reload_window

</script>

Try pressing \"a\" now to see the script in action.

All of the above examples are intentionally made simplistic to allow you to focus on the fundamental technique to detecting and reacting to a key press. Having understood that, you can then go on to creating more complex key scripts, such as ones that detect when a certain word has been typed, advanced form validation scripts, and so on. But wait, we\'re not quite through with this subject matter just yet. In Part 2 of \"JavaScript and your keyboard\", we\'ll see how to detect and handle key presses in IE 4+. Stay tuned!
Avatar billede Slettet bruger
08. maj 2001 - 12:05 #2
Avatar billede dreamcode Nybegynder
08. maj 2001 - 14:51 #3
Det jeg ville detecte var faktisk et \"CTRL + F4\" tryk .... er det muligt ?
Avatar billede Slettet bruger
08. maj 2001 - 20:03 #4
Jeg tror at hvis du finder charkoderne for Ctrl + F4 og bytter ovenstående ud med dem...Men er ikke nogen haj til JavaScript...
Avatar billede engrob Nybegynder
08. maj 2001 - 21:40 #5
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Vi tilbyder markedets bedste kurser inden for webudvikling

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester