Forum Moderators: open

Message Too Old, No Replies

Intercepting Keys and Firefox Reloading

         

ocon

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

10+ Year Member Top Contributors Of The Month



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>

lostdreamer

9:08 am on Aug 29, 2011 (gmt 0)

10+ Year Member



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.

ocon

12:25 pm on Aug 29, 2011 (gmt 0)

10+ Year Member Top Contributors Of The Month



Thank you so much, that worked great!