v6502/vm/computer/keyboard.js

106 lines
2.9 KiB
JavaScript

/*
* 6502 Based Virtual Computer
* Copyright (C) 2011 Scott C. Duensing <scott@jaegertech.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
// Our keyboard object.
function keyboard() {
// OMFGWTFBBQ - http://unixpapa.com/js/key.html
// --- Private variables.
// Memory location for keyboard status.
var MEMORY = null;
var MEMORY_START = 0;
var CHARACTER_ADDRESS = 0;
var META_ADDRESS = 0;
var RUNNING = false;
// --- Public methods.
// Attach keyboard to DOM.
this.attach = function(newMemory, newStartPosition) {
MEMORY = newMemory;
MEMORY_START = newStartPosition;
CHARACTER_ADDRESS = MEMORY_START;
META_ADDRESS = MEMORY_START + 1;
};
// Keyboard handler for use with Meteor.
this.handleEvent = function(event) {
// Process the key press.
var code = event.which || window.event.keyCode;
var k = {
code: code,
shift: event.shiftKey,
alt: event.altKey,
ctrl: event.ctrlKey
};
var processIt = false;
if (event.type == "keydown") {
if ((code == 8) ||
(code == 9) ||
(code == 27))
processIt = true;
}
if (event.type == "keypress") {
processIt = true;
}
//console.log(code + " " + event.type + " " + processIt);
if (processIt) {
// Write meta first. Then we can process on the character being written.
MEMORY.writeByte(META_ADDRESS, (k.alt * 4) + (k.ctrl * 2) + k.shift);
// Ignore "naked" meta keys in the character address.
if ((k.code != 16) && (k.code != 17) && (k.code != 18) && (k.code != 145) && (k.code != 146)) {
MEMORY.writeByte(CHARACTER_ADDRESS, k.code);
}
}
return !processIt;
};
// Deserialize keyboard state.
this.deserialize = function(state) {
RUNNING = state.R;
};
// Fetch how much RAM the current settings will require.
this.getMemoryNeeded = function() {
return 2; // Complicated, huh?
};
// Pause the keyboard.
this.pause = function() {
console.log("Pausing Keyboard.");
RUNNING = false;
};
// Start the keyboard.
this.run = function() {
console.log("Starting Keyboard.");
RUNNING = true;
};
// Serialize keyboard state.
this.serialize = function() {
var state = {
R: RUNNING
};
return state;
};
}