Open spl237 opened 7 years ago
Pushing stuff into the global keyboard queue from Python is possible, but quite hard (because platforms are rather different in the way they handle keyboard buffers). However, the point of developing the "stick" API in both the sense_hat
library, and the sense_emu
emulation was to avoid the need for people to go querying the keyboard itself. The "stick" API allows you to do the same stuff, but only queries the joystick (not any other keyboard), and works in both the "real" HAT and the emulator.
In other words I'd suggest migrating the code to that API for a quick fix; anything else will take quite a bit longer (and I'm not entirely convinced the emulator should be encouraging people to query the keyboard for the joystick).
Might combine this with #23 and consider permitting various different layouts including options with / without the joystick buttons (though I still think people should just use the stick API).
The joystick buttons on the GUI window do not seem to correctly emulate the joystick. They do not generate the relevant key codes - this may just be an issue with window focus, but the effect is that the buttons are not useful in Python code.
This sample code shows the problem:
If this code is run with a SenseHAT connected (and the real SenseHAT library loaded instead of the emulator library), pressing the joystick in the 4 directions changes the colour of the LED, as expected.
If this code is run under the emulator, pressing the buttons on the window which are supposed to emulate the joystick has no effect. If the emulator window has focus, pressing the keyboard cursor buttons causes the buttons on the window to be activated (as they can be seen to highlight), but the state of the LED doesn't change.
The only way to get the LED to respond to button pushes is to change focus to the pygame window, and to press the keyboard cursor keys - at this point, the simulated LED does respond as intended.
I am not sure if it is actually possible to have the buttons on the window cause the required events to be generated given the limitations of focus and the targeting of events. It might be preferable to remove these buttons from the window completely and to rely on keyboard events; this seems to more accurately reflect the operation of a real SenseHAT.