5 ms·
I find I run into similar discussions whenever a beginner programmer decides they want to capture input from the arrow keys. They've worked through the bog sta
by nickelpro 6y ago
I find I run into similar discussions whenever a beginner programmer decides they want to capture input from the arrow keys.
They've worked through the bog standard Python tutorials using input() to capture from stdin, so it should be trivial to design a simple game that uses the arrow keys right? And suddenly this gulf of complexity opens up of event polling, keycodes, integrating third party modules, and they're left wondering why moving one key over on the keyboard is so hard.
- Spivak 6y agoHaving never really had a needed to do this I'm a little confused -- isn't this just a matter of putting the tty into cbreak or raw mode and then just reading keys from stdin? Like keycodes are annoying but "special keys have this weird text representation, here's a table" doesn't seem like a huge gap to cross.
- tmpz22 6y agoFor games you also have to decipher what a keystroke should do physics wise which isn’t always simple, ie acceleration and deceleration, including what happens when multiple arrows are held down. You now are polling multiple events concurrently and processing them outside of that loop. And so on and so on.
- deleted 6y ago[deleted]
- divingdragon 6y ago> putting the tty into cbreak or raw mode and then just reading keys from stdin You're saying this like most beginners even have an idea what a tty is. An anecdote: When I wrote my first C++ program that reads an input (using `cin`, hello-world style), I immediately wondered how to read single keys including arrow keys. The book I was following had nothing about this. I don't remember the exact details but a bit of Internet search got me to `conio.h` (I was using Dev-C++ on Windows) which did work. But getting it to work was so confusing to me at the time. And I didn't even get to understand what it's actually doing under the hood. I imagine it would be a bit easier in Python given the right libraries, but the dependency management probably adds to the complexity for a beginner.
- sokoloff 6y agoFor games, I usually want to handle at the level of keydown and keyup rather than keystrokes. What’s the keystroke for right-arrow, spacebar, left-shift, and W all being held down? (Think of an Asteroids arcade game where w is thrust, spacebar is fire, left-shift is shields, and right arrow is rotate. Those are up/down, not strokes, that the player is giving you.)
- qertistick 6y agoYou use a game library for this and then it becomes as simple as pushing a key handler onto the event loop and usually the keycodes are already processed for you as well. I don't get the false dilemma people are creating that somehow this is complicated or that people who use the easy way are less knowledgable than others. If you make a game engine from scratch by yourself it's going to be a logical disaster, whether you've learned c++ or python.
- Gravityloss 6y agoYeah, it's surprisingly simple. It took little time with a game library to get the key polling to work. I was experimenting with chorded keyboard ideas back then.
- saagarjha 6y agoBut then you might end up talking about event loops and routing…
- MaxBarraclough 6y ago> You use a game library for this If it's a command-line application, they might use curses. edit Apparently curses isn't available by default in Python3 for Windows. Is there a nice way to do this?