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


Lendo teclas de função


LeTecla.pas
Unit LeTecla;

Interface

const BKSPC   = $08;
const TAB     = $09;
const LF      = $0A;
const CR      = $0D;
const ESC     = $1B;

const F1      = $3B00;
const F2      = $3C00;
const F3      = $3D00;
const F4      = $3E00;
const F5      = $3F00;
const F6      = $4000;
const F7      = $4100;
const F8      = $4200;
const F9      = $4300;
const F10     = $4400;
const F11     = $8500;
const F12     = $8600;

const UP      = $4800;
const LEFT    = $4B00;
const RIGHT   = $4D00;
const DOWN    = $5000;

const HOME    = $4700;
const ENDKEY  = $4F00;
const PAGEUP  = $4900;
const PAGEDN  = $5100;
const INSERT  = $5200;
const DELETE  = $5300;

function ReadKeyWait : integer;
function ReadKeyEx : integer;

Implementation

uses
  CRT;

function ReadKeyWait : integer;
var
  ch : char;
begin
  ch := ReadKey;
  if ch = chr(0) then
    ReadKeyWait := ord(ch) or (ord(ReadKey) shl 8)
  else
    ReadKeyWait := ord(ch);
end;

function ReadKeyEx : integer;
begin
  if KeyPressed then
    ReadKeyEx := ReadKeyWait
  else
    ReadKeyEx := 0;
end;

end.

Teste.pas
program Teste;
uses
  CRT, LeTecla;

var
  value : integer;

begin
  writeln('Digite uma tecla para ver o seu valor, ESC p/ sair.');
  repeat
    value := ReadKeyEx;
    
    if value <> 0 then
    begin
      if value >= 256 then
        case value of
          { setas }
          UP: writeln('Up');
          LEFT: writeln('Left');
          RIGHT: writeln('Right');
          DOWN: writeln('Down');
        else
          { outras teclas extendidas (F1..F10, Insert, Delete, End...) }
          writeln('*: ', value);
        end
      else
        { Teclas normais }
        writeln(chr(value));
    end;
  { Se foi digitado ESC, sai do loop }
  until value = ESC;
end.




1