program to read key press values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • srikanth123456
    New Member
    • Jan 2007
    • 16

    program to read key press values

    can u guide me how to write this program


    my program should give outputas follows

    when i press a key on the keyboard it should return the "key is pressed"

    and also if the key is pressed more than 350ms it should return the "key is hold"
  • rajesh6695
    New Member
    • Oct 2006
    • 96

    #2
    kbhit() is a function returns integer value whenever the key is pressed...
    you can use the above function.....

    Try to post the code you had written...

    Comment

    • srikanth123456
      New Member
      • Jan 2007
      • 16

      #3
      ok

      i tried the same
      but how to do the 350 ms time gap on hold key event

      Comment

      • rajesh6695
        New Member
        • Oct 2006
        • 96

        #4
        Please explain the code you want in clear....

        Comment

        • srikanth123456
          New Member
          • Jan 2007
          • 16

          #5
          #include<stdio. h>
          #include<conio. h>
          void main()
          {
          printf("enter key a");
          while(kbhit()=' a')
          {
          printf("key a is pressed");
          }
          }
          but it is saying that lvalue required i entered ascii value also .
          how to get "something done" when a mentioned key is pressed

          Comment

          • rajesh6695
            New Member
            • Oct 2006
            • 96

            #6
            check the while loop condition are you comparing or assiginig...
            Check it.......

            Comment

            • willakawill
              Top Contributor
              • Oct 2006
              • 1646

              #7
              kbhit() returns 0 if the keyboard has not been hit and a nonzero value if it has been hit. It does not return the value of the key that has been hit e.g.
              Code:
              /* KBHIT.C: This program loops until the user
               * presses a key. If _kbhit returns nonzero, a
               * keystroke is waiting in the buffer. The program
               * can call _getch or _getche to get the keystroke.
               */
              
              #include <conio.h>
              #include <stdio.h>
              
              void main( void )
              {
                 /* Display message until key is pressed. */
                 while( !_kbhit() )
                    _cputs( "Hit me!! " );
              
                 /* Use _getch to throw key away. */
                 printf( "\nKey struck was '%c'\n", _getch() );
                 _getch();
              }
              which is the example shown in vs6 help

              Comment

              • rajesh6695
                New Member
                • Oct 2006
                • 96

                #8
                #include <stdio.h>
                #include <conio.h>
                #include <math.h>
                #include <time.h>

                int main()
                {
                int m;
                clock_t st,end,end_out;
                clrscr();
                st=clock();
                printf("The value of the clock is : %f\n",st/CLK_TCK);
                do
                {

                if(kbhit())
                {
                if((m=getch())= =97)
                {
                end=clock();
                printf("Key a is pressed....\n") ;
                printf("The value of the clock in sec : %f\n",end/CLK_TCK);
                }
                }
                }while(1);
                getch();
                return 0;
                }



                I think the above code will help you understand more...Clock is used measure the time taken to complete the execution

                Comment

                • srikanth123456
                  New Member
                  • Jan 2007
                  • 16

                  #9
                  thanks for u r advice i got much from u r program

                  Comment

                  • dusty
                    New Member
                    • Oct 2006
                    • 12

                    #10
                    [QUOTE=rajesh669 5]#include <stdio.h>
                    #include <conio.h>
                    #include <math.h>
                    #include <time.h>

                    int main()
                    {
                    int m;
                    clock_t st,end,end_out;
                    clrscr();
                    st=clock();
                    printf("The value of the clock is : %f\n",st/CLK_TCK);
                    do
                    {

                    if(kbhit())
                    {
                    if((m=getch())= =97)
                    {
                    end=clock();
                    printf("Key a is pressed....\n") ;
                    printf("The value of the clock in sec : %f\n",end/CLK_TCK);
                    }
                    }
                    }while(1);
                    getch();
                    return 0;
                    }


                    Hi all,

                    I was looking for a similar program, but when I compiled this program in linux, i got an error message which goes:

                    stopwatch.cpp:2 :19: conio.h: No such file or directory
                    stopwatch.cpp: In function `int main()':
                    stopwatch.cpp:1 0: error: `clrscr' undeclared (first use this function)
                    stopwatch.cpp:1 0: error: (Each undeclared identifier is reported only once for
                    each function it appears in.)
                    stopwatch.cpp:1 2: error: `CLK_TCK' undeclared (first use this function)
                    stopwatch.cpp:1 6: error: `kbhit' undeclared (first use this function)
                    stopwatch.cpp:1 8: error: `getch' undeclared (first use this function)

                    Does conio.h is only for windows. I compiled this program in cygwin. Any help please. Thanks

                    Comment

                    • Abdur Rehman
                      New Member
                      • Jul 2014
                      • 1

                      #11
                      [QUOTE=dusty;239 5686]
                      Originally posted by rajesh6695
                      #include <stdio.h>
                      #include <conio.h>
                      #include <math.h>
                      #include <time.h>

                      int main()
                      {
                      int m;
                      clock_t st,end,end_out;
                      clrscr();
                      st=clock();
                      printf("The value of the clock is : %f\n",st/CLK_TCK);
                      do
                      {

                      if(kbhit())
                      {
                      if((m=getch())= =97)
                      {
                      end=clock();
                      printf("Key a is pressed....\n") ;
                      printf("The value of the clock in sec : %f\n",end/CLK_TCK);
                      }
                      }
                      }while(1);
                      getch();
                      return 0;
                      }


                      Hi all,

                      I was looking for a similar program, but when I compiled this program in linux, i got an error message which goes:

                      stopwatch.cpp:2 :19: conio.h: No such file or directory
                      stopwatch.cpp: In function `int main()':
                      stopwatch.cpp:1 0: error: `clrscr' undeclared (first use this function)
                      stopwatch.cpp:1 0: error: (Each undeclared identifier is reported only once for
                      each function it appears in.)
                      stopwatch.cpp:1 2: error: `CLK_TCK' undeclared (first use this function)
                      stopwatch.cpp:1 6: error: `kbhit' undeclared (first use this function)
                      stopwatch.cpp:1 8: error: `getch' undeclared (first use this function)

                      Does conio.h is only for windows. I compiled this program in cygwin. Any help please. Thanks
                      I Know it's not a permanent solution but,
                      it's also not a very very bad idea to use online compiler
                      Try This Online Compiler

                      Comment

                      Working...