blackcell Posted May 9, 2011 Share Posted May 9, 2011 I am trying to figure out how to detect keys like the one at the bottom of this page: http://www.quirksmode.org/js/keys.html#t10 I am incorporating a programmable foot pedal to one of our web based applications to page forward and back. I have a basic knowledge of JS and am wondering the best way to do this. Do I need to go into a while loop and how does this script detect a key without setting focus to any particular field? Quote Link to comment https://forums.phpfreaks.com/topic/235952-javascript-detect-key/ Share on other sites More sharing options...
Adam Posted May 11, 2011 Share Posted May 11, 2011 You need to attach the key(press|up|down) event to the window, and use the event object to determine the key pressed. Certain keys however are only catchable for certain events. What keys are you wanting to capture -- which will you programme the pedal to? Quote Link to comment https://forums.phpfreaks.com/topic/235952-javascript-detect-key/#findComment-1213832 Share on other sites More sharing options...
blackcell Posted May 11, 2011 Author Share Posted May 11, 2011 Well I have it working. I have a pedal send a unlikely macro to the client(comp) and an autohotkey script catches that key sequence. At the time the script sets focus to my browser, and send } for forward and { for back. The only issue I have is that after one key detection the script stops. I need it to loop in case a wrong key is accidentally pressed on the keyboard while focus is on browser. This shouldn't be a problem but I try to foresee problems like this beforehand and implement a fix. function WatchKeys(e) { var code; var Part = $("#Part").val(); var Task = $("#Task").val(); var Page = $("#Page").val(); if (!e) var e = window.event; if (e.keyCode) code = e.keyCode; else if (e.which) code = e.which; var character = String.fromCharCode(code); if(character == '}'){ //alert('Forward'); Page++; window.location.replace("index.php?part="+Part+"&task="+Task+"&page="+Page); }else if(character == '{'){ //alert('Back'); Page--; window.location.replace("index.php?part="+Part+"&task="+Task+"&page="+Page); }else{ //Other key pressed and it needs to refresh to reinitialize the script. (only works once) window.location.replace("index.php?part="+Part+"&task="+Task+"&page="+Page); } } I am using jquery for the main content retrieval which is irrelevant. It explains some of the .val() stuff. Quote Link to comment https://forums.phpfreaks.com/topic/235952-javascript-detect-key/#findComment-1213846 Share on other sites More sharing options...
Adam Posted May 11, 2011 Share Posted May 11, 2011 How are you binding the key event to the window? When you say you have to refresh to get it to work, does simply clicking the window make it work again? I ran into a little issue the other week in FF4 where I was using alerts to debug, but they were taking focus away from the window and the event wasn't firing. I notice there's some alerts (albeit commented out presently) in your code... If not I would guess it's something to do with the macro / "autohotkey" script you're using, as opposed to the JavaScript - but show us how you're binding it. Quote Link to comment https://forums.phpfreaks.com/topic/235952-javascript-detect-key/#findComment-1213884 Share on other sites More sharing options...
blackcell Posted May 12, 2011 Author Share Posted May 12, 2011 <body onkeypress="WatchKeys(event);"> Edit: Now it seems to be working....how strange. I will try to get more info on why it isn't working. This is acting crazy. Quote Link to comment https://forums.phpfreaks.com/topic/235952-javascript-detect-key/#findComment-1214465 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.