Welcome to WebmasterWorld Guest from 3.80.4.76

Forum Moderators: open

Message Too Old, No Replies

Intercepting Keys and Firefox Reloading

     
11:56 pm on Aug 25, 2011 (gmt 0)

Full Member

5+ Year Member Top Contributors Of The Month

joined:Sept 30, 2009
posts:227
votes: 1


Hello,

I'm trying to associate different functions to different keys to create keyboard shortcuts for my online application. I will create a help page to share these keyboard shortcuts with my users.

My problem is, I'm trying to associate the key "t" to toggle the screen. Unfortunately, it seems whenever the user refreshes the page in Firefox using the F5 button, the browser seems to simulate pressing the "t" key after the page reloads. I'm wondering if there is some problem with my code, or some way to have Firefox not do this. My other keyboard shortcuts work just fine.

<script>
function keyboard(e){
var code;
if(!e) var e = window.event;
if(e.keyCode) code = e.keyCode;
else if(e.which) code = e.which;
var key = String.fromCharCode(code).toLowerCase();

if(key=="l"){alert("L pressed");}
else if(key=="t"){alert("T pressed");}
else if(key=="w"){alert("W pressed");}
}

window.onkeypress = keyboard;
</script>
9:08 am on Aug 29, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:July 8, 2010
posts: 114
votes: 0


replace

if(e.keyCode) code = e.keyCode;
else if(e.which) code = e.which;


With

if(e.charCode) code = e.charCode;
else if(e.which) code = e.which;


e.charCode is the normalized version of keyCode and Charcode.
With it, F5 will not be key 116 (which is T) The rest of your code will still work.
12:25 pm on Aug 29, 2011 (gmt 0)

Full Member

5+ Year Member Top Contributors Of The Month

joined:Sept 30, 2009
posts:227
votes: 1


Thank you so much, that worked great!
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members