views:

40

answers:

3

I'm writing a game in assembly, and I need to check if a key was pressed. So, how is kbhit implemented in Linux?

Thanks.

A: 

Google turned up a kbhit implementation for Linux in C: http://cboard.cprogramming.com/c-programming/63166-kbhit-linux.html

You could either compile this as is and call it from your assembly code, or if you really want to you could convert it to assembly.

Paul R
I know it's a noob question, but how can I use the c standard library with assembly?I mean, I don't know which files to link...
Sason
@Sason: probably the easiest thing would be to have a C main(), e.g. in main.c, which calls the entry point of your assembly program. Then you could just build something like this: `gcc -Wall main.c my_game.S other_stuff.c some_other_stuff.S -o my_game`.
Paul R
You could also open asm() tag inside the C code, i.e. `asm("jmp label")` and reference variables declared in the C code inside the asm() code. This could be very useful.
jyzuz
Or you could just pass -lc to the linker.
ninjalj
A: 

I tried what you said, but as far as I know linking a c module doesn't mean that the c standard library is linked. Is there another way?

Sason
This is not an answer - it needs to be a comment to whatever answer you are responding to.
Paul R
A: 

I assume that you want also key releases. I also assume you are on the console (for X, XKeyEvent has enough info).

First, you have to put your terminal (i.e: console) in non-canonical or in raw mode. If you don't do this, you won't see any input until there is a line delimiter or EOF on input. See my answer to your previous question.

Then, to get key releases, you want to set the keyboard to RAW or MEDIUMRAW mode (this has nothing to do with terminal raw mode, this is very Linux and console specific, see console_ioctl(4)). Don't forget to set the keyboard back to its original mode before exiting.

There's a nice article about this here.

ninjalj