2

As the title says, I have tried THREEx and Stemkovskis standalone KeyboardState.js , and neither of them seems to update properly.

This is my code:

m_vKeyboard = new THREEx.KeyboardState();
// m_vKeyboard.update(); // if using stemkovskis
if (m_vKeyboard.pressed("F")) {
    alert("And now it is always true!");
}

you click the F key once, release it; alert window pops up, click OK, it pops up again for all eternity. How come?

Yenza
  • 440
  • 5
  • 19

3 Answers3

1

I'm wondering if it has something to do with alert() being a blocking call. Using the code below gives me your same issue. If I comment out the alert() and un-comment the console.log() it seems to work fine. However, I'm not sure if that helps your issue.

    var m_vKeyboard = new THREEx.KeyboardState();

    setInterval(function () {
        var key = "F";
        var pressed = m_vKeyboard.pressed(key);
        alert("And now it is always true!");
        //console.log("key", key, "pressed", pressed);
    }, 100);
JoelCool
  • 333
  • 2
  • 4
  • 15
  • Well yes, commenting it solves the "always" true problem. However, I want to use promt to have a user insert a INT value to use in an algorithm. Perhaps there is some other way? – Yenza Aug 25 '14 at 05:21
  • Yeah, using alert(), prompt(), and confirm() are problematic. Can you not use jQuery or other library for prompting so that you aren't calling blocking type functions? – JoelCool Aug 26 '14 at 16:12
  • I problem is that I can't have the rest of the webpage rolling in the background so a blocking call is needed I'm afraid :/ – Yenza Aug 27 '14 at 05:10
1

Many browsers repeat keydown. Read more here and here (ctrl+f : auto-repeat).

Here's a proposed solution for your specific problem :

A. when keydown store its state as true in some array and make it false on keyup.

wasPressed['F'] = true;  //on keydown
wasPressed['F'] = false; //on keyup

B. when checking for next keydown check its state as well.

if (m_vKeyboard.pressed("F") && !wasPressed['F'])

Find full implementation : Here

UPDATE

var wasPressed  = {};
if( keyboard.pressed('F')  && !wasPressed['f'] ){
            alert("F was pressed");
            prompt("Enter data : ");
            wasPressed['f'] = true;         
        }

UPDATE 2

keyboard.domElement.addEventListener('keydown', function(event){
            wasPressed  = {};
    })
Community
  • 1
  • 1
Himanshu Tyagi
  • 5,201
  • 1
  • 23
  • 43
  • I haven't tried your solution but I do not think it will work. Since when I click on 'F', I use prompt which locks the code (aka when I release 'F' it will not trigger the onKeyUp) therefor its always true. I've looked at your code, how does it check if a button on the keyboard is currently pressed? – Yenza Aug 25 '14 at 11:41
  • I tried with prompt(see update) and it works just fine. – Himanshu Tyagi Aug 25 '14 at 12:02
  • Well of course it works, you set the boolean to true (that's how I have solved it right now). What happends if you click on F a second time just after you closed the prompt? – Yenza Aug 25 '14 at 12:12
  • Works but feels like a cheap way to do it ;) Cheers though! Wanted a solution for this for a while now! – Yenza Aug 25 '14 at 12:38
  • I know! it really is a cheap workaround. But for a quick solution, it seems like the best! :) – Himanshu Tyagi Aug 25 '14 at 13:31
0

Just add this to the beginning of onKeyDown in KeyboardState.js:

if (event.repeat) return;
dpren
  • 1,225
  • 12
  • 18