Skip to content
/ input.js Public

Simple javascript game library that abstracts away how you handle mouse and keyboard inputs.

Notifications You must be signed in to change notification settings

reu/input.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Input.js

Input.js is a simple game library that abstracts away how you handle mouse and keyboard inputs. As games oftenly run in a infinite loop, trying to use a event based API (the one that Javascript provides) is always hard and unrealiable. This library just stores all the events that happens during each frame of your game loop, so it can be asked on your next update cycle.

Although it has no external dependencies (like jQuery for instance), it will return a Vector object instead of a hash to represent the mouse position if you are using the Vector.js library.

Usage

<script src="input.js" language="text/javascript"></script>
<script language="text/javascript">
  var mouse = new Mouse();
  var keyboard = new Keyboard();

  setInterval(function(){
    console.log("Mouse position: (" + mouse.position.x + ", " + mouse.position.y + ")");
    console.log("Previous position: (" + mouse.oldPosition.x + ", " + mouse.oldPosition.y + ")");

    if (mouse.isPressed) {
      console.log("Mouse is pressed!");
    }

    if (keyboard.isKeyPressed(Keyboard.KEYS.SHIFT)) {
      console.log("Looks like you are holding your shift key.");
    }

    for (keyCode in keyboard.keysPressed) {
      console.log("Also, you are pressing " + keyboard.keysPressed[keyCode]);
    }
  }, 1000 / 60);
</script>

About

Simple javascript game library that abstracts away how you handle mouse and keyboard inputs.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published