Título: Lendo teclas de função
Linguagem: C/C++
S.O.: DOS
Autor(es): Wenderson Teixeira


Lendo teclas de função


ReadKey.h

#ifndef _READKEY_H
#define _READKEY_H

#define BKSPC   0x08
#define TAB     0x09
#define LF      0x0A
#define CR      0x0D
#define ESC     0x1B

#define F1      0x3B00
#define F2      0x3C00
#define F3      0x3D00
#define F4      0x3E00
#define F5      0x3F00
#define F6      0x4000
#define F7      0x4100
#define F8      0x4200
#define F9      0x4300
#define F10     0x4400
#define F11     0x8500
#define F12     0x8600

#define UP      0x4800
#define LEFT    0x4B00
#define RIGHT   0x4D00
#define DOWN    0x5000

#define HOME    0x4700
#define END     0x4F00
#define PAGEUP  0x4900
#define PAGEDN  0x5100
#define INSERT  0x5200
#define DELETE  0x5300

#ifdef __cplusplus
extern "C" {
#endif

int ReadKeyWait(void);
int ReadKey(void);

#ifdef __cplusplus
}
#endif

#endif

ReadKey.cpp

#include <conio.h>

int ReadKeyWait(void)
{
  int ch = getch();
  return ch ? ch : getch() << 8;
}

int ReadKey(void)
{
  return kbhit() ? ReadKeyWait() : 0;
}


Teste.c

#include <stdio.h>
#include "ReadKey.h"

void main()
{
  unsigned Key;

  printf("Digite uma tecla para ver seu valor, ESC p/ Sair\n\n");
  do
  {
    Key = ReadKey();
    if(Key)
    {
      switch(Key)
      {
        case UP: printf("Up");
        break;
        case LEFT: printf("Left");
        break;
        case RIGHT: printf("Right");
        break;
        case DOWN: printf("Down");
        break;
        
        default:
          printf("%s - %3ud  %02Xh\t  %c\n", 
                 (Key > 255 ? "ctrl" : "char"),
                 Key, Key, (Key > 256 ? Key >> 8 : Key));
      }
    }
      
  } while (Key != ESC);
}





1