Disabling F1, F3 etc. with Javascript

Need technical advice on coding your web pages? Covers HTML, JavaScript, CSS, and some server side technologies. Also the issue of some webpages not displayed well.

Moderator: Don_HH2K

Disabling F1, F3 etc. with Javascript

Postby Rob Dixon » Fri 19 Nov, 2004 4:23 am

I tried this question on a Javascript forum but did not get any constructive responses. I wonder if anyone here knows the answer.

I am web enabling existing neural database applications and my users want to operate them as they do at present but over the Internet using a browser, without using a terminal emulator.


I am writing Javascript to capture all function keys for Netscape, IE, Firefox, etc.. I think that I know how to do this, but have a problem with F1, F3 F7, etc., as although I can capture them, I do not know how to stop them from starting a browser function. Is there a way of preventing this?

Many thanks

Rob Dixon
UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)
Rob Dixon
junior member
junior member
 
Posts: 10
Joined: Thu 21 Aug, 2003 5:43 am
Location: UK

Re: Disabling F1, F3 etc. with Javascript

Postby Antony » Fri 19 Nov, 2004 4:30 am

Rob Dixon wrote:I am writing Javascript to capture all function keys for Netscape, IE, Firefox, etc.. I think that I know how to do this, but have a problem with F1, F3 F7, etc., as although I can capture them, I do not know how to stop them from starting a browser function. Is there a way of preventing this?
What would F1, F3, F7 keys do? What browser functions?

You can try to fire up a dialogue telling your users those keys were disabled while connecting to your server. That is assigning an event after capturing those keys.
UserAgent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/125.5.5 (KHTML, like Gecko) Safari/125.11
User avatar
Antony
diamond member
diamond member
 
Posts: 14509
Joined: Tue 18 Jun, 2002 11:36 pm
Location: Sydney, Australia

Postby Rob Dixon » Fri 19 Nov, 2004 5:37 am

Many thanks for your response

Well, for instance, F1 would provide access to the application help rather than browser help, F3 would jump back several pages in one go (as it does in the present application), rather than provide a search facility within the page , F7 gives access to the database audit trail with details of who last updated each piece of data and when, etc..

Apart from a few static home pages, all the HTML and Javscript on my sites is generated on the fly - there is no stored HTML and there are no large pages so, for instance, no searching of individual pages is normally required, but, if it is, there is always Ctrl & F.


You said
<snip>
You can try to fire up a dialogue telling your users those keys were disabled while connecting to your server.
</snip>

This I understand but I did not quite understand

<snip>
That is assigning an event after capturing those keys.
</snip>

How would I do this?

Many thanks

Rob Dixon
UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)
Rob Dixon
junior member
junior member
 
Posts: 10
Joined: Thu 21 Aug, 2003 5:43 am
Location: UK

Postby Antony » Fri 19 Nov, 2004 5:45 am

Rob Dixon wrote:<snip>
You can try to fire up a dialogue telling your users those keys were disabled while connecting to your server.
</snip>

This I understand but I did not quite understand

<snip>
That is assigning an event after capturing those keys.
</snip>

How would I do this?
It's something like disabling context click (right clicking). You pop out a JavaScript dialogue instead of letting the regular context click (right click) working.

So instead of capturing right mouse click, you can catch those function keys.

Please note that I've never trying to disable users' ability of escaping my sites, although I can see many legit usage, please keep in mind that all browsers support those, and there are ways to bypass those.

If I remember correctly, in Netscape Communicator, you can disable a number of things with signed scripts. I can't recall if there's signed script feature for Netscape 6+ or Mozilla.
UserAgent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/125.5.5 (KHTML, like Gecko) Safari/125.11
User avatar
Antony
diamond member
diamond member
 
Posts: 14509
Joined: Tue 18 Jun, 2002 11:36 pm
Location: Sydney, Australia

Postby akbash » Fri 19 Nov, 2004 12:40 pm

Antony wrote:Please note that I've never trying to disable users' ability of escaping my sites...
AFAIK a web app can't prevent a user from leaving the site. But it can make it more difficult. Preventing the browser from acting on an event is part of the standard DOM toolkit.
Code: Select all
  if (event.keyCode == event.DOM_VK_F1) {
    // no help for you
    event.stopPropagation()
    event.preventDefault()
  }
See the DOM 2 Event Flow spec. Of course it's something completely different in IE.
UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041116 Firefox/1.0
akbash
silver member
silver member
 
Posts: 364
Joined: Mon 09 Feb, 2004 9:13 pm

Postby Rob Dixon » Sat 20 Nov, 2004 10:23 am

Thank you both akbash & Antony.

akbash. I couldn't get

