Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

wardekar

Two Easy Programming Questions

This topic is 6595 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m trying to think of the old DOS function that returns a boolean that tells you if ANY key is being pressed. And what header it''s in. And I forget the old DOS function to convert an integer back to a character, like int blah=getch();, how do you then go back and display blah in it''s character form? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
I think the first one you''re thinking of his kbhit(). And I think it''s declared in conio.h. Not sure about the second one though.



"We are the music makers, and we are the dreamers of the dreams."
- Willy Wonka

Share this post


Link to post
Share on other sites
The following code will read a character if one is pressed, and output it to the screen, otherwise it will do nothing. This is not a realistic use of the functions, but you''ll get the idea .
  
#include <conio.h>
#include <stdio.h>

int main(void) {
while(1) {
if(kbhit()) {
printf("%c\n",(unsigned char) getch());
} else {
// No key is held down...

}
}
}




http://www.gdarchive.net/druidgames/

Share this post


Link to post
Share on other sites
That method doesn''t work. What I''m trying to do is if a key is being pressed, add that character to a string. Also, the character must be a number or a period.

Share this post


Link to post
Share on other sites
quote:
What I'm trying to do is if a key is being pressed, add that character to a string. Also, the character must be a number or a period.


      
#include < string.h >
char string[80];
.........
.........
while(TRUE) {
if(kbhit()) {
if(strlen(string)+1 > 79) {
break;
}
char ch = getch();
if(ch == '\n') {
sprintf(string, "%s\0", string); // Not sure if needed

break;
}
if(ch >= 48 && ch <= 57) {
sprintf(string, "%s%d", string, ch);
} else {
if(ch == 46) {
sprintf(string, "%s.", string);
} else {
sprintf(string, "%s%c", ch);
}}
}
}
.........
.........


Hope I didn't forget anything!

Edited by - Zipster on November 26, 2000 12:41:29 AM

Share this post


Link to post
Share on other sites
Extra Note If You Happen To Be Writing a DOS Game:

Best way to get input from keyboard in DOS when doing stuff like games (where normal kbhit and getch just dont give ample performance), latch onto the keyboard interrupt. Too tired right now to remember the exact code, but if u do need it, i would be happy to post it. You can also find that technique in "Tricks of the Game Programming Gurus" by Andre LaMothe (sp) and company

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!