12 Replies - 827 Views - Last Post: 18 June 2013 - 04:57 AM Rate Topic: -----

#1 lewm  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 160
  • Joined: 29-March 13

Kbhit question.

Posted 05 May 2013 - 08:01 AM

Hi guys, I cant seem to get this to work I get back either a 1 0 or smiley face...
int main(void)
{
    char a;

    while( ! (a=kbhit()) );
    printf("%c", a);
    
    getch();
    return 0;
}

Is This A Good Question/Topic? 0
  • +

Replies To: Kbhit question.

#2 lewm  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 160
  • Joined: 29-March 13

Re: Kbhit question.

Posted 05 May 2013 - 08:42 AM

I realise now that kbhit only returns TRUE or FALSE, how do I save the key pressed?
Was This Post Helpful? 0
  • +
  • -

#3 lewm  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 160
  • Joined: 29-March 13

Re: Kbhit question.

Posted 05 May 2013 - 08:47 AM

Got it, the key press is still in buffer I retrieved it using getch().
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2128
  • View blog
  • Posts: 3,262
  • Joined: 21-June 11

Re: Kbhit question.

Posted 05 May 2013 - 09:07 AM

Can't you replace your entire while loop with a call to getch?
Was This Post Helpful? 0
  • +
  • -

#5 lewm  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 160
  • Joined: 29-March 13

Re: Kbhit question.

Posted 05 May 2013 - 09:09 AM

View Postsepp2k, on 05 May 2013 - 09:07 AM, said:

Can't you replace your entire while loop with a call to getch?

No because I will eventually have some code in the loop.
Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1086
  • View blog
  • Posts: 4,559
  • Joined: 09-June 09

Re: Kbhit question.

Posted 05 May 2013 - 01:52 PM

kbhit is not apart of ANSI C standard, why can't you use scanf, or getchar ?
Was This Post Helpful? 1
  • +
  • -

#7 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2128
  • View blog
  • Posts: 3,262
  • Joined: 21-June 11

Re: Kbhit question.

Posted 05 May 2013 - 02:54 PM

View Postjjl, on 05 May 2013 - 10:52 PM, said:

kbhit is not apart of ANSI C standard, why can't you use scanf, or getchar ?


Because those would block, I imagine. Also they're line buffered.
Was This Post Helpful? 0
  • +
  • -

#8 lewm  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 160
  • Joined: 29-March 13

Re: Kbhit question.

Posted 06 May 2013 - 06:36 AM

View Postjjl, on 05 May 2013 - 01:52 PM, said:

kbhit is not apart of ANSI C standard, why can't you use scanf, or getchar ?

I don't want to wait until key has been pressed but I do need to check if one has been. Is there another method similar to kbhit?
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is online

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,866
  • Joined: 25-December 09

Re: Kbhit question.

Posted 06 May 2013 - 06:47 AM

There is no standard replacement for kbhit(), only non-standard operating system specific methods. Just remember that kbhit() should only be used in a console program.

Jim
Was This Post Helpful? 1
  • +
  • -

#10 lewm  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 160
  • Joined: 29-March 13

Re: Kbhit question.

Posted 06 May 2013 - 06:50 AM

View Postjimblumberg, on 06 May 2013 - 06:47 AM, said:

There is no standard replacement for kbhit(), only non-standard operating system specific methods. Just remember that kbhit() should only be used in a console program.

Jim

Ok thanks, im only making console programs at the minute anyway. If you needed this sort of function in lets say an application how would you achieve this?
Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is online

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,866
  • Joined: 25-December 09

Re: Kbhit question.

Posted 06 May 2013 - 08:04 AM

Quote

If you needed this sort of function in lets say an application how would you achieve this?


What type of application? What operating system? What are the actual requirements?

I would first try to find a solution using standard functions. If standard functions are deficient in some way I would then try to find some standard non-standard way of getting the job done. This means if I'm writing a console application for DOS I may think about using kbhit(), if using Windows I would first try to find some Windows API call that may do the job. If I can't find a standard, standard non-standard function or system API call that does the job I'll investigate writing a function myself.

Jim
Was This Post Helpful? 1
  • +
  • -

#12 l0z  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-June 13

Re: Kbhit question.

Posted 17 June 2013 - 07:09 PM

Hi I don't understand the line while((a=kbhit())) will this get the key that was pressed?
Was This Post Helpful? 0
  • +
  • -

#13 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2128
  • View blog
  • Posts: 3,262
  • Joined: 21-June 11

Re: Kbhit question.

Posted 18 June 2013 - 04:57 AM

No, kbhit only returns true or false, which is why the original code did not work.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1