Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

(Special keys) KeyPress

Posted on 2002-06-11
17
Medium Priority
?
511 Views
Last Modified: 2007-11-27
Is there a way to capture special key (F1,F2,...) press?

Regards
NTony.
0
Comment
Question by:ntony
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
  • 3
17 Comments
 

Expert Comment

by:hualing
ID: 7072191
yes,
when you click some key ,you can get the keyCode
by using    
var myKeyCode      = e.keyCode;
if myKeyCode = 112 ,F1 is pressed

for more infomation ,please view here,i just found this by google.

http://www.gar.no/gweb/gwebkbd.htm

hope this helpful

good luck

Ling
0
 
LVL 1

Author Comment

by:ntony
ID: 7072265
i'm using IE and it doesn't work.
The keycode=112 is for letter 'p'

0
 

Expert Comment

by:hualing
ID: 7072273
download the js of the page i gave u ,and investigate it,
I think you can make it.

Ling
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Expert Comment

by:hualing
ID: 7072283
download the js of the page i gave u ,and investigate it,
I think you can make it.

Ling
0
 
LVL 1

Author Comment

by:ntony
ID: 7072294
ok it's working but if i press F1 the help window apera and i don't whant this to happen.
0
 
LVL 14

Expert Comment

by:avner
ID: 7072860
then add :

window.onhelp="return false"
0
 
LVL 1

Author Comment

by:ntony
ID: 7072918
it's not working
0
 
LVL 14

Expert Comment

by:avner
ID: 7072924
try this :

<BODY onhelp="return false">

</BODY>
0
 
LVL 1

Author Comment

by:ntony
ID: 7074708
if i write the body tag without onKeyDown it's working
but if i write this :
<body onhelp="return false;"; onKeyDown="return doKeyDown(event);">

it's not working.
(the problem is not only with F1 but also with F3,F5,...)
How can invalidate all this and replace tham with my code.

Regards
NTony.
0
 
LVL 14

Expert Comment

by:avner
ID: 7074710
You cannot disable F keys, the only one you can disable is F1using th3 onhelp event handler.
just use :
<body onhelp="return doKeyDown(event);"; onKeyDown="return doKeyDown(event);">

Then when you handle the click on the onhelp event you can see if it's a F1 (112) or not.
0
 
LVL 1

Author Comment

by:ntony
ID: 7074791
it's not working
(have you try it?)
0
 
LVL 14

Expert Comment

by:avner
ID: 7074795
What's the behavior you get ? can you send your code ?
0
 
LVL 1

Author Comment

by:ntony
ID: 7075041
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title>Gweb JavaScript keyboard mapping</title>
<link rel=StyleSheet href="gweb.css">

<SCRIPT LANGUAGE="JavaScript" SRC="gwebkbd.js"> </SCRIPT>

<SCRIPT LANGUAGE="JavaScript">

var browserIsOK = 0;



function findKeyMap (keycode, shift, ctrl, alt) {
    for (var i = 0; i < fKeyMap.length; i++)
        if (fKeyMap[i].keyCode  == keycode
            && fKeyMap[i].shift == shift
            && fKeyMap[i].ctrl  == ctrl
            && fKeyMap[i].alt   == alt)
               if (fKeyMap[i].isMapped)
                  return ("mapped as " + fKeyMap[i].vKey);
               else
                  return ("unmapped as " + fKeyMap[i].vKey);

    return "not mapped to anything in the gwebkbd.js table";
} // findKeyMap

function doKeyDown (e) {
    var myKeyCode      = e.keyCode;
    var mySrcElement   = e.srcElement;
    var isShiftPressed = e.shiftKey;
    var isCtrlPressed  = e.ctrlKey;
    var isAltPressed   = e.altKey;
    if (!browserIsOK)
        return true;

    // Enter(13), Shift(16), Ctrl(17), Alt(18), CapsLock(20) keys?
    if (myKeyCode >= 13 && myKeyCode <= 20)
        return true;

window.onhelp="return false";
    alert("Key code=" + myKeyCode
        + "; Shift=" + isShiftPressed
        + "; Ctrl=" + isCtrlPressed
        + "; Alt=" + isAltPressed
        + "\nThis key is " + findKeyMap (myKeyCode, isShiftPressed, isCtrlPressed, isAltPressed)
        );

   return true;
} // doKeyDown

</SCRIPT>
</head>

<body onhelp="return doKeyDown(event);"; onKeyDown="return doKeyDown(event);">

<SCRIPT LANGUAGE="JavaScript">
browserIsOK = navigator.appVersion.indexOf("MSIE ");
if (browserIsOK < 0) {
   browserIsOK = 0;
   document.write ("<P><B><FONT COLOR=Red><BLINK>Warning:</BLINK></FONT>\n");
   document.write ("You are using a browser that does not support keyboard mapping.</B></P><HR>\n");
   }
