I'm currently working on a project where I'm trying to run some QBasic code on a webpage. I transpiled it to JavaScript and I wrote some helper functions to replace functions in QBasic. Specifically, there's this function INKEY$ that returns the last key the user pressed. I tried implementing this by having an event listener update a global variable to keep track of the last key the user pressed, and then I created an inkeys function to return this variable.
document.addEventListener('keydown', event => qb.key_pressed = event.key);
...
function inkeys() {
return qb.key_pressed
}
The transpiled code then has something like this to get what they typed.
a$ = "";
while (a$ == "") {
a$ = inkeys();
}
When I run it, my browser gets stuck in an infinite loop inside that loop and eventually crashes. I've checked that the inkeys() function is running each time, but the key_pressed variable isn't updating. I also know the event listener is working normally outside the loop, so I assume there's an issue with event listeners not firing inside a while loop or something? I'm not all that familiar with JavaScript so I don't know really know what to do. I'm getting the feeling that this is a bigger problem that's gonna involve me having to change the structure of my code. Does anyone have any help with how I could fix this?
Furthermore, my tab crashing makes me think this while loop setup, while having worked in QBasic, may not be good for a website, and that concerns me since this particular program involves running a simulation until the user types a key. If so, are there better methods of handling something like this that won't kill the user's tab?