Web Hosting Talk







View Full Version : keyboard control and hyperlink


0124
12-11-2007, 08:30 PM
is that possible that i can assign a combination (such as Ctrl + 1, Ctrl + 2) to a hyperlink.

ex:
www.cnn.com (assigned as Ctrl + 1)
www.nba.com (assigned as Ctrl + 2)
www.youtube.com (assigned as Ctrl + 3)

so when the user do Ctrl+1, will take him/her to www.cnn.com.

possible?

thx

Xeentech
12-11-2007, 09:14 PM
You could do this with a bit of javascript, using the OnKeyDown event.

0124
12-11-2007, 10:18 PM
You could do this with a bit of javascript, using the OnKeyDown event.

any example? I found some but they are all for a KEY not a combination of keys.

thx

Xeentech
12-11-2007, 10:50 PM
I the Event that your function is passed is an attribute that tells you wich modifier keys were held at the time, shift alt or ctrl.

So you'd do a OnKeyDown, then check if it is a one, then check ctrl work held..

Cliff Joffrion
12-11-2007, 11:42 PM
This is very interesting.

jstanden
12-12-2007, 10:36 PM
Here's an example we use (Firefox/IE6/IE7/Safari/Opera).

I'm offering it without any support, but if I get a chance I'll check in if you have questions. :)

keys.js:

// ***********
function CreateKeyHandler(cb) {
if(window.Event) {
document.captureEvents(Event.KEYDOWN);
}

document.onkeydown = cb;
}

function getKeyboardKey(evt) {
var browser=navigator.userAgent.toLowerCase();
var is_ie=(browser.indexOf("msie")!=-1 && document.all);

if(window.Event) {
if(evt.altKey || evt.metaKey || evt.ctrlKey) {
return;
}
mykey = evt.which;
}
else if(event) {
evt = event;
if((evt.modifiers & event.ALT_MASK) || (evt.modifiers & evt.CTRL_MASK)) {
return;
}
mykey = evt.keyCode
}

mykey = String.fromCharCode(mykey);

var src=null;

try {
if(evt.srcElement) src=evt.srcElement;
else if(evt.target) src=evt.target;
}
catch(e) {}

if(null == src) {
return;
}

for(var element=src;element!=null;element=element.parentNode) {
var nodename=element.nodeName;
if(nodename=="TEXTAREA"
|| (nodename=="SELECT")
|| (nodename=="INPUT") // && element.type != "checkbox"
|| (nodename=="BUTTON")
)
{ return; }
}

return mykey;
}


index.html:

<script type="text/javascript">
CreateKeyHandler(function doShortcuts(e) {

var mykey = getKeyboardKey(e);

// [JAS]: Header Shortcuts
switch(mykey) {
case "1":
document.location = '#';
break;
case "2":
document.location = '#';
break;
}
});
</script>


In our case we ignore modifiers (ALT/CTRL), but you could tweak keys.js to do whatever you need.

(Note: I also pulled this out of working code and modified it for posting, so YMMV.)