else if (parseFloat (navigator.appVersion.slice(browserIsOK+5)) < 5.0) {
   browserIsOK = 0;
   document.write ("<P><B><FONT COLOR=Red><BLINK>Warning:</BLINK></FONT>\n");
   document.write ("The IE version you are currently using will not give complete functionality.</B></P><HR>\n");
   }
</SCRIPT>


</body></html>













the  gwebkbd.js file is:




fKeyMap = [
 { keyCode:49,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"command:F1"  }, // Alt/1
 { keyCode:49,  isMapped:true, shift:false, ctrl:false, alt:false,  vKey:"command:F1"  }, // Alt/1
 { keyCode:50,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"command:F2"  }, // Alt/2
 { keyCode:51,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"command:F3"  }, // Alt/3
 { keyCode:52,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"command:F4"  }, // Alt/4
 { keyCode:53,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"command:F5"  }, // Alt/5
 { keyCode:54,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"command:F6"  }, // Alt/6
 { keyCode:55,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"command:F7"  }, // Alt/7
 { keyCode:56,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"command:F8"  }, // Alt/8
 { keyCode:57,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"command:F9"  }, // Alt/9
 { keyCode:48,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"command:F10" }, // Alt/0
 { keyCode:187, isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"command:F11" }, // Alt/+
 { keyCode:219, isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"command:F12" }, // Alt/\
 { keyCode:49,  isMapped:true, shift:true,  ctrl:false, alt:true,  vKey:"command:F13" }, // Shift/Alt/1
 { keyCode:50,  isMapped:true, shift:true,  ctrl:false, alt:true,  vKey:"command:F14" }, // Shift/Alt/2
 { keyCode:51,  isMapped:true, shift:true,  ctrl:false, alt:true,  vKey:"command:F15" }, // Shift/Alt/3
 { keyCode:52,  isMapped:true, shift:true,  ctrl:false, alt:true,  vKey:"command:F16" }, // Shift/Alt/4
 { keyCode:53,  isMapped:true, shift:true,  ctrl:false, alt:true,  vKey:"command:F17" }, // Shift/Alt/5
 { keyCode:54,  isMapped:true, shift:true,  ctrl:false, alt:true,  vKey:"command:F18" }, // Shift/Alt/6
 { keyCode:55,  isMapped:true, shift:true,  ctrl:false, alt:true,  vKey:"command:F19" }, // Shift/Alt/7
 { keyCode:56,  isMapped:true, shift:true,  ctrl:false, alt:true,  vKey:"command:F20" }, // Shift/Alt/8
 { keyCode:57,  isMapped:true, shift:true,  ctrl:false, alt:true,  vKey:"command:F21" }, // Shift/Alt/9
 { keyCode:48,  isMapped:true, shift:true,  ctrl:false, alt:true,  vKey:"command:F22" }, // Shift/Alt/0
 { keyCode:187, isMapped:true, shift:true,  ctrl:false, alt:true,  vKey:"command:F23" }, // Shift/Alt/+
 { keyCode:219, isMapped:true, shift:true,  ctrl:false, alt:true,  vKey:"command:F24" }, // Shift/Alt/\
 { keyCode:49,  isMapped:true, shift:false, ctrl:true,  alt:false, vKey:"command:PA1" }, // Ctrl/1 (3270)
 { keyCode:50,  isMapped:true, shift:false, ctrl:true,  alt:false, vKey:"command:PA2" }, // Ctrl/2 (3270)
 { keyCode:51,  isMapped:true, shift:false, ctrl:true,  alt:false, vKey:"command:PA3" }, // Ctrl/3 (3270)
 { keyCode:67,  isMapped:true, shift:false, ctrl:true,  alt:false, vKey:"command:Break" },      // Ctrl/C
 { keyCode:68,  isMapped:true, shift:false, ctrl:true,  alt:false, vKey:"command:Disconnect" }, // Ctrl/D
 { keyCode:82,  isMapped:true, shift:false, ctrl:true,  alt:false, vKey:"command:Refresh" },    // Ctrl/R
 { keyCode:33,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"command:RollDown" },   // Alt/PageUp (5250)
 { keyCode:34,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"command:RollUp" },     // Alt/PageDown (5250)
 { keyCode:72,  isMapped:true, shift:false, ctrl:true,  alt:true,  vKey:"command:Help" },       // Ctrl/Alt/Home (5250)

 { keyCode:35,  isMapped:true, shift:false, ctrl:true,  alt:false, vKey:"GoToBottom" },  // Ctrl/End
 { keyCode:46,  isMapped:true, shift:true,  ctrl:false, alt:false, vKey:"EraseField" },  // Shift/Del
 { keyCode:46,  isMapped:true, shift:false, ctrl:true,  alt:false, vKey:"EraseEOP" },    // Ctrl/Del
 { keyCode:46,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"ClearScreen" }, // Alt/Del
 { keyCode:9,   isMapped:true, shift:true,  ctrl:false, alt:false, vKey:"BackTab" },     // Shift/Tab in first field
 { keyCode:36,  isMapped:true, shift:false, ctrl:false, alt:false, vKey:"CursorHome" },  // Home
 { keyCode:38,  isMapped:true, shift:false, ctrl:false, alt:false, vKey:"CursorUp" },    // Cursor Up
 { keyCode:40,  isMapped:true, shift:false, ctrl:false, alt:false, vKey:"CursorDown" },  // Cursor Down

 { keyCode:84,  isMapped:false, shift:false, ctrl:true,  alt:false, vKey:"macro:@E" },   // Ctrl/T: transmit key (example only)

 { keyCode:0,   isMapped:false } // End of table
];

// If you need to modify this table, you might want to use the
// HTML file "gwebkbd.htm" to help you find key codes easily.


0
 
LVL 14

Accepted Solution

by:
avner earned 200 total points
ID: 7075074
1. You are using two event handlers for same event and you keep calling both.
2. When you pop up alert you change IE event flow.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title>Gweb JavaScript keyboard mapping</title>
<link rel=StyleSheet href="gweb.css">


<SCRIPT LANGUAGE="JavaScript">
fKeyMap = [
{ keyCode:49,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"command:F1"  }, // Alt/1
{ keyCode:49,  isMapped:true, shift:false, ctrl:false, alt:false,  vKey:"command:F1"  }, // Alt/1
{ keyCode:50,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"command:F2"  }, // Alt/2
{ keyCode:51,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"command:F3"  }, // Alt/3
{ keyCode:52,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"command:F4"  }, // Alt/4
{ keyCode:53,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"command:F5"  }, // Alt/5
{ keyCode:54,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"command:F6"  }, // Alt/6
{ keyCode:55,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"command:F7"  }, // Alt/7
{ keyCode:56,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"command:F8"  }, // Alt/8
{ keyCode:57,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"command:F9"  }, // Alt/9
{ keyCode:48,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"command:F10" }, // Alt/0
{ keyCode:187, isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"command:F11" }, // Alt/+
{ keyCode:219, isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"command:F12" }, // Alt/\
{ keyCode:49,  isMapped:true, shift:true,  ctrl:false, alt:true,  vKey:"command:F13" }, // Shift/Alt/1
{ keyCode:50,  isMapped:true, shift:true,  ctrl:false, alt:true,  vKey:"command:F14" }, // Shift/Alt/2
{ keyCode:51,  isMapped:true, shift:true,  ctrl:false, alt:true,  vKey:"command:F15" }, // Shift/Alt/3
{ keyCode:52,  isMapped:true, shift:true,  ctrl:false, alt:true,  vKey:"command:F16" }, // Shift/Alt/4
{ keyCode:53,  isMapped:true, shift:true,  ctrl:false, alt:true,  vKey:"command:F17" }, // Shift/Alt/5
{ keyCode:54,  isMapped:true, shift:true,  ctrl:false, alt:true,  vKey:"command:F18" }, // Shift/Alt/6
{ keyCode:55,  isMapped:true, shift:true,  ctrl:false, alt:true,  vKey:"command:F19" }, // Shift/Alt/7
{ keyCode:56,  isMapped:true, shift:true,  ctrl:false, alt:true,  vKey:"command:F20" }, // Shift/Alt/8
{ keyCode:57,  isMapped:true, shift:true,  ctrl:false, alt:true,  vKey:"command:F21" }, // Shift/Alt/9
{ keyCode:48,  isMapped:true, shift:true,  ctrl:false, alt:true,  vKey:"command:F22" }, // Shift/Alt/0
{ keyCode:187, isMapped:true, shift:true,  ctrl:false, alt:true,  vKey:"command:F23" }, // Shift/Alt/+
{ keyCode:219, isMapped:true, shift:true,  ctrl:false, alt:true,  vKey:"command:F24" }, // Shift/Alt/\
{ keyCode:49,  isMapped:true, shift:false, ctrl:true,  alt:false, vKey:"command:PA1" }, // Ctrl/1 (3270)
{ keyCode:50,  isMapped:true, shift:false, ctrl:true,  alt:false, vKey:"command:PA2" }, // Ctrl/2 (3270)
{ keyCode:51,  isMapped:true, shift:false, ctrl:true,  alt:false, vKey:"command:PA3" }, // Ctrl/3 (3270)
{ keyCode:67,  isMapped:true, shift:false, ctrl:true,  alt:false, vKey:"command:Break" },      // Ctrl/C
{ keyCode:68,  isMapped:true, shift:false, ctrl:true,  alt:false, vKey:"command:Disconnect" }, // Ctrl/D
{ keyCode:82,  isMapped:true, shift:false, ctrl:true,  alt:false, vKey:"command:Refresh" },    // Ctrl/R
{ keyCode:33,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"command:RollDown" },   // Alt/PageUp (5250)
{ keyCode:34,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"command:RollUp" },     // Alt/PageDown (5250)
{ keyCode:72,  isMapped:true, shift:false, ctrl:true,  alt:true,  vKey:"command:Help" },       // Ctrl/Alt/Home 5250)
{ keyCode:35,  isMapped:true, shift:false, ctrl:true,  alt:false, vKey:"GoToBottom" },  // Ctrl/End
{ keyCode:46,  isMapped:true, shift:true,  ctrl:false, alt:false, vKey:"EraseField" },  // Shift/Del
{ keyCode:46,  isMapped:true, shift:false, ctrl:true,  alt:false, vKey:"EraseEOP" },    // Ctrl/Del
{ keyCode:46,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"ClearScreen" }, // Alt/Del
{ keyCode:9,   isMapped:true, shift:true,  ctrl:false, alt:false, vKey:"BackTab" },     // Shift/Tab in first field
{ keyCode:36,  isMapped:true, shift:false, ctrl:false, alt:false, vKey:"CursorHome" },  // Home
{ keyCode:38,  isMapped:true, shift:false, ctrl:false, alt:false, vKey:"CursorUp" },    // Cursor Up
{ keyCode:40,  isMapped:true, shift:false, ctrl:false, alt:false, vKey:"CursorDown" },  // Cursor Down
{ keyCode:84,  isMapped:false, shift:false, ctrl:true,  alt:false, vKey:"macro:@E" },   // Ctrl/T: transmit key (example only)
{ keyCode:0,   isMapped:false } // End of table
];
var browserIsOK = 0;



