468,513 Members | 1,766 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

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 7947
Banfa
9,056 Expert Mod 8TB
Understanding that this is platform specific to Windows,

Expand|Select|Wrap|Line Numbers
  1. char c;
  2.  
  3. for(;;)
  4. {
  5.     if (_kbhit() != 0)
  6.     {
  7.         c = (char)_getch();
  8.  
  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. }
  20.  
Oct 16 '06 #2
vivicio047,

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
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.