...

View Full Version : Resolved fun with hotkeys



king2k5
08-13-2008, 04:23 AM
I have created some hotkeys for fun on my site, and to familiarize myself a little more with JS(im new to it). My hotkeys work, but i want it so that when ctrl is pressed in, the hotkeys arent activated... such as ctrl+a and ctrl+c are still accessible... my code so far:


function checkEnter(e){ //e is event object passed from function invocation
var characterCode //literal character code will be stored in this variable

if(e && e.which){ //if which property of event object is supported (NN4)
e = e
characterCode = e.which //character code is contained in NN4's which property
}
else{
e = event
characterCode = e.keyCode //character code is contained in IE's keyCode property
}


if(characterCode == 65){ //Letter A
window.location = "army.php" //submit the form
return false
}
if(characterCode == 72){//Letter H
window.location = "member.php" //submit the form
return false
}
if(characterCode == 77){//Letter M
window.location = "pm.php" //submit the form
return false
}
if(characterCode == 78){//Letter N
window.location = "news.php" //submit the form
return false
}
if(characterCode == 67){//Letter C
window.location = "construc.php" //submit the form
return false
}
if(characterCode == 82){//Letter R
window.location = "research.php" //submit the form
return false
}
if(characterCode == 69){//Letter E
window.location = "explore.php" //submit the form
return false
}
if(characterCode == 80){//Letter P
window.location = "population.php" //submit the form
return false
}
if(characterCode == 75){//Letter K
window.location = "map.php" //submit the form
return false
}
if(characterCode == 83){//Letter S
window.location = "market.php" //submit the form
return false
}
if(characterCode == 66){//Letter B
window.location = "bank.php" //submit the form
return false
}
if(characterCode == 84){//Letter T
window.location = "product.php" //submit the form
return false
}

else{
return true
}

}

<body onKeyDown="checkEnter(event)">

Thank you in advance

Philip M
08-13-2008, 08:24 AM
I don't think that you can detect ordinary keys modified by modifier keys in Javascript (Crtl is keycode 17).

Have a look at:-

http://journals.ecs.soton.ac.uk/java/tutorial/post1.0/ui/keylistener.html for a java solution.

dumpfi
08-13-2008, 10:40 AM
Try this:
<html>
<head>
<script type='text/javascript'>
function keyJump()
{
var e = arguments[0] || window.event;

if(!keyFromInput(e.target || e.srcElement) && !specialKeysPressed(e))
{
var
keyMapping = {
a : 'army.php',
h : 'member.php',
m : 'pm.php',
n : 'news.php',
c : 'construc.php',
r : 'research.php',
e : 'explore.php',
p : 'population.php',
k : 'map.php',
s : 'market.php',
b : 'bank.php',
t : 'product.php'},
key = String.fromCharCode((e.which === undefined) ? e.keyCode : e.which);

if(key in keyMapping)
{
window.location.href = keyMapping[key];
return false;
}
}
return true;
}
function keyFromInput(el)
{
if(el.nodeType == 3)
{
el = el.parentNode;
}
switch(el.tagName.toLowerCase())
{
case 'textarea':
return true;
case 'input':
if(el.type == 'text')
{
return true;
}
}
return false;
}
function specialKeysPressed(e)
{
return e.altKey || e.ctrlKey || e.metaKey || e.shiftKey;
}

window.onload = function()
{
document.onkeypress = keyJump;
}
</script>
</head>
<body></body>
</html>dumpfi

king2k5
08-13-2008, 03:36 PM
Thanks! that worked great!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum