471,850 Members | 1,056 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,850 software developers and data experts.

looking for a combi function of kbhit() and getch()

please help me !
1) i m trying to move a character using the arrow key, but dont know how to scan the arrow key.
2) form a while(1) loop i have to break, when a key is pressed(cant use getch() as the while loop should break only on pressing a key and should not wait for a user i/p all the time in the loop), and also know the what key was pressed. how do i achieve this?
Oct 16 '06 #1
2 8130
9,065 Expert Mod 8TB
Understanding that this is platform specific to Windows,

Expand|Select|Wrap|Line Numbers
  1. char c;
  3. for(;;)
  4. {
  5.     if (_kbhit() != 0)
  6.     {
  7.         c = (char)_getch();
  9.         if (c==0 || c==0xE0)
  10.         {
  11.             c = (char)_getch();
  12.             // Handle arrow key hit c
  13.         }
  14.         else
  15.         {
  16.             // Handle normal key hit c
  17.         }
  18.     }
  19. }
Oct 16 '06 #2

You should consider that several keys (F1,...F12, arrow keys) will produce two bytes, not one. The first of these 2 bytes is 0x00, the second is key specific.
In case a call of getch function produces 0x00, a second call a second call of getch is necessary.
Nov 22 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Hans Georg Krauthaeuser | last post: by
2 posts views Thread by Shugong Wang | last post: by
6 posts views Thread by exquisitus | last post: by
1 post views Thread by Carsten Orthbandt | last post: by
9 posts views Thread by Arun | last post: by
45 posts views Thread by simnav | last post: by
1 post views Thread by SpecialKay | last post: by
reply views Thread by NeoPa | last post: by
reply views Thread by aboka | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.