943,595 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3161
  • C RSS
Sep 24th, 2008
0

How can we get scan code in C using gcc compiler

Expand Post »
hi guys,

working in C using Borland Compiler we can get scan codes of keyboards keys by this code

  1. ch = getch();
  2. if (ch == 0)
  3. ch = getch();
  4.  
  5. printf("%d",ch);

because when first time getch() is called, it will return the ascii code of the pressing key. when second time it called then it will return the scan code of that pressing key. So when we want to control our program with arrow keys, there ascii codes are 0 but scan codes are different.

Now while working in linux environment using gcc compiler. there is no conio.h header file in it and we have such function getch(). so how can we get the scan code for arrows and functional keys in C using gcc compiler.

Many Thanks

Asif
Reputation Points: 10
Solved Threads: 0
Light Poster
asifjavaid is offline Offline
40 posts
since Oct 2006
Sep 24th, 2008
0

Re: How can we get scan code in C using gcc compiler

  1. #include <stdio.h>
  2. #include <termios.h>
  3. #include <unistd.h>
  4.  
  5. int getch(){
  6. struct termios oldt,
  7. newt;
  8. int ch;
  9. tcgetattr( STDIN_FILENO, &oldt );
  10. newt = oldt;
  11. newt.c_lflag &= ~( ICANON | ECHO );
  12. tcsetattr( STDIN_FILENO, TCSANOW, &newt );
  13. ch = getchar();
  14. tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
  15.  
  16. return ch;
  17. }

It works for me. Enjoy
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Sep 24th, 2008
0

Re: How can we get scan code in C using gcc compiler

Read up on ncurses if you want advanced control of a terminal environment.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Sep 28th, 2008
0

Re: How can we get scan code in C using gcc compiler

Hi,
If its ok the character is echoed on the screen, you can very well go for getc(stdin) or getchar() in a while loop with the termminating condition like carriage return or something you want...
Reputation Points: 51
Solved Threads: 14
Junior Poster
ahamed101 is offline Offline
114 posts
since Jul 2008
Sep 28th, 2008
0

Re: How can we get scan code in C using gcc compiler

Here is a link which could give you some hint!!!

ssharish
Reputation Points: 73
Solved Threads: 20
Posting Whiz in Training
ssharish2005 is offline Offline
253 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: C Programming
Next Thread in C Forum Timeline: Frames in C ...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC