...

View Full Version : Acces key script



Zynex
01-15-2007, 11:27 AM
I just received a great script from someone for making your own acces keys. BUT I need the visitor to press a combination of 2 keys. For instance Ctrl + Y. Does anyone know how?

here's the code

<script type="text/javascript">
keys="hyg"
dest=["http://www.htmlforums.com","http://www.yahoo.com",
"http://www.google.com"]
if (document.layers)
document.captureEvents(Event.KEYPRESS)
function go_dest(e){
for (i = 0; i < dest.length; i++){
if (document.layers){
if (e.which==keys.charCodeAt(i))
window.location=dest[i]
}
else if (document.all){
if (event.keyCode==keys.charCodeAt(i))
window.location=dest[i]
}
else if ((document.getElementById)&&(!document.all)){
if (e==keys.charCodeAt(i))
window.location=dest[i]
}
}
}

document.onkeypress=go_dest
</script>

chump2877
01-15-2007, 11:51 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>

<script type="text/javascript">

function getKeys(e)
{
if (e.ctrlKey && e.keyCode == 89)
alert('You have just pressed the Control and the Y key');
}

</script>

<BODY>

<textarea onKeyUp='getKeys(event);'></textarea>

</BODY>
</HTML>

A list of keycodes are here: http://javascript.js-x.com/key_codes/

Zynex
01-15-2007, 02:26 PM
Thats still not it... this script only works when youre cursor is on the textfield.

otaku149
01-15-2007, 02:58 PM
Try this for cross-browser compatibility:


<script type="text/javascript">
function getKeys(e){
var key = (document.all)?event.keyCode:e.which;
var ctrl = (document.all)?event.ctrlKey:e.ctrlKey;
if(ctrl == true && key == 89){
alert('You have just pressed the Control and the Y key');
}
}
document.onkeyup = getKeys;
</script>

Zynex
01-15-2007, 03:05 PM
Thats it :thumbsup: Finally. I tried for ages, and the solution is actualy 5 lines of code haha. Thank you very much!

Zynex :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum