Skip to content

Commit

Permalink
Updated the keyCode implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
univta0001 committed Sep 5, 2024
1 parent 427f0a8 commit 8a6f264
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
8 changes: 5 additions & 3 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@

// Setup Keyboard Handler
const onKeydown = (e) => {
if (e.isComposing || e.keyCode === 229) {
var keyCode = e.charCode || e.keyCode || e.which;
if (e.isComposing || keyCode === 229) {
return;
}

Expand Down Expand Up @@ -273,7 +274,7 @@
return emu6502.keyboard_latch(29);
}

let value = e.keyCode & 0x7f;
let value = keyCode & 0x7f;
let shift_mode = e.shiftKey;
let ctrl_mode = e.ctrlKey;
let caps_mode = e.getModifierState("CapsLock");
Expand Down Expand Up @@ -430,7 +431,8 @@

const onKeyup = (e) => {
clear_hidden_text();
if (e.isComposing || e.keyCode === 229) {
var keyCode = e.charCode || e.keyCode || e.which;
if (e.isComposing || keyCode === 229) {
return;
}

Expand Down
41 changes: 34 additions & 7 deletions emu6502_web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
}

const clear_hidden_text = (e) => {
hidden_text.value = '';
//hidden_text.value = '';
}

document.getElementById("emulator").addEventListener("click", show_keyboard);
Expand Down Expand Up @@ -240,7 +240,8 @@

// Setup Keyboard Handler
const onKeydown = (e) => {
if (e.isComposing || e.keyCode === 229) {
var keyCode = e.charCode || e.keyCode || e.which;
if (e.isComposing || keyCode === 229) {
return;
}

Expand Down Expand Up @@ -430,8 +431,8 @@

const onKeyup = (e) => {
clear_hidden_text();

if (e.isComposing || e.keyCode === 229) {
var keyCode = e.charCode || e.keyCode || e.which;
if (e.isComposing || keyCode === 229) {
return;
}

Expand All @@ -441,7 +442,7 @@
} else {
emu6502.any_key_down(false);
}

let leftAlt = e.code == "AltLeft"
let rightAlt = e.code == "AltRight"

Expand Down Expand Up @@ -479,10 +480,36 @@
}
}

const onInput = (e) => {
if ('nativeEvent' in e) {
const ev = e.nativeEvent;
var key = 0;
if (ev.data) {
key = ascii_to_int(ev.data);
} else if (ev.inputType === 'deleteContentBackward') {
key = 8;
} else if (ev.inputType === 'deleteContentForward') {
key = 46;
} else if (ev.inputType === 'insertLineBreak') {
key = 13;
}

if (key != 0) {
emu6502.keyboard_latch(key);
}
}
}

const setupKeyHandler = () => {
if (typeof window !== 'undefined') {
hidden_text.addEventListener('keydown', onKeydown);
hidden_text.addEventListener('keyup', onKeyup);
const isTouchDevice = "ontouchstart" in document.documentElement;
const isAndroidDevice = /Android/i.test(navigator.userAgent);
if (isTouchDevice || isAndroidDevice) {
hidden_text.addEventListener('input', onInput);
} else {
hidden_text.addEventListener('keydown', onKeydown);
hidden_text.addEventListener('keyup', onKeyup);
}
}
};

Expand Down

0 comments on commit 8a6f264

Please sign in to comment.