Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Keyboard issues
#2
More info on keyboard issues. 

I was just doing the Magpi lesson on starting to move sprites around with keyboard, and since I am not using my own keyboard code I needed to use a standard system like this

Code:
//set up a raw keyboard read, save the old one and steal a lot of its info but set to no pause
    int fd = fileno(stdin);
    ioctl(fd, TCGETA, &original_termio);
    new_termio = original_termio;
    new_termio.c_cc[VMIN] = 0;
    new_termio.c_cc[VTIME] = 0;
    new_termio.c_lflag = 0;
    ioctl(fd, TCSETA, &new_termio);


and the actual read
Code:
        int fd = fileno(stdin);
        char c = '\0';
        int err = read(fd, &c, 1);
        if (err == 0) c = '\0';
        // returns err = 0 for no key, err = 1 for pressed, and char in c


But that simply wont' work if you are running your code in VisualGDB...its annoying as hell, The code will work perfectly if you fire it up from the target directly though so the system is viable, but I assume VisualGDB is redirecting the input somewhere preventing you from using it.... I have posted a note on sysprogs.

oh if you are going to use this code, be sure to switch the key input back to normal when done with this

Code:
int fd = fileno(stdin);
    ioctl(fd, TCSETA, &original_termio); // put the old one back
I don't really like this form of keyreader, it is quite laggy, and also buffers your keypresses, meaning it will still register them for a few frames after you remove  your finger, I assume there is some way to flush the buffer once you do a read, but I've not found it yet. (anyone??)

My own input systems are far more precise and reliable, IF they are set up correctly.
Brian Beuken
Lecturer in Game Programming at Breda University of Applied Sciences.
Author of The Fundamentals of C/C++ Game Programming: Using Target-based Development on SBC's 



Reply


Messages In This Thread
Keyboard issues - by Brian Beuken - 03-05-2018, 09:23 AM
RE: Keyboard issues - by Brian Beuken - 04-11-2018, 10:14 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)