I am looking for a library that would normally handle keystrokes. For example, I press the A key and then I press the W key and the character must move to the left and up. Maybe I initially did not correctly try to move the character. The script works like this, I press it to the left, it moves to the left, I press up and it stops, then if there is an A + W script, then it moves up with a delay. And it turns out if on the contrary I run up and then press to the left, then there should be a W + A script. When I press W + D there is no movement at all. Here is a sample code:

var renderer = PIXI.autoDetectRenderer(800, 600); $(document).ready(function() { $('body').append(renderer.view); $('body').click(function(e) { player.target.x = e.clientX; player.target.y = e.clientY; player.distance(); player.active_move = 1; }); $('body').mousemove(function(e) { player.target.x = e.clientX; player.target.y = e.clientY; player.loock(); }); $(document).keyboard( 'a', function(e, bind) { // player.body.position.x -= 1; player.left = 1; } ); $(document).keyboard( 's', function(e, bind) { player.down = 1; } ); $(document).keyboard( 'w', function(e, bind) { player.top = 1; } ); $(document).keyboard( 'd', function(e, bind) { player.right = 1; } ); $(document).keyboard( 'a+s', function(e, bind) { player.down = 1; player.left = 1; } ); $(document).keyboard( 's+d', function(e, bind) { player.right = 1; player.down = 1; } ); $(document).keyboard( 'w+a', function(e, bind) { player.top = 1; player.left = 1; } ); $(document).keyboard( 'w+d', function(e, bind) { player.top = 1; player.down = 1; } ); }); // create the root of the scene graph var stage = new PIXI.Container(); PIXI.loader .add('frames/walck.json') .load(onAssetsLoaded); var player; function onAssetsLoaded() { // create an array to store the textures player = new player(); // start animating requestAnimationFrame(animate); } function player() { this.hp = 100; this.level = 1; this.vector_to_target = { x: 100, y: 100 }; this.target = { x: 100, y: 100 }; this.speed = 1; this.body = null; this.texture = null; this.textures = []; this.active_move = 0; this.distance = function() { return Math.sqrt(Math.pow(this.target.x - this.body.position.x, 2) + Math.pow(this.target.y - this.body.position.y, 2)); } this.loock = function() { //Поворот this.vector_to_target.x = this.target.x - this.body.position.x; this.vector_to_target.y = this.target.y - this.body.position.y; this.body.rotation = Math.atan2(this.vector_to_target.y, this.vector_to_target.x); } this.move = function() { //Поворот this.vector_to_target.x = this.target.x - this.body.position.x; this.vector_to_target.y = this.target.y - this.body.position.y; this.body.rotation = Math.atan2(this.vector_to_target.y, this.vector_to_target.x); //Движение this.modul_target = Math.sqrt(this.vector_to_target.x * this.vector_to_target.x + this.vector_to_target.y * this.vector_to_target.y); this.body.position.x += this.speed * this.vector_to_target.x / this.modul_target; this.body.position.y += this.speed * this.vector_to_target.y / this.modul_target; } this.test_move = function(nav) { switch (nav) { case 'left': this.body.position.x -= 10; this.left = 0; break; case 'right': this.body.position.x += 10; this.right = 0; break; case 'top': this.body.position.y -= 10; this.top = 0; break; case 'down': this.body.position.y += 10; this.down = 0; break; } } var i; for (i = 0; i < 12; i++) { this.texture = PIXI.Texture.fromFrame('walck' + i + '.png'); this.textures.push(this.texture); } this.body = new PIXI.extras.MovieClip(this.textures); this.body.anchor = { x: 0.5, y: 0.5 }; this.body.position = { x: 200, y: 200 }; this.body.animationSpeed = 0.4; this.body.play(); stage.addChild(this.body); setInterval(this.animate.bind(this), 10); } player.prototype.animate = function() { console.log(this.target); console.log(this.distance()); if (this.distance() < 10) this.active_move = 0; if (this.right) { this.test_move('right'); } if (this.left) { this.test_move('left'); } if (this.top) { this.test_move('top'); } if (this.down) { this.test_move('down'); } } function animate() { renderer.render(stage); requestAnimationFrame(animate); } 
  • @shatal is not true. - Qwertiy
  • Where can I look at it? By code I can not guess what is wrong there. And the keys need to handle and save the flags yourself. And the whole movement is on a timer. - Qwertiy
  • @shatal, what nonsense? if the arms are straight, then html5 is very good for game devs. Quake III , Plus 360 Degrees , Trigger Rally . Still need examples? - vihtor
  • Of course, I’m not an expert in game development, but it seems to me that you need to listen separately for keystrokes and its release. In this case, the player must have a velocity vector allowed velocity . Initially, it must be equal to [0, 0] (that is, we stand still). So if you press, for example, the key A , then add to the velocity vector [-1, 0] . With the release of A , we subtract this vector, in the same way with the other keys. In this case, if you press A and W to velocity add the vector [-1, 0] + [0, 1] = [-1, 1] . - vihtor
  • @vihtor: for example, in Unity on each frame you can simply check if a key is pressed. This is enough to work with a long press, as well as to detect the moment of pressing and release. - Nick Volynkin

2 answers 2

You do not need a "library that would normally handle keystrokes," but rather handle these keystrokes normally.

there is the current position of the object position = [100, 100] , there is its velocity vector velocity = [0, 0] . press just change the velocity vector.

Here is a working example.

  • Thank you, sensei =) ideally, I will finish the game, I will write thanks where the thread is in the corner - Serge Esmanovich
  • Everything turned out even better than expected =) smartcook.info/testgame/test1.html - Serge Esmanovich
  • Not bad! Now you can immediately replace that diagonally the player moves faster than along the axes. And, for example, in Crimsonland, a player can move and look in different directions, according to this they divided the player into the upper part (head, shoulders and arms) and lower (legs), which rotate independently - vihtor
  • Yes, I also noticed, I plan to also divide will consist of 2 movie clips, I just plan on making the basis on nodejs and then start drawing and finishing animations. - Serge Esmanovich

Please see this library. Maybe it will work: keymaster 1. connect the library 2. use the necessary key combinations key('a+w', function(){ alert('you pressed a!') });

  • 3
    Welcome to StackOverflow. Thanks for your reply! There is such a thing: the answers from one link are usually not considered to be good answers. They usually come to questions in which the author asks for some software for him (as in this case), which is also not approved. Therefore, just links are published in the comments. - Nick Volynkin
  • 3
    If you can supplement the answer with an example of how to use this library in its code, the author would be very cool. - Nick Volynkin
  • It would be a good example to press 2 keys, but not CTRL, Shift, but for example A + W - Serge Esmanovich
  • accepted, thank you. The next time I promise to take into account the rules, and write detailed comments and answers. - spectre_it