Question

I am working on a CHIP-8 emulator to be used on a Linux system. As I understand it, I need to get keyboard input from (any) 16 keys and map these to a hex keypad. Once I get the inputs, I have no trouble mapping them to the 16 CHIP-8 keys.

However, I cannot figure out how to actually tell if a key is pressed. I believe in Java I would use keylistener, but I cannot find anything analogous for Linux in C++. Is there a way I can tell what (if any) specific keys are pressed at any given time? Something like a getKeyState function I could call?

Thanks in advance.

Was it helpful?

Solution 2

There is this tutorial on ncurses. NCURSES Programming HOWTO. I have confirmed that on my MacBook Pro, Mavericks 10.9.1, with the Xcode Command Line Tools installation, that the ncurses.h is a symbolic link to curses.h, both located in /usr/include. This is the NCURSES Introduction page.

I have not created a specific class myself. However, some searching on /dev/input/eventX, where X seems to be either a zero or one finds these two separate posts:Read from /dev/input and linux keypress events.

OTHER TIPS

In linuxOS, you can use: getchar function in stdio.h header file. Here is an example:

#include <iostream>
#include <stdio.h>
int main() {
    char pressedKey;
    std::cout << "Press the key 'A' to continue: ";
    pressedKey = getchar();
    if(pressedKey == 'A') {
        std::cout << "Lets continue\n";
    } else {
        return;
    }
}

Or on windowsOS, you can use getch() in conio.h header file Example:

#include <iostream>
#include <conio.h>
int main() {
    std::cout << "Press any key to continue: ";
    int key = getch();
    std::cout << "You pressed: " << key << "\n";
    if(key == 'k' || key == 'K') {
        std::cout << "You pressed k, i love letter k.\n";
    }
}

Your program may read the characters from standard input.

You can use fgets() library function to perform the reading.

See, for example:

I believe that the recommended approach would be to use ncurses as already mentioned by @CPlusPlus OOA and D.

Although you could implement a non-blocking "key-press-detector" with this code:

#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <vector>

#include <termios.h>
#include <unistd.h>
#include <fcntl.h>

int kbhit(void) {
    struct termios oldt, newt;
    int ch;
    int oldf;

    tcgetattr(STDIN_FILENO, &oldt);
    newt = oldt;
    newt.c_lflag &= ~(ICANON | ECHO);
    tcsetattr(STDIN_FILENO, TCSANOW, &newt);
    oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
    fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);

    ch = getchar();

    tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
    fcntl(STDIN_FILENO, F_SETFL, oldf);

    if(ch != EOF) {
        ungetc(ch, stdin);
        return 1;
    }

    return 0;
}

char get_char(){
    int k;
    int i = 0;
    while(i == 0){
        if(kbhit()){
            k = _getch();
            i++;
        }
    }
    return char(k);
}

int main() {
    while(1) {
        while(!kbhit()) {
            // Works continuously until interrupted by keyboard input.
            printf("Continuous...\n");
        }
        int c = get_char(); 
        std::cout << c;
        break; // Or whatever you want to do...
    }
    return 0;
} 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top