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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

keyboard values

Status
Not open for further replies.

Denster

Programmer
Apr 9, 2001
198
GB
I'm trying to write a program that uses the arrow keys on the keyboard when it is run. What is the best way to this, I want to use the getch() function but what value do I put in to recognise the arrow keys ?.
 
since all the characters on the keyboard are stored as an ascii number, try doing this

while (c != 'x')
{
int c;
c = getch();
printf("%d",c);
}


this will make it so that everytime you push a key other than "x" it will tell you the ascii numeric value for that key, once you find out that numeric value (say it's 20) then...

case 20:
printf("%c",c);

or something along those lines..

Rob
 
Yeh! already tried that. it just gives back a value of 0 for all the arrow keys. I'll keep trying
 
I don't belive this is as easly as calling getch(). My understanding is that the arrow keys on the keyboard are amongst a group called accelerator keys. When one of these get pressed they get sent to the message pump which controls containers. How ever if you are writing in C++ you can intercept the arrow key event.
If your application is for the microsoft environment you will have to get into MFC to intercept the event before it carries out its assigned tasks.
If your application is for an environment such as MS DOS you may need to write a keyboard driver.
The following are some sites that may help you:
1.Explains what happens accelerator keys.
2.Explains how you can use the arrow keys..(lower level coding)
Have fun.... this seems to be quiet a challenge.
 
yeah, now that i think of it, that doesn't work for arrow keys, heh...you could always use the number pad as your arrows, to make things simpler...

Rob
 
Under DOS try something like this:

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
 
Superb, thats just what I've been looking for, thankyou.
I've never used an if statement like that before, is it common practice.
if (a == 0 ) { a = getch() + 0x100; }
 
its aint 100 . Its 0x100 == decimal 256
Do not rejoice that ur code works.
it might be a special case of an error :-(
 
Hello sir
I am doing my Bachealor of Engineering
(Computer Science) and in my project i need to
implement a Database in C. I need some help regarding
a small module where i need to ask System users to
enter their Login name and Password to validate
them.But the Recquirement is that the Password field
should appear as &quot;*&quot; on the screen. How do i implement
this in C language so that password entered by the
user appear as '*' instead of text on the console.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top