kbhit in console?

R

Rasmus B. Nielsen

hi

Can any tell me how/if I can do something like (in C) kbhit, in the console?


From Rasmus B. Nielsen
 
H

Harald Hein

Rasmus B. Nielsen said:
Can any tell me how/if I can do something like (in C) kbhit, in
the console?

You can't with pure Java, and you shouldn't.

kbhit is not a standard C function but some operating system specific
extension. It's principle model is based on wasting CPU cycles in loops
like

while(!kbhit()) {
;
}

This is desastrous on any timesharing operating system and should be
avoided at all cost. Java doesn't have any equivalent, and Java is in
general very weak when it comes to deal with the console.

You can create a GUI doing this things but in a more efficient way. You
subscribe to keyboard events, and whenever a key is pressed, your code
is called. When no key is pressed, the VM and the OS can do other
important things instead of burning CPU cycles.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,268
Messages
2,571,095
Members
48,773
Latest member
Kaybee

Latest Threads

Top