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...
[957 byte] By [
Sucrac] at [2007-11-20 11:14:33]

# 1 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(buffer);
addstr("press any key to exit.");
while ( (ch = getch()) == ERR );
endwin();
}
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.
Kurt
EDIT:you will have to link to the curses library
e.g.
gcc testcurses.c -lcurses
ZuK at 2007-11-9 1:25:13 >

# 2 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...