Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using keyboard routines in C

Status
Not open for further replies.

newbieatassembly

Technical User
Oct 4, 2000
39
0
0
IN
Hi,
I was trying to use keyboards routines using inportb(0x60) function for Left Arrow, right Arrow but failing miserable.

This is what I was trying to (simple animation stuffs).

Plot pixels so that a blue sqaure of 10x10 is displayed on screen. Now if Left arrow is pressed the sqaure should move left. If right arrow key is pressed the square should move right.

Anyone know exactly how to do this using arrow keys. Well if required I'll post the code but probably its going terribly wrong and printing all characters instead. Probably I am not getting the tail stuff of the keyboard.
If you have time to give some code or give an asm code, if you have time please do explain.

Thank you
 
the Arrow keys have extented code, so if you use the getch() or getchar() routines, for getting the code for the arrow you should call them twice (this goes for F... keys, home,end,pgup etc)

So, the program loop should look like:

Code:
char c;

.
.
.

while ((c = getchar()) != 27) /* that is exit when hitting escape */
{ if (c == 0)
     c = getchar();
  switch (c)
    {case 77: Do stuff... 
    }  
}
I guess 77 will be the up arrow key or something... you'll figure it out yourself.

Hope that helped.
 
Here is another way (Very simular to Nosferatu's) of getting the extended keys along with the regular keys.

Code:
#include <stdio.h>
#include <conio.h>

int main(){
int a;

printf(&quot;Press escape to quit!\n\n&quot;);
while(1)
 {
 a = getch();                          /* Get key press and   */
 if (a == 0 ) { a = getch() + 0x100; } /* Add 256 if extended */
 switch ( a )
  {
  case 27:                           /* If escape key */
  printf( &quot;Escape key. Exiting...\n&quot; );
  return 0;
  case 315:                          /* If F1 key */
  printf(&quot;Help me!\n&quot;);
  break;
  case 328:                          /* If Up  Arrow key */
  printf(&quot;Up  Arrow key\n&quot;);
  break;
  case 336:                          /* If Down Arrow key */
  printf(&quot;Down Arrow key\n&quot;);
  break;
  case 333:                          /* If Right  Arrow key */
  printf(&quot;Right  Arrow key\n&quot;);
  break;
  case 331:                          /* If Left Arrow key */
  printf(&quot;Left Arrow key\n&quot;);
  break;
  default:
  printf(&quot;Key code is --> %d\n&quot;, a);
  }
 }
}


Kim_Christensen@telus.net
 
Hey zBuilder and Nosfaratu,

Thanks, got it working now.

Now I am wondering how to do it in Linux console. Tried the same thing but it prints [[c, ]]d, etc. The scancodes are different or is it the code ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top