floooh / chips-test

Tests and sample code for https://github.com/floooh/chips
MIT License
380 stars 44 forks source link

Keyboard input problem for games #23

Open tanoxyz opened 1 year ago

tanoxyz commented 1 year ago

Hi! I was trying to use the CPC emulator to bundle a web version of a game I made, but I've noticed that there is a problem with the input handling. Maybe you already know about this problem and that there is no clear solution .

The problem

In https://github.com/floooh/chips-test/blob/b8a46cca2a97cd459b16ee5ee2cf673ac398bc79/examples/sokol/cpc.c#L198 you check most keys in the keyboard from char events, as they don't specify key down/up, the key up and down event are sent in the same frame. This seems mostly fine on the default CPC's firmware but some games may need to hold the key for more than 1 frame, for example our game's jump height varies based on how long you have been pressing the key. Our game I suppose that you did it that way in order to map any keyboard layout to the original, for example on the CPC's case the symbol " is on the Key 2 but on today's keyboards it may vary.

Solutions?

For our game, I've ignored the char events from A to Z and used the usual key down/up events to detect them, code here. But a more robust solution would be to just map the all key up/down events to the original keyboard's position. Although some keys won't match in some keyboards, it will be the "real position", in Retro Virtual Machine works that way. We can also add a toggle to change the layout to "physically correct" or not.

Thanks for these emulators by the way!

floooh commented 1 year ago

Thanks for the input, I need to think about this a bit :)

In general, I was hoping that most games would be able to use the joystick emulation (when this is activated, some host keys are intepreted as joystick input instead of keyboard input, but this also has issues - namely that the arrow keys and Spacebar no longer work as regular keys).

The reason that CHAR input is used is indeed that this provides an automatic key mapping for international (host) keyboards, instead of trying to map the home computer keyboards to modern 'physical' keyboards.

Another thing I have somewhere down on the todo list is virtual onscreen keyboards and joysticks, this would mainly be useful for mobile devices, but would also help to make 'exotic keys' on some home computers accessible which are currently ignored. This would not help with your specific problem though :)

I guess the best solution is - as you mentioned - a separate input mode which would use key up/down events instead of CHAR for all keyboard input.