0

I have a script written in javascript which does something to a drone that I have. As of now I run that script in my shell like this node foo.js and the script runs till I abort it using control C. But now I want to be able to run that script and have it listen for keyboard events that I give it (such as ENTER, up/down arrow key, spacebar), and depending on the event it performs a specific function. And when I am done I should be still able to press control C to stop the program. It would be awesome if someone could help me. I am still in highschool and very new to programming.

Here is the script for reference:

var arDrone = require('ar-drone');
var client  = arDrone.createClient();

client.takeoff();

client

  .after(10000, function() {
    this.stop();
    this.land();
  });
chepner
  • 497,756
  • 71
  • 530
  • 681
Ravin Sardal
  • 717
  • 1
  • 13
  • 23
  • 2
    Have you taken a look at this: http://stackoverflow.com/questions/5006821/nodejs-how-to-read-keystrokes-from-stdin – Morne May 31 '14 at 17:23

1 Answers1

0

Each keyboard key has a special key code (i.e. f = 70). You can find that here:

http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes

I would use jQuery to trigger these events, the first is any keystroke:

$(function() {
    $('client').keydown();
    $('client').keypress();
    $('client').keyup();
});

The second is specific keystrokes:

$function() {
    var e = $.Event('keypress');
    e.which = 39; // right arrow
    $('client').trigger(e);
});

So then you would have a function called trigger that checked for specific numbers, and you would move your drone accordingly.

tcase360
  • 366
  • 3
  • 11