CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 3 of 3

Thread: Keydown and Keyup In linux

  1. #1
    Join Date
    May 2007

    Keydown and Keyup In linux

    For those who don't care about the back story, skip this paragraph:
    I can't seem to catch the key press events, I have tried getchar() but that requires pressing enter, and also doesn't tell me when the button is being pressed... Anything I search comes up with "Borland compiler, windows.h, conio.h" and stuff like windows or direct x, thats not what I'm looking for. I want to learn to catch the keydown event without changing my compiler, adding more crap, or booting into windows. I use g++, and SUSE Linux with the 2.6 kernel (not so sure if it makes a difference. I'd send a snippet of code, but I dont have one, I've been randomly tracking down tutorials but all require something extra. sorry for the long story, I was irritated and venting...

    My setup:
    SUSE Linux Kernel 2.6
    GNU C compiler (g++)

    All I want is to do is catch the keyup/keydown events.... and when they're being used...
    Last edited by Sucrac; October 8th, 2007 at 01:33 AM. Reason: Fixed stupid looking error

  2. #2
    Join Date
    Oct 2002

    Re: Keydown and Keyup In linux

    On Linux you would use the ncurses library instead of the cinio.h functions.
    Try somthing like this.
    #include <string.h>
    #include <ncurses.h>
    int main() {
        int ch;
        char buffer[256];
        initscr(); cbreak(); noecho();
        keypad(stdscr, TRUE);
        while ( (ch = getch()) == ERR );
        sprintf(buffer,"key 0x%02X was pressed\n\n", ch);
        addstr("press any key to exit.");
        while ( (ch = getch()) == ERR );
    This works for me. But I found it difficult to initialize the ncurses library properly.
    man ncurses should give you all the information you need.

    EDIT:you will have to link to the curses library
    gcc testcurses.c -lcurses
    Last edited by ZuK; October 7th, 2007 at 04:26 AM.

  3. #3
    Join Date
    May 2007

    Re: Keydown and Keyup In linux

    Ok, it works to tell me the ASCII code of the character that was pressed, but doesn't continue to show that while I hold the key, but I assume that is because the keyboard auto-repeats the key I hold down? Is there anyway to avoid the auto repeat while holding a key? I think I'm going to try to just make a loop for it...

    edit: I used
    g++ filename.cpp -lncurses
    I don't know if that was wrong (you posted -lcurses) but yours didn't work for me...
    Last edited by Sucrac; October 8th, 2007 at 01:34 AM. Reason: Added code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)