4 ms·
Having 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
by Spivak 6y ago
Having 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.)