1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-11-25 18:33:11 +00:00

refactored keyboard stuff

This commit is contained in:
Steven Hugg 2018-12-09 07:44:39 -05:00
parent 7441196b2e
commit 8cb1338364
2 changed files with 42 additions and 28 deletions

View File

@ -27,7 +27,7 @@ export function setNoiseSeed(x : number) {
_random_state = x;
}
type KeyboardCallback = (which:number, charCode:number, flags:number) => void;
type KeyboardCallback = (which:number, charCode:number, flags:KeyFlags) => void;
function __createCanvas(mainElement:HTMLElement, width:number, height:number) : HTMLCanvasElement {
var canvas = document.createElement('canvas');
@ -39,15 +39,25 @@ function __createCanvas(mainElement:HTMLElement, width:number, height:number) :
return canvas;
}
export enum KeyFlags {
KeyDown = 1,
Shift = 2,
Ctrl = 4,
Alt = 8,
Meta = 16,
KeyUp = 64,
KeyPress = 128,
}
function _setKeyboardEvents(canvas:HTMLElement, callback:KeyboardCallback) {
canvas.onkeydown = function(e) {
callback(e.which, 0, 1|_metakeyflags(e));
callback(e.which, 0, KeyFlags.KeyDown|_metakeyflags(e));
};
canvas.onkeyup = function(e) {
callback(e.which, 0, 0|_metakeyflags(e));
callback(e.which, 0, KeyFlags.KeyUp|_metakeyflags(e));
};
canvas.onkeypress = function(e) {
callback(e.which, e.charCode, 1|_metakeyflags(e));
callback(e.which, e.charCode, KeyFlags.KeyPress|_metakeyflags(e));
};
};
@ -396,7 +406,10 @@ export const Keys = {
};
function _metakeyflags(e) {
return (e.shiftKey?2:0) | (e.ctrlKey?4:0) | (e.altKey?8:0) | (e.metaKey?16:0);
return (e.shiftKey?KeyFlags.Shift:0) |
(e.ctrlKey?KeyFlags.Ctrl:0) |
(e.altKey?KeyFlags.Alt:0) |
(e.metaKey?KeyFlags.Meta:0);
}
export function setKeyboardFromMap(video, switches, map, func?) {
@ -410,11 +423,11 @@ export function setKeyboardFromMap(video, switches, map, func?) {
var mask = o.mask;
if (mask < 0) { // negative mask == active low
mask = -mask;
flags ^= 1;
flags ^= KeyFlags.KeyDown;
}
if (flags & 1) {
if (flags & KeyFlags.KeyDown) {
switches[o.index] |= mask;
} else {
} else if (flags & KeyFlags.KeyUp) {
switches[o.index] &= ~mask;
}
}

View File

@ -1,7 +1,7 @@
"use strict";
import { Platform, Base6502Platform, BaseMAMEPlatform, getOpcodeMetadata_6502, getToolForFilename_6502 } from "../baseplatform";
import { PLATFORMS, RAM, newAddressDecoder, padBytes, noise, setKeyboardFromMap, AnimationTimer, RasterVideo, Keys, makeKeycodeMap, dumpRAM } from "../emu";
import { PLATFORMS, RAM, newAddressDecoder, padBytes, noise, setKeyboardFromMap, AnimationTimer, RasterVideo, Keys, KeyFlags, makeKeycodeMap, dumpRAM } from "../emu";
import { hex, lzgmini } from "../util";
import { SampleAudio } from "../audio";
@ -164,27 +164,28 @@ const _Apple2Platform = function(mainElement) {
audio = new SampleAudio(cpuFrequency);
video.create();
video.setKeyboardEvents((key,code,flags) => {
if (flags & 1) {
if (code) {
// convert to uppercase for Apple ][
if (code >= 0x61 && code <= 0x7a)
code -= 0x20;
if (flags & KeyFlags.KeyPress) {
// convert to uppercase for Apple ][
if (code >= 0x61 && code <= 0x7a)
code -= 32;
if (code >= 32) {
if (code >= 65 && code < 65+26) {
if (flags & KeyFlags.Ctrl)
code -= 64; // ctrl
}
kbdlatch = (code | 0x80) & 0xff;
} else if (key) {
switch (key) {
case 16: return; // shift
case 17: return; // ctrl
case 18: return; // alt
case 37: key=8; break; // left
case 39: key=21; break; // right
case 38: key=11; break; // up
case 40: key=10; break; // down
}
if (key >= 65 && key < 65+26) {
if (flags & 5) key -= 64; // ctrl
}
kbdlatch = (key | 0x80) & 0xff;
}
} else if (flags & KeyFlags.KeyDown) {
code = 0;
switch (key) {
case 13: code=13; break; // return
case 37: code=8; break; // left
case 39: code=21; break; // right
case 38: code=11; break; // up
case 40: code=10; break; // down
}
if (code)
kbdlatch = (code | 0x80) & 0xff;
}
});
var idata = video.getFrameData();