Two easy questions


I have a couple questions about simple C stuff:

1) How do I pause the computer for x seconds?

2) How, at a given point in the program, do I test whether a key, any key, has been pressed?

Please provide specific code.

Thanks,

Mark Ebden
Mark EbdenAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
AlexVirochovskyConnect With a Mentor Commented:
1. This is OS specific.
   for Dos: sleep(n), where n: sec
            or delay(n),where n: msec
  for Windows Sleep(n), where n: msec
2. Same.
   For dos: getch() or
    bioskey(): see Doc
 for windows: trap
  WM_CHAR message and test uKey
Alex
0
 
AlexVirochovskyCommented:
Or , fore Windows you can  use
GetKeaboardState/GetKeyState
Api for test some specific key.
0
 
Mark EbdenAuthor Commented:
Re: Question 1: What library are sleep and delay in?

Re: Question 2: Please reread the original question.
If you are still stuck, keep in mind the following: I want to test, "noninvasively", whether a key has been pressed at or before that point.  Please provide specific code.


Thank you,

Mark
0
Live webcast with Pinal Dave

Pinal Dave will teach you tricks to help identify the real root cause of database problems rather than red herrings. Attendees will learn scripts that they can use in their environment to immediately figure out their performance Blame Shifters and fix them quickly.

 
AlexVirochovskyCommented:
1.
For sleep/delay you must use
#include <dos.h>. Example:
/* delay example */

/* emits a 440-Hz tone for 500 milliseconds */
#include <dos.h>

int main(void)
{
   sound(440);
   delay(500);
   nosound();
   return 0;
}
2. For pression: bioskey:
/* bioskey example */

#include <stdio.h>
#include <bios.h>
#include <ctype.h>

#define RIGHT  0x01
#define LEFT   0x02
#define CTRL   0x04
#define ALT    0x08

int main(void)
{
   int key, modifiers;

   /* function 1 returns 0 until a key is pressed */
   while (bioskey(1) == 0);

   /* function 0 returns the key that is waiting */
   key = bioskey(0);

   /* use function 2 to determine if shift keys were used */
   modifiers = bioskey(2);
   if (modifiers)
   {
      printf("[");

      if (modifiers & RIGHT) printf("RIGHT");
      if (modifiers & LEFT)  printf("LEFT");
      if (modifiers & CTRL)  printf("CTRL");
      if (modifiers & ALT)   printf("ALT");
      printf("]");
   }
   /* print out the character read */
   if (isalnum(key & 0xFF))
      printf("'%c'\n", key);
   else
      printf("%#02x\n", key);
   return 0;
}
Alex





0
 
Mark EbdenAuthor Commented:
Thank you - that's what I was looking for.  I still can't get this to work, though.  I am writing regular C code within Microsoft's Developer Studio, and it doesn't recognize bios.h, and won't find sleep or delay within dos.h.  Unfortunately, I don't have the help CD that goes with Developer Studio, so I can't figure out where Sleep (Visual C++ version) is.  

Which library is Visual C++ Sleep in?


Thanks,

Mark
0
 
AlexVirochovskyCommented:
Or , Developer Studio is Windows(not Dos)! You must use Sleep() and
GetKeaboardState/GetKeyState/GetAsyncKeyState Api, as I wrote in an answer.
0
 
Mark EbdenAuthor Commented:
But Developer Studio doesn't like it when I use Sleep().  Please provide specific code.

Thanks,

Mark
0
 
AlexVirochovskyCommented:
For using Sleep/SleepEx you must
set flag MultiThread in Developer
Studio. How exectly i don't know,
(i use BC), but of course it is very
easy.
0
 
Mark EbdenAuthor Commented:
Thank you.
0
All Courses

From novice to tech pro — start learning today.