How can we get scan code in C using gcc compiler

Reply

Join Date: Oct 2006
Posts: 38
Reputation: asifjavaid is an unknown quantity at this point 
Solved Threads: 0
asifjavaid asifjavaid is offline Offline
Light Poster

How can we get scan code in C using gcc compiler

 
0
  #1
Sep 24th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

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

 
0
  #2
Sep 24th, 2008
  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
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

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

 
0
  #3
Sep 24th, 2008
Read up on ncurses if you want advanced control of a terminal environment.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 113
Reputation: ahamed101 is an unknown quantity at this point 
Solved Threads: 14
ahamed101's Avatar
ahamed101 ahamed101 is offline Offline
Junior Poster

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

 
0
  #4
Sep 28th, 2008
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...
regards,
Ahamed.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 241
Reputation: ssharish2005 is on a distinguished road 
Solved Threads: 20
ssharish2005's Avatar
ssharish2005 ssharish2005 is offline Offline
Posting Whiz in Training

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

 
0
  #5
Sep 28th, 2008
Here is a link which could give you some hint!!!

ssharish
"Any fool can know, point is to understand"
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC