Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OnKeyPress F1 1

Status
Not open for further replies.

Mighty

Programmer
Feb 22, 2001
1,682
US
I am designing a website and would like to be able to use the KeyPress event to launch online help. Ideally, I would like to use F1 but could use another function key if this was not possible.

Can anyone tell me how I can do this. Mise Le Meas,

Mighty :)
 
Hi Mighty,

I don't know if you can use the F1 key.
Better way to know if you can use it is to see if it returns an event number.

Try this sample to see the event number and type:

function detectEvent() {
alert(event.type);
alert(event.which);
};

(Better is to put this script on an onKeyPress in a textarea)

Hope this helps ...
 
SeAl,

Thanks for the response. I modified it slightly and used he onKeyPress event in the BODY tag and none of the function keys gave a response. Would you have any other suggestions as to how I could do what I described.

I could just have a help link on each page but I was hoping to avoid that. Mise Le Meas,

Mighty :)
 
Mighty,

After searching my knowledge base, I can't find anything on Function Key with Javascript, I think you cannot do it via Javascript :-(

Perhaps you can do something using activeX but i'm not sure ...

Hope this helps ...
 
Mighty,

Did you take a look to FAQ216-1052?

Hope this helps ...
 
SeAL,

Had already looked at that but it doesn't really solve my problem. What I want to be able to do is allow the user to access help at any time. For example, if the customer is at a form page, he/she can enter a certain key and a window will open containing information on how to fill out that form. Thus I need to be able to use a key that the customer is not likely to type into the form.

I think that I will just have to have a help button.
Thanks for th help though. Mise Le Meas,

Mighty :)
 
Mighty,

It seems that all functions keys get a keycode like all the others.
You can get those keyCode with this little script i've just made:

<script language=&quot;JavaScript&quot;>
<!--
ns4 = (document.layers)? true:false
ie4 = (document.all)? true:false
function keyDown(e) {
if (ns4) {
var nKey = e.which
alert(&quot;nKey=&quot; + nKey)
}
if (ie4) {
var ieKey = event.keyCode
alert(&quot;ieKey=&quot; + ieKey)
}
}
document.onkeydown = keyDown
if (ns4) document.captureEvents(Event.KEYDOWN)
//-->
</SCRIPT>

It seems to work to get the keyCode but I don't know if you can do something with it. (I'm sure this script works in IE but I don't have any NN here :-( )

Hope this helps ...
 
Mighty,

It seems to be between 112 and 123

Hope this helps ...
 
Thanks SeAL.
Much appreciated. It enables me to get the keycode as you say. I'm sure I can figure out some way of using that. Mise Le Meas,

Mighty :)
 
Can I get a copy of your script if you figure out some way of using that? lol
 
Are you only interested if I get it workin with the F1 key or does any function key interest you?? Mise Le Meas,

Mighty :)
 
Unfortunatly, Javascript does not give you access directly to the function keys. Its a security limitation that they included. However Mighty, you can use other keys. The H key for example. Its about as universial towards the help topic as F1 is. You could set up a script that whenever the client presses H on their keyboard, a window is popped up with the relevent document.
 
Bentley22,

In theory that is all good but what if they are just filling in form fields. They don't want to be bringing up help menus when they are just filling in text boxes. Mise Le Meas,

Mighty :)
 
Heres the original bit of code by SeAL, modified to use the H key:

Code:
	<script language=&quot;JavaScript&quot;>
<!--
ns4 = (document.layers)? true:false
ie4 = (document.all)? true:false
function keyDown(e) {
  if (ns4) {
  	 if (e.which == &quot;72&quot;)
	 {
	  alert(&quot;your Help Box here&quot;);
	 }
  }
  if (ie4) {
  	 if (event.keyCode == &quot;72&quot;)
	 {
	  alert(&quot;your Help Box here&quot;);
	 }
  }
}
document.onkeydown = keyDown
if (ns4) document.captureEvents(Event.KEYDOWN)
//-->
</SCRIPT>
BTY, nice function SeAL

You could use the keycode for the F1 key, but it will still open up the Help for IE or Netscape, so you would be safest to use a neutral key.
 
LOL, I didn't think of that. Hmm, F2 dosn't seem to have anything associated with it.

Interesting Fact: The Print Scrn Key is the only key that won't return a value.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top