-
Up/down arrows
Hi! I have a small piece of code that checks the number (code) of an inputted character and later, for example, prints something. My idea is for the user to be able to navigate through the display only with the up and down arrows, and now this code:
Code:
do {
c = getch();
}
while (c!=0);
if(c==0)
{
c = getch(); // retrieve second byte of extended code
c *= 256; // shift left by 8 bits to get extended code
printf( "Extended keystroke: %X\n", c );
}
The problem is that it will check if the first byte is 0, however I don't know if it's possible to limit the pressing of buttons like End, Pg Dn, Home, Pg Up etc... ? I can write after this: if (c==0x4800) printf("up"); else if (c==0x5000) printf("down"); Perhaps it's an easy task and I just need some rest before finishing this, but the fact is that at this moment I have no solution. Glad, if you could help.
-
What is your compiler and OS?
This is a program that works on WindowsXP, *if* you have the header file conio.h. If you don't have it, or can't get it, then I'd suggest using the Microsoft API for this.
Code:
#include <stdio.h>
#include <conio.h>
#define ESC 27
#define F1 59
#define F2 60
#define F3 61
#define F4 62
#define F5 63
#define F6 64
#define F7 65
#define F8 66
#define F9 67
#define F10 68
#define HOME 71
#define UP 72
#define PAGE_UP 73
#define LEFT 75
#define RIGHT 77
#define END 79
#define DOWN 80
#define PAGE_DOWN 81
int main(void) {
char key;
char msg[20];
printf("\n\n\t\t\t press escape to quit\n\n") ;
do {
key = getch();
if (key == 0) {
key = getch(); //key code has two keys - read the second one
switch (key) {
case F1: memcpy(msg,"F1", sizeof(msg)); break;
case F2: memcpy(msg,"F2", sizeof(msg)); break;
case F3: memcpy(msg,"F3", sizeof(msg)); break;
case F4: memcpy(msg,"F4", sizeof(msg)); break;
case F5: memcpy(msg,"F5", sizeof(msg)); break;
case F6: memcpy(msg,"F6", sizeof(msg)); break;
case F7: memcpy(msg,"F7", sizeof(msg)); break;
case F8: memcpy(msg,"F8", sizeof(msg)); break;
case F9: memcpy(msg,"F9", sizeof(msg)); break;
case F10: memcpy(msg,"F10", sizeof(msg)); break;
case PAGE_UP: memcpy(msg,"PAGE UP", sizeof(msg)); break;
case PAGE_DOWN: memcpy(msg,"PAGE DOWN", sizeof(msg)); break;
case HOME: memcpy(msg,"HOME", sizeof(msg)); break;
case END: memcpy(msg,"END", sizeof(msg)); break;
case UP: memcpy(msg,"UP", sizeof(msg)); break;
case DOWN: memcpy(msg,"DOWN", sizeof(msg)); break;
case LEFT: memcpy(msg,"LEFT", sizeof(msg)); break;
case RIGHT: memcpy(msg,"RIGHT", sizeof(msg)); break;
default: memcpy(msg,"unknown key", sizeof(msg)); break;
}
printf("\n Key: %s", msg);
continue;
}
if(key == ESC)
printf("\n Key: ESCAPE");
else
printf("\n Key: %c", key);
}while (key != ESC);
printf("\n\n\t\t\t press enter when ready");
key = getchar(); //hold the console window open
return 0;
}
I wanted to practice with using memcpy, so it looks a bit odd, but you should get the idea. You can just remove the memcpy and print the key, right from that same place, inside the switch statement.
-
-
Another thing:
Code:
while (1){
do
c = getch();
while (c!=0);
c = getch()*256;
if (c==0x4800)
printf("up");
else if (c==0x5000)
printf("down");
else if (c==0x0D)
break;
}
why doesn't [Enter] (0x0D) work in this case?