Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overlapping key events

I am working on a little HTML/JavaScript/CSS3 project for fun. I'm basically trying to make a wheel that rolls around in the browser window. To control the wheel I'm using keyup and keydown events for the cursor keys (left and right turn the wheel and up and down roll it forward or backward).

I've got it working pretty well so far, but there are two major glitches. Say I want to roll the wheel forward and without stopping I want to turn it a little to the right, then I would keep the up key pressed and press the right cursor key. When I do this there's a pause in the movement before it registers both events and keeps rolling.

That's one of the problems, the main problem is that, once I've performed the previous action and then wheel is at a desirable angle, if I let go of the right cursor key the browser registers both keys as released and the wheel comes to a stand still. Here is a jsFiddle of what it looks like: http://jsfiddle.net/UKqwu/1/. I know the code is a mess but it's a work in progress/learning experience and I've only been programming for a month or so.

Anyways thanks for any help. It only works in Chrome at the moment as far is I know. Haven't really been bothered fixing compatibility issues at this stage.

like image 202
jrl589 Avatar asked Jul 29 '12 18:07

jrl589


People also ask

What are overlapping events in probability?

Overlapping events are events that have one or more outcomes in common.

What is event overlap?

Overlapping events are events that have outcomes in common. Then, determine whether the events are disjoint or overlapping. You can do this by drawing a Venn diagram that shows if any events are common to the two sets.

How do you find overlapping events?

The equation for determining the either/or probability of overlapping events is: P(A or B) = P(A) + P(B) - P(A and B). As you can see, you must subtract out the probability of the overlapping event to get the right answer.


1 Answers

So, what is happening is essentially a limitation built in by your operating system, but there is a simple work-around. First I'll explain the limitation, and then the work-around.

If you were to (in a text box) hold down the "j" button, first one "j" would appear, and then after a short delay many "j"s would appear "jjjjjjjjjjjjjjjjjjjj"

This is the same problem your experiencing. The event fires once, waits for a moment, and then fires many more times.

The solution, however is simple. Instead of having your wheel move when the events are fired... have it update constantly, and separately keep track of what keys are up or down.

The Key Handler would look something like this...

function KeyHandler() {
  this.left = false;
  this.right= false;
  ...
  function onKeyDown(e) {
    if (e.keyCode == 37) {
      this.left = true;
    }
    ...
  }
  function onKeyUp(e) {
    if (e.keyCode == 37) {
      this.left = false;
    }
    ...
  }
}

(you'd attach the key handler to the body or whatever element you wish)

Your wheel would have an update function that looked like...

wheel.update = function() {
  // the wheel is turning left
  if (wheel.keyhandler.left) {
    // make the appropriate adjustments
  }
}

and then the game would have something like this...

wheel = new Wheel;
setInterval(function() {
  wheel.update();
},100);

That way your wheel will always be updating based on the current state of the keys, and you wont have to rely on the limitations of events that are firing. :)

like image 101
zconnelly13 Avatar answered Sep 30 '22 12:09

zconnelly13