faqts : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

153 of 176 people (87%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

Can you trap function keys to prevent their native operations?

Apr 4th, 2008 19:39
ha mo, Darton Williams, Colin Fraser, David Coverley,


I found a way to do it, but it only works in IE and I've only tested it 
in IE5+. Netscape doesn't seem to return anything for function keys. If 
you trap the keyCode you want to disable and assign a new value to the 
it, you can do whatever you want with the function keys. The keycodes 
for them are 112 - 123. The only real use I've found for this is in a 
proprietary app I'm developing for IE, but I guess you could also annoy 
the heck out of some people with it.
document.onkeydown = function(){
if(window.event && window.event.keyCode == 116) 
        { // Capture and remap F5
	window.event.keyCode = 505;
  	}
if(window.event && window.event.keyCode == 505) 
        { // New action for F5
	alert('F5 key was pressed');
	return false; 
        // Must return false or the browser will refresh anyway
	}
}
http://www.businessian.com
http://www.computerstan.com
http://www.financestan.com
http://www.healthstan.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.technologystan.com
http://www.zobab.com
http://www.healthinhealth.com