Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/235952-javascript-detect-key/
Share on other sites

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?

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.

 

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.