caiiiycuk / js-dos

The best API for running dos programs in browser

Home Page:https://js-dos.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Non recognized keys

NaLiJa opened this issue · comments

commented

It seems that several keys are not working, especially backspace and /,, some more.
The keys neither work through real keyboard, nor through virtual keyboard. I tried to push the keys through the interface as well, but it didn't help (although I am not sure if my code is correct, it was quite hard to get this together from all the sources)

The most critical for me is backspace (for a text adventure game)

 function getKeyCode(code) {
      switch (code) {
             case 8: return 259;
             case 13: return 257;
             case 38: return 265;
             case 39: return 262;
             case 37: return 263;
             case 40: return 264;
             default: return code;
       }
 }

async function runEmu() {
  emulators.pathPrefix = "/msgame/";
  const ci = await Dos(document.getElementById("jsdosbox")).run("/msgame/QLP.jsdos");
 ...
pinput.addEventListener("keydown",(function(e){
ci.sendKeyEvent(getKeyCode(e.keyCode),true);
}));

pinput.addEventListener("keyup",(function(e){
ci.sendKeyEvent(getKeyCode(e.keyCode),false);
}));
}

Thanks for helping!

commented

Small addendum: I guess, what I am facing here is a somewhat different problem than I first thought, I just did not recognize it. I have an old game with command line entry. The game uses "Left control+left" keys instead of Backspace key for deleting the last char and "left" key for moving to the left in the command line. When sending this combination via simulateKeyPress, I often see the cursor moving to the left instead of deleting the last char. I think this happens because of the behaviour of

    public simulateKeyPress(...keyCodes: number[]) {
        const timeMs = Date.now() - this.startedAt;
        keyCodes.forEach((keyCode) => this.addKey(keyCode, true, timeMs));
        keyCodes.forEach((keyCode) => this.addKey(keyCode, false, timeMs + 16));
    }

when using a key combination. When pressing Ctrl+Left it presses the Ctrl key, then the Left key, however when releasing it releases Ctrl first, then Left, but actually it should release Left first, then Ctrl. So with key combinations the second line should probably something like

 keyCodes.slice().reverse().forEach((keyCode) => this.addKey(keyCode, false, timeMs + 16));

I guess, it should work like that in most situations. If there are exceptions, perhaps it would make sense to have a second function like simulateCombinedKeyPress or alike.

So for your case you need just api to send button events, like sendPress, sendRelease. Will it works for you?

commented

Sort of, what seems to be working in my case is

if (e.keyCode == 8)
{
setTimeout((()=>{ci.sendKeyEvent(342,true);}),0);
setTimeout((()=>{ci.sendKeyEvent(263,true);}),10);
setTimeout((()=>{ci.sendKeyEvent(263,false);}),20);
setTimeout((()=>{ci.sendKeyEvent(342,false);}),30);
}
else
{
ci.sendKeyEvent(getKeyCode(e.keyCode),true);
setTimeout((()=>{ci.sendKeyEvent(getKeyCode(e.keyCode),false);}),20);
}

but this is very sensitive to timing, so it might fail on other browsers and this only works when injecting it into js-dos from the outside, the virtual keyboard and the mobile ui would need to be customized or fixed to work reliabliy with key combinations. Need to try to setup a build chain for all three involved projects. I need to find out how to make js-dos use the local builds of emulator and emulator-ui. I could try to fix some things then. There are some other minor problems (IPad OS needs a different detection than UserAgent, the doublequote key seems to be missing,...)

Thanks for helping!