Watcher.prototype.observeStdin = function (stdin) { var self = this; if (stdin.isTTY) { keypress(stdin); stdin.setRawMode(true); } stdin.resume(); stdin.setEncoding('utf8'); stdin.on('keypress', function (char, key) { if (key.ctrl && key.name === 'c') { process.exit(2); } if (char !== 'r') { return; } // Cancel the debouncer, it might rerun specific tests whereas *all* tests // need to be rerun. self.debouncer.cancel(); self.busy.then(function () { // Cancel the debouncer again, it might have restarted while waiting for // the busy promise to fulfil. self.debouncer.cancel(); self.rerunAll(); }); }); };
return function(done){ var buf = ''; keypress(process.stdin); process.stdin.setRawMode(true); process.stdout.write(msg); process.stdin.on('keypress', function(c, key){ if (key && 'return' == key.name) { console.log(); process.stdin.pause(); process.stdin.removeAllListeners('keypress'); process.stdin.setRawMode(false); if (!buf.trim().length) return exports.password(msg, mask)(done); done(null, buf); return; } if (key && key.ctrl && 'c' == key.name) { console.log('%s', buf); process.exit(); } if (key && key.name === 'backspace') { if (buf.length > 0) { buf = buf.slice(0, -1); } } else { process.stdout.write(mask); buf += c; } }).resume(); }
pinio.on('ready', function(board) { var motors = require('./motorconfig').getMotors(board) var testMotor = motors[1] var keypress = require('keypress'); // make `process.stdin` begin emitting "keypress" events keypress(process.stdin); process.stdin.setRawMode(true); // listen for the "keypress" event process.stdin.on('keypress', function (ch, key) { console.log('Key: ' + key.name); if (key && key.ctrl && key.name == 'c') { console.log("Exiting") process.exit() } if (key.name === 'left') { console.log('left!') testMotor.ccw() setTimeout(function() { testMotor.stop() }, 200) } else if (key.name === 'right') { testMotor.cw() setTimeout(function() { testMotor.stop() }, 200) } }); })
_listenKeyPress () { // Listen commands keypress(process.stdin); process.stdin.on('keypress', (ch, key) => { if (this.lockKeyPress) return null; this.lockKeyPress = true; setTimeout(() => { this.lockKeyPress = false; }, LOCK_KEY_PRESS_TIMEOUT); if (key && key.ctrl) { switch (key.name) { case 's': return this._stop(); case 'r': return this._restart(); case 'c': return this._exit(); case 'w': return this._toggleWatching(); } } return null; }); }
exports.keycontrol = function keycontrol(){ keypress(process.stdin); function turnUpDegrees(degrees){ stopTime = Math.floor(degrees * 22.3) setTimeout(function(){ThunderConnector.command('up');},0); setTimeout(function(){ThunderConnector.command('stop');},stopTime); } function turnLeftDegrees(degrees){ stopTime = Math.floor(degrees * 22.3) setTimeout(function(){ThunderConnector.command('left');},0); setTimeout(function(){ThunderConnector.command('stop');},stopTime); } function turnDownDegrees(degrees){ stopTime = Math.floor(degrees * 22.3) setTimeout(function(){ThunderConnector.command('down');},0); setTimeout(function(){ThunderConnector.command('stop');},stopTime); } function turnRightDegrees(degrees){ stopTime = Math.floor(degrees * 22.3) setTimeout(function(){ThunderConnector.command('right');},0); setTimeout(function(){ThunderConnector.command('stop');},stopTime); } // listen for the "keypress" event process.stdin.on('keypress', function (ch, key) { console.log('got "keypress"', key); if (key.name == "w") { turnUpDegrees(2.3); // if (key.name == "shift" && key.name == "w") // { // turnUpDegrees(1); // } } if (key.name == "a") { turnLeftDegrees(2.3); } if (key.name == "s") { turnDownDegrees(2.3); } if (key.name == "d") { turnRightDegrees(2.3); } if (key.name == "f") { // console.log("Fire"); setTimeout(function(){ThunderConnector.command('fire');},0); setTimeout(function(){ThunderConnector.command('stop');},10000); } if (key && key.ctrl && key.name == 'c') { process.stdin.pause(); } }); if (typeof process.stdin.setRawMode == 'function') { process.stdin.setRawMode(true); } else { tty.setRawMode(true); } process.stdin.resume(); }
bot.on('ready', function () { var left = bot.attach(new FootballBot.Motor({pins: {pwm: 9, dir: 7}})) var right = bot.attach(new FootballBot.Motor({pins: {pwm: 10, dir: 8}})) var motorsStarted = false function startMotors () { if (!motorsStarted) { left.start() right.start() motorsStarted = true } } keypress(process.stdin) process.stdin.on('keypress', function (ch, key) { if (!key) return; if (key.name == 'up') { left.forward() right.forward() startMotors() } else if (key.name == 'down') { left.reverse() right.reverse() startMotors() } else if (key.ctrl && key.name == 'c') { process.exit() } }) process.stdin.setRawMode(true) process.stdin.resume() })
Replcator.prototype.secret = function(question, callback) { var sshh; function goRaw(bool) { (typeof process.stdin.setRawMode !== 'undefined') ? process.stdin.setRawMode(bool) : tty.setRawMode(bool); } keypress(process.stdin); goRaw(true); process.stdin.setEncoding('utf8'); process.stdout.write(question); process.stdin.on('keypress', function (ch, key) { if (key && key.ctrl && key.name === 'c') { process.stdin.removeAllListeners('keypress'); goRaw(false); process.stdin.pause(); } else if (key && key.name === 'enter') { process.stdin.removeAllListeners('keypress'); goRaw(false); process.stdin.pause(); callback(sshh.replace(/^undefined/, '')); } else { sshh += key.name; } }); };
init: function() { // make `process.stdin` begin emitting "keypress" events keypress( process.stdin ); var letters = "abcdefghijklmnopqrstuvwxyz`-=[];',./\\" for(var l = 0; l < letters.length; l++) { var lt = letters.charAt(l); Keys.outputs[ lt ] = { min:0, max:1, value:0 } } // listen for the "keypress" event process.stdin.on('keypress', Keys.processKeystroke ); process.stdin.setRawMode(true); process.stdin.resume(); if( this.onload ) this.onload() Keys.devices.push( Keys ) Keys._on = Keys.on.bind( Keys ) Keys.on = function( outputName, func ) { Keys.register( outputName, func ) Keys._on( outputName, func ) } this.emit( 'new device', 'keypress', Keys ) },
module.exports = function(keys, callback){ var stdin = process.stdin; function setRawMode(enabled){ if (typeof stdin.setRawMode === 'function'){ stdin.setRawMode(enabled); } else { require('tty').setRawMode(enabled); } } var subscr; var unsubscr = function(){ if (subscr){ stdin.removeListener('keypress', subscr); setRawMode(false); stdin.pause(); subscr = null; } }; subscr = function(ch, key){ if (key && (Array.isArray(keys) ? keys.indexOf(key.name) >= 0 : key.name === keys)){ unsubscr(); callback(); } }; keypress(stdin); stdin.on('keypress', subscr); setRawMode(true); stdin.resume(); return unsubscr; };
installPromise.promise.then(function() { console.log("Starting GDB..."); var gdbProcess = pty.spawn(path.join(sdkBasePath, "bin", "arm-none-eabi-gdb"), [result.output], { name: "xterm", cwd: options.projectBase, env: process.env }); gdbProcess.on("data", function(d) { process.stdout.write(d); }); keypress(process.stdin); process.stdin.on("keypress", function(ch, keypress) { gdbProcess.write((ch !== undefined)? ch : keypress.sequence); }); process.stdin.setRawMode(true); process.stdin.resume(); process.on("SIGINT", function() { gdbProcess.kill("SIGINT"); //send it right along return(false); }); gdbProcess.on("exit", function(code) { process.exit(code); }); gdbProcess.write("target remote localhost:1033\n"); //give it a whirl on the most common port. });
function listenForKey() { var ke = new (require('events').EventEmitter); var keypress = require('keypress'); keypress(process.stdin); var self = this; // listen for the "keypress" event process.stdin.on('keypress', function (ch, key) { //console.log(ch, typeof ch, key); if (key && key.ctrl && key.name == 'c') { process.stdin.pause(); return; } if (key) { ke.emit(key.name); } else if(typeof(key) == 'undefined' && ch) { ke.emit(ch); } ke.emit('keypress', ch, key); }); ke.setMaxListeners(50); process.stdin.setRawMode(true); process.stdin.resume(); return ke; }
util.passphrase = function(prompt, callback) { process.stderr.write(prompt); keypress(process.stdin); process.stdin.resume(); var passphrase = ""; process.stdin.on("keypress", function(c, key) { if (key && (key.name == "return" || key.name == "enter")) { process.stderr.write("\n"); process.stdin.pause(); callback(passphrase); } else if (key && key.name == "backspace") { if (passphrase.length > 0) { passphrase = passphrase.substring(0, passphrase.length-1); process.stderr.write('\x1B[D\x1B[K'); } } else if (key && key.ctrl && key.name == "u") { process.stderr.write(passphrase.replace(/./g, '\x1B[D')); passphrase = ""; } else if (key && key.ctrl && key.name == "c") { process.stdin.pause(); callback(null); } else { process.stderr.write("*"); passphrase += c; } }); };
var readKeystrokes = function() { keypress(process.stdin); process.stdin.on('keypress', function(character, key) { console.log(key.name); if(key) { if(key.name == 'up') { sendDatagram(new Buffer('MOVE_FORWARD')); } if(key.name == 'left') { sendDatagram(new Buffer('MOVE_LEFT')); } if(key.name == 'right') { sendDatagram(new Buffer('MOVE_RIGHT')); } if(key.name == 'down') { sendDatagram(new Buffer('MOVE_BACKWARD')); } if(key.name == 'space') { sendDatagram(new Buffer('STOP')); } if(key.ctrl && key.name == 'c') { client.close(); process.exit(); } } }); process.stdin.setRawMode(true); process.stdin.resume(); };
board.on("ready", function() { var speed, commands, servos; speed = 1; commands = null; servos = { right: new five.Servo({ pin: 10, type: "continuous" }), left: new five.Servo({ pin: 11, type: "continuous" }) }; this.repl.inject({ servos: servos }); function controller( ch, key ) { if ( key ) { if ( key.name === "space" ) { servos.right.stop(); servos.left.stop(); } if ( key.name === "up" ) { servos.right.ccw(speed); servos.left.cw(speed); } if ( key.name === "down" ) { servos.right.cw(speed); servos.left.ccw(speed); } if ( key.name === "right" ) { servos.right.cw(0.25); servos.left.cw(0.25); } if ( key.name === "left" ) { servos.right.ccw(0.25); servos.left.ccw(0.25); } commands = [].slice.call(arguments); } else { if ( ch >= 1 && ch <= 9 ) { speed = five.Fn.scale( ch, 1, 9, 0, 1 ); controller.apply(null, commands); } } } keypress(process.stdin); process.stdin.on("keypress", controller); process.stdin.setRawMode(true); process.stdin.resume(); });
offer : function(commitF) { var committed; var returnSelections = function() { process.stdin.removeListener('keypress', handleKeypress); commitF && commitF(api.selected().map(function(s) { return s.id; })) } var handleKeypress = function(ch, key) { if(!key) { return; } if(key.ctrl && key.name == 'c') { return process.exit(0); } switch(key.name) { case "up": api.up() break; case "down": api.down() break; case "space": api.select() !options.isMulti && returnSelections() break; case "return": !options.isMulti && api.select() returnSelections(); break; default: //console.log(key) break; } }; keypress(process.stdin); process.stdin.on('keypress', handleKeypress); process.stdin.setRawMode(true); process.stdin.resume(); // This initializes the view -- it will create a checkmark // in the first product slot. // options.selectFirst && api.down() }
function setup (options) { stdout = options && options.stdout || process.stdout; stdin = options && options.stdin || process.stdin; keypress(stdin); return prompt; }
function listen() { keypress(process.stdin); process.stdin.on("keypress", handle); console.log("starting to listen for arrow key presses"); process.stdin.setRawMode(true); process.stdin.resume(); }
function calibrateApplication() { var keypress = require('keypress'); // make `process.stdin` begin emitting "keypress" events keypress(process.stdin); // listen for the "keypress" event process.stdin.on('keypress', function (ch, key) { // var rpmChange = key.ctrl ? 10 : 1; switch (key.name) { case 'c': // ??? if (key && key.ctrl) { process.stdin.pause(); } break; case 'up': // Increase RPM calibration.speeds[currentSpeed].rpm += rpmChange; adjustRPM(); break; case 'down': // Decrease RPM calibration.speeds[currentSpeed].rpm -= rpmChange; adjustRPM(); break; case 'left': // Goto lower setting if (currentSpeed > 0) { currentSpeed -= 1; } showSpeedChange(); adjustRPM(); break; case 'right': // Goto higher setting if (currentSpeed < (calibration.speeds.length - 1)) { currentSpeed += 1; } showSpeedChange(); adjustRPM(); break; case 's': // Save calibration configuration if (key && key.ctrl) { saveCalibrationFile(); } break; case 'q': // Quit if (key && key.ctrl) { stopMotor(); setInterval(function() { process.exit(); }, 500); } break; } }); process.stdin.setRawMode(true); process.stdin.resume(); }
function test() { const filename = process.argv.length > 2 ? process.argv[2] : 'airwolf.mod'; const buffer = fs.readFileSync(filename); const module = Module.fromBuffer(buffer); const format = { channels: 2, bitDepth: 32, float: true, sampleRate: 44100, }; const speaker = new Speaker(format); const player = new Player(module); keypress(process.stdin); process.stdin.on('keypress', function(ch, key) { const keyAsInt = parseInt(ch); if (!isNaN(keyAsInt)) { player.toggleChannel(keyAsInt); } switch (key && key.name) { case 'left': player.reverse(); break; case 'right': player.forward(); break; case 'space': player.pause(); break; } if (key && key.ctrl && key.name == 'c') { console.log(unimplemented); process.exit(0) } }); process.stdin.setRawMode(true); process.stdin.resume(); const playerReadable = new Readable({ read(size) { // console.time('mix'); const outBuffer = new Float32Array(size); player.mix(outBuffer); const bytes = new Buffer(size * 4) for (let i = 0; i < size; i++) { bytes.writeFloatLE(outBuffer[i], i * 4); } this.push(bytes); if (player.endOfSong) { this.push(null); } // console.timeEnd('mix'); } }); playerReadable.pipe(speaker); }
board.on("ready", function() { var led = new five.Led.RGB([9,10,11]); keypress(process.stdin); process.stdin.on("keypress", function (ch, key) { if (key.name === "space") { led.toggle(); } }); });
board.on("ready", function() { var speed, commands, motors; speed = 100; commands = null; motors = { a: new five.Motor([3, 12]), b: new five.Motor([11, 13]) }; this.repl.inject({ motors: motors }); function controller(ch, key) { if (key) { if (key.name === "space") { motors.a.stop(); motors.b.stop(); } if (key.name === "up") { motors.a.rev(speed); motors.b.fwd(speed); } if (key.name === "down") { motors.a.fwd(speed); motors.b.rev(speed); } if (key.name === "right") { motors.a.fwd(speed * 0.75); motors.b.fwd(speed * 0.75); } if (key.name === "left") { motors.a.rev(speed * 0.75); motors.b.rev(speed * 0.75); } commands = [].slice.call(arguments); } else { if (ch >= 1 && ch <= 9) { speed = five.Fn.scale(ch, 1, 9, 0, 255); controller.apply(null, commands); } } } keypress(process.stdin); process.stdin.on("keypress", controller); process.stdin.setRawMode(true); process.stdin.resume(); });
function repl (client) { // make `process.stdin` begin emitting "keypress" events keypress(process.stdin); // listen for the ctrl+c event, which seems not to be caught in read loop process.stdin.on('keypress', function (ch, key) { if (key && key.ctrl && key.name == 'c') { process.exit(0); } }); client.on('message', prompt); function convertToContext (cmd) { var self = this, matches, scopeVar = /^\s*var\s*([_\w\$]+)(.*)$/m, scopeFunc = /^\s*function\s*([_\w\$]+)/; // Replaces: var foo = "bar"; with: self.context.foo = bar; matches = scopeVar.exec(cmd); if (matches && matches.length === 3) { return matches[1] + matches[2]; } // Replaces: function foo() {}; with: foo = function foo() {}; matches = scopeFunc.exec(self.bufferedCommand); if (matches && matches.length === 2) { return matches[1] + ' = ' + self.bufferedCommand; } return cmd; }; function prompt() { read({prompt: '>>'}, function (err, data) { try { if (err) { throw err; } data = String(data); data = convertToContext(data); var script // = 'function _locals()\nlocal variables = {}\nlocal idx = 1\nwhile true do\nlocal ln, lv = debug.getlocal(2, idx)\nif ln ~= nil then\n_G[ln] = lv\nelse\nbreak\nend\nidx = 1 + idx\nend\nreturn variables\nend\n' = 'local function _run ()\n' + colonyCompiler.colonize(data, {returnLastStatement: true, wrap: false}) + '\nend\nsetfenv(_run, colony.global);\nreturn _run()'; client.send(script); } catch (e) { console.error(e.stack); setImmediate(prompt); } }); } }
var read_password = function (callback) { // Password prompt code adapted from // https://github.com/visionmedia/commander.js/blob/master/lib/commander.js var buf = ''; if (process.stdin.setRawMode) { // when piping password from bash to meteor we have no setRawMode() available process.stdin.setRawMode(true); } // keypress var keypress = require('keypress'); keypress(process.stdin); process.stdin.on('keypress', inFiber(function(c, key){ if (key && (key.name === 'enter' || key.name === 'return')) { console.log(); process.stdin.pause(); process.stdin.removeAllListeners('keypress'); if (process.stdin.setRawMode) { // when piping password from bash to meteor we have no setRawMode() available process.stdin.setRawMode(false); } // if they just hit enter, prompt again. let's not do this. // This means empty password is a valid password. //if (!buf.trim().length) return self.password(str, mask, fn); callback(transform_password(buf)); return; } // deal with backspace if (key && 'backspace' === key.name) { buf = buf.substring(0, buf.length - 1); return; } // raw mode masks control-c. make sure users can get out. if (key && key.ctrl && 'c' === key.name) { console.log(); process.stdin.pause(); process.stdin.removeAllListeners('keypress'); process.stdin.setRawMode(false); process.kill(process.pid, 'SIGINT'); return; } buf += c; })); process.stdin.resume(); };