rezoner / CanvasQuery

Canvas for 2d gamedevelopers. Out of box canvas, keyboard, mouse, events.
http://canvasquery.com
586 stars 52 forks source link

playground.Keyboard.prototype.keypress Event is an empty Function. #45

Closed RoryDuncan closed 9 years ago

RoryDuncan commented 9 years ago

I noticed that the there isn't functionality for the Keyboard.prototype.keypress event, is this intentional/incomplete?

kozie commented 9 years ago

Offtopic:

Coffee, code and Cats ^_^ well whattaya know :)

rezoner commented 9 years ago

To be honest - I've never noticed it's empty. I've been using Playground (or its derivatives) for 3 years now - and I never been tempted to use keypress. I am willing to implement it if you tell me what do you expect it to do :) If it's obsolete I will remove it within next iteration.

Also I will be stressing this in every thread - I am soon splitting CanvasQuery and Playground into two different repos and domains. Playground will get a cleaner structure for source code and ability to bind renderer of your choice.

RoryDuncan commented 9 years ago

I imagine it would just have the same functionality as 'keydown' or 'keyup', so nothing special to it's circumstance. The keypress event is valuable for when the user holds a key, as that's what brought me here.

    if (e.which >= 48 && e.which <= 90) var keyName = String.fromCharCode(e.which).toLowerCase();
    else var keyName = this.keycodes[e.which];

    if (this.keys[keyName]) return;

    this.keypressEvent.key = keyName;
    this.keypressEvent.original = e;

    this.keys[keyName] = true;

    this.trigger("keypress", this.keypressEvent);

    if (this.preventDefault && document.activeElement === document.body) {
      e.returnValue = false;
      e.keyCode = 0;
      e.preventDefault();
      e.stopPropagation();
    }
rezoner commented 9 years ago

The only real life usage for keypress I can think of is when implementing a canvas based GUI (input, edit) that behaves like just any other system wise input element.

Since the delay and interval is configurable by system/user it cannot be trusted gameplay wise.

TL;DR; I will add the functionality - all in all it's just pushing the event forward :)

RoryDuncan commented 9 years ago

Hmm, I didn't realize that those were system configured. In my case that changes everything, as I was meaning to use it for gameplay.

Does this mean to emulate a hold event someone should create their own handler based on keydown and keyup events -and is a 'keyhold' event something that would be valuable to have in playground? Backtrack me, if i missed something important.. :grinning:

rezoner commented 9 years ago

Let's assume we are implementing a shooting functionality for player.

Overly simplified approach should look like this:

keydown: function() {
  this.player.shooting = true;
},

keyup: function() {
  this.player.shooting = false;
}
Player.prototype.step = function(delta) { 

  if(this.shooting && this.cooldown <= 0) {
    this.fire();
    this.cooldown = this.baseCooldown;
  }

  this.cooldown -= delta;

}

If you feel like lazy - you can even skip event handling and just check if desired key is pressed:

Player.prototype.step = function(delta) { 

  if(app.keyboard.keys.space && this.cooldown <= 0) 

  /* ... */

}
RoryDuncan commented 9 years ago

Ah, I see. Your second example seems exceptionally convenient for that purpose. Thanks for the help :)