Jump to content

How to handle keyboard events inside main loop?


splash27
 Share

Recommended Posts

I suppose that using 

document.addEventListener("keydown", function (event) {
    //detect key and instantly do something
});

is not a good way, because it works outside the game loop. This might cause synchronization and performance issues in some cases.

So far as I know, there are a keyboard event queue in most games. When user press button the corresponding event being placed into the queue. The the callback actually fired from game loop and queue reduces.

Is there such mechanism in BabylonJS? How to attach my keyboard handling to it?

Link to comment
Share on other sites

https://www.babylonjs-playground.com/#1MK1Y4#13

Hiya S... good to see you again.  Would THAT method of keypress-checking (our ActionManager) smell any fresher?

I haven't studied the underbelly of actionManager event-wrangling, but if I know Deltakosh and The Core Boys... they have done their best to smoooooothly integrate into browser event system.  Them guys are HOT! 

I think... if you can use the ActionManager key-testing 'method', you'll be fartin' thru silk.  (in good hands with good keypress listening).

Now, I hope I was anywhat on-subject.  :D

As far as buffering-up held/fast keypress events... boy... let's BOTH ask for information about that.  Those answers might be at browser, OS, or hardware levels.

http://unixpapa.com/js/key.html

Link to comment
Share on other sites

ActionManager is a good solution,

But, I'm not seeing any issues using eventListener like you did, internally, the actionManager also uses those ( all user-input does )
you could add it to the canvas element instead,
so they are only fired while canvas has focus.

If you want a 'queue', simply write one using an array :), but i doubt it is necessary.

Link to comment
Share on other sites

I wonder how many entry points for keyboard handling in BabylonJS: ActionManager, KeyboardObservable, camera's keys and checkInputs, default JS addEventListener. What is the best way to implement in-game controls (firing, reloading, gun switching etc.)?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...