function findKeyMap (keycode, shift, ctrl, alt) {
   for (var i = 0; i < fKeyMap.length; i++)
       if (fKeyMap[i].keyCode  == keycode
           && fKeyMap[i].shift == shift
           && fKeyMap[i].ctrl  == ctrl
           && fKeyMap[i].alt   == alt)
              if (fKeyMap[i].isMapped)
                 return ("mapped as " + fKeyMap[i].vKey);
              else
                 return ("unmapped as " + fKeyMap[i].vKey);

   return "not mapped to anything in the gwebkbd.js table";
} // findKeyMap

function doKeyDown (e) {
   var myKeyCode      = e.keyCode;
   var mySrcElement   = e.srcElement;
   var isShiftPressed = e.shiftKey;
   var isCtrlPressed  = e.ctrlKey;
   var isAltPressed   = e.altKey;
   if (!browserIsOK)
       return true;

   // Enter(13), Shift(16), Ctrl(17), Alt(18), CapsLock(20) keys?
   if (myKeyCode >= 13 && myKeyCode <= 20)
       return true;

   window.status="Key code=" + myKeyCode
       + "; Shift=" + isShiftPressed
       + "; Ctrl=" + isCtrlPressed
       + "; Alt=" + isAltPressed
       + "\nThis key is " + findKeyMap (myKeyCode, isShiftPressed, isCtrlPressed, isAltPressed)   ;

if (window.event.type=="help")
      {
            firedBefore=true;
      }

 window.event.returnValue=false;
} // doKeyDown


function callKeyDown()
{
if (!firedBefore)
      {
            doKeyDown(event);
      }
      else
      {
            firedBefore=!firedBefore
      }
}
</SCRIPT>
</head>

<body onhelp="doKeyDown(event);"; onKeyDown="callKeyDown()">

<SCRIPT LANGUAGE="JavaScript">
var firedBefore=false;
browserIsOK = navigator.appVersion.indexOf("MSIE ");
if (browserIsOK < 0) {
  browserIsOK = 0;
  document.write ("<P><B><FONT COLOR=Red><BLINK>Warning:</BLINK></FONT>\n");
  document.write ("You are using a browser that does not support keyboard mapping.</B></P><HR>\n");
  }
else if (parseFloat (navigator.appVersion.slice(browserIsOK+5)) < 5.0) {
  browserIsOK = 0;
  document.write ("<P><B><FONT COLOR=Red><BLINK>Warning:</BLINK></FONT>\n");
  document.write ("The IE version you are currently using will not give complete functionality.</B></P><HR>\n");
  }
</SCRIPT>


</body></html>
0
 
LVL 1

Author Comment

by:ntony
ID: 7077286
ok got it.
Is there a way to do the same thing for F3,F4,F5??

Best Regards
NTony.
0
 
LVL 14

Expert Comment

by:avner
ID: 7081159
No. They are reserved for IE usage.
0
 
LVL 1

Author Comment

by:ntony
ID: 7082943
thaks for your help

Best regards
to all
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question