event.stopPropagation()
event.preventDefault()
to work but maybe I was putting them in the wrong place. My code was
_______________
document.onkeydown = keyHit; if (document.layers) {document.captureEvents(Event.KEYDOWN); F03 = 114; F05 = 116; F12 = 123;} else {F03 = 114; F05 = 116; F12 = 123;}
function keyHit(evt) {if (evt) {thisKey = evt.which; event.stopPropagation(); event.preventDefault();} else {thisKey = window.event.keyCode;event.stopPropagation(); event.preventDefault();}
if (thisKey == F03) {var buttonname = session + "/BUTTON.999-999=*3"; document.getElementsByName(buttonname)[0].click();}}


Antony You mention of right clicking reminded me that I knew of some code to prevent that (although I hadn't used it) and I tried to adapt it to my needs.

I tried the following

_____________

document.onkeydown = keyHit; if (document.layers) {document.captureEvents(Event.KEYDOWN); F03 = 114; F05 = 116; F12 = 123;} else {F03 = 114; F05 = 116; F12 = 123;}
function keyHit(evt) {if (evt) {thisKey = evt.which;} else {thisKey = window.event.keyCode;}
if (thisKey == F03) {var buttonname = session + "/BUTTON.999-999=*3"; document.getElementsByName(buttonname)[0].click();return false}}

_______________
(I have left out the code for F5 and F12 and much else, including the code for the button)
__________________-
This causes the button to be "clicked" correctly in both NS and IE but the "return false", which I believe should cancel the key event, does not work, so in both browsers a search facility is opened which is what I am trying to prevent.

My knowledge of Javascript is strictly limited and I cannot see what is wrong.

Have either of you any ideas?

Many thanks

Rob Dixon
UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)
Rob Dixon
junior member
junior member
 
Posts: 10
Joined: Thu 21 Aug, 2003 5:43 am
Location: UK

Postby akbash » Sat 20 Nov, 2004 2:39 pm

Code: Select all
<html><body>
<script>
document.onkeypress = keyHit
function keyHit(event) {
  if (event.keyCode == event.DOM_VK_F1) {
    // cancel browser app event handler for F1 key
    event.stopPropagation()
    event.preventDefault()
  }
}
</script>
</body></html>
Again, that's only for W3-compliant browsers.
UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041116 Firefox/1.0
akbash
silver member
silver member
 
Posts: 364
Joined: Mon 09 Feb, 2004 9:13 pm

Postby Rob Dixon » Sun 21 Nov, 2004 12:20 pm

akbash

That was really helpful and I have now got several function keys working in NS, with their default action disabled. This is a signifcant step forward for me and I am most grateful.

Unfortunately, the changes have made my code unuseable in IE as, if I touch any key, IE complains that keyCode is null or not an object A snip of my code is
______________________________

document.onkeypress = keyHit;
if (document.layers) {document.captureEvents(Event.KEYDOWN); F01=112;F03=114;F05=116;F12=123;} else {F01=112;F03=114;F05=116;F12=123;}
function keyHit(event)
{if (event) {thisKey = event.which;} else {thisKey = window.event.keyCode;}
if (event.keyCode == event.DOM_VK_F1) {var buttonname = session + "/BUTTON.999-999=*1"; document.getElementsByName(buttonname)[0].click(); event.stopPropagation(); event.preventDefault();}
}
_________________

I have left out the code for F3, F5, etc. and the code for the buttons.

I am capturing the keys for both NS and IE in "thisKey" and I tried using

"thisKey == F01" instead of

"event.keyCode == event.DOM_VK_F1"

without success.

I need to get it working in IE as I cannot choose which browser will be chosen by my users.

Do you know if it is possible in IE?

Many thanks again for your help

Rob Dixon

PS: BTW, how do you do quotes and code examples on this forum?
UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)
Rob Dixon
junior member
junior member
 
Posts: 10
Joined: Thu 21 Aug, 2003 5:43 am
Location: UK

Postby akbash » Mon 22 Nov, 2004 12:27 pm

Yes I believe this is also possible in IE. But I'm just a Mozilla muse. As I mentioned earlier, IE will require something completely different. So will Navigator 4. Of Opera and Safari I know few details but I'd be surprised if code written for the first three wouldn't also work in those two. You don't want to replace your previous code with the Mozilla snippet I supplied; you just want to work the W3/Mozilla stuff in to the proper place of the multi-browser code you've mentioned in earlier posts in this thread.

Quotes and nicely formatted code can be had using BBCode tags. While in the Add Reply page of this forum click the BBCode link in the left margin for details. The Preview button is also handy for proof-reading.
UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041116 Firefox/1.0
akbash
silver member
silver member
 
Posts: 364
Joined: Mon 09 Feb, 2004 9:13 pm

Postby Rob Dixon » Mon 22 Nov, 2004 1:24 pm

akbash

Many thanks. What you have already given me is invaluable.

Best wishes

Rob Dixon
UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax)
Rob Dixon
junior member
junior member
 
Posts: 10
Joined: Thu 21 Aug, 2003 5:43 am
Location: UK


Return to Web Design and Page Coding

Who is online

Registered users: Google [Bot], Yahoo [Bot]