Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
I've been trying to get a script to somehow recognize the combination of two input keys, but have not figured out how to do it. As of now, each one of the arrows has its own function (not JS function(), but its own instruction), but I need that a combination of, say, the up and right arrow keys should be a different result than just the up or just the down.Any help or advice would be greatly appreciated,
Thanks,
Larry

Larry-
This one is a little more difficult...Get ready for the good stuff.It's impossible to press down two keys "at the same time", so no matter what you do, pressing down the two keys will fire two separate events. The trick is writing some code that will have the effect you want. I don't have a lot of interest in doing this myself, but I'll give you some ideas and help you as you figure it out yourself.
You're lucky...I had a nasty solution to this problem all worked out, then as I started writing this I thought of a much better one. Assign each arrow key a numeric value of a power of 2: 1 for up, 2 for right,4 for down, and 8 for left. Have a hidden input field whose initial value is 0. Each time a key is pressed, check to figure out which one it is, and add the corresponding value to the hidden field. Then schedule the form to be submitted at some time in the future (500ms, for example) by using the window.setTimeout function. You will need to write a small function that submits the form. This will give you enough time to press any other keys and have their value added to the hidden input field before the form is submitted.
Then in your PHP, instead of checking for which button they pressed, you just do a little math to figure out which value to output to the serial port. Depending on what values you actually want to use, you can make the javascript match them and save a lot of code.
Good luck,
-SN

Thanks, I think I'll try something along those lines. Thing is, if someone is, say, pressing the up arrow key, and then after a few seconds, presses the right one as well- if I use the timeout feature, then it probably wouldnt register it, I would assume. The general idea sounds good- to assign a 'next step' rather than have the keys trigger the action. Maybe just simply using values without the timeout, and then 'onkeyup', the values would be reset...
Thanks for your help (you also helped me out get this thing started, in an earlier thread...),
Larry

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |