Advertisement
jacknpoe

Exemplo (ruim) de tratamento de teclas 2

Feb 13th, 2014
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 5.81 KB | None | 0 0
  1. // ###  A V I S O  ###
  2. // Os códigos abaixo são LEGADOS e feitos "pra funcionar" em momentos de desespero em compiladores antigos e
  3. // quando não haviam "engines" e documentação sobre o assunto. Não faça isso em casa
  4.  
  5. —————————————————————————————————————————————————————————————————————————————————————————————————————————————————
  6. // ********** DROPS 3 - ZICAU SOFTWARE LIMITADA - ITAJAI, SC - ago/2000 - em Delphi **********
  7. —————————————————————————————————————————————————————————————————————————————————————————————————————————————————
  8.  
  9. NA INICIALIZAÇÃO:
  10.  
  11.   KeyCode[1] := 13;
  12.   KeyCode[2] := 32;
  13.   KeyCode[3] := 37;
  14.   KeyCode[4] := 38;
  15.   KeyCode[5] := 39;
  16.   KeyCode[6] := 40;   // Inicializa o código das teclas interceptadas
  17.  
  18.   KeyTime[1] := 6;
  19.   KeyTime[2] := 1;
  20.   KeyTime[3] := 3;
  21.   KeyTime[4] := 4;
  22.   KeyTime[5] := 3;
  23.   KeyTime[6] := 4;    // Inicializa o tempo das teclas interceptadas
  24.  
  25.  
  26.  
  27. procedure TPrincipal.FormKeyDown(Sender: TObject; var Key: Word;
  28.   Shift: TShiftState);
  29. var
  30.   temp: integer;
  31. begin
  32.   If ( Key = 8) AND ( CampoMinado = 144) then
  33.   begin
  34.     Inicial.Show;
  35.     Key := 0;
  36.   end;
  37.   If StatusClock = 0  then exit;  // Se o status for parado então sai
  38.   If Key = 19 then
  39.   begin
  40.     FazPause;
  41.     exit;
  42.     Key := 0;
  43.   end;
  44.   If Pause then exit;
  45.   For temp := 1 to 6 do           // Para todas as teclas...
  46.     If ( Key = KeyCode[temp])then // verificamos se o código bate...
  47.     begin
  48.       Key := 0;                   // zeramos o caractere...
  49.       if not KeyPres[temp] then   // e verificamos se não está pressionada
  50.       begin
  51.         KeyPres[temp] := True;                              // Se tudo Ok, ela agora está pressionada...
  52.         KeySoma[temp] := 0;                                  // a soma dos ciclos em StandBy é 0...
  53.         Key := 0;
  54.         If (StatusClock = 1) AND (temp <> 2) then ExecutaTeclas( temp);  // e executamos a tecla se o status é normal...
  55.       end;
  56.     end;
  57. end;
  58.  
  59.  
  60. procedure TPrincipal.FormKeyUp(Sender: TObject; var Key: Word;
  61.   Shift: TShiftState);
  62. var
  63.   temp: integer;
  64. begin
  65.   For temp := 1 to 6 do          // Para todas as teclas...
  66.     If Key = KeyCode[temp] then  // verificamos se o código bate...
  67.     begin
  68.       Key := 0;                  // zeramos o caractere...
  69.       KeyPres[temp] := False;    // e então a despressionamos
  70.     end;
  71. end;
  72.  
  73.  
  74. procedure TPrincipal.TimerKeyTimer(Sender: TObject);
  75. var
  76.   temp: integer;
  77. begin
  78.   If StatusClock = 0  then exit;  // Se o status for parado então sai
  79.   if Pause then exit;
  80.   For temp := 1 to 6 do           // Para todas as teclas...
  81.     If KeyPres[temp] then         // se está pressionada...
  82.       Inc( KeySoma[temp]);        // incrementa a soma de ciclos
  83.   If StatusClock = 2  then exit;  // Se o status for bloqueado então sai
  84.   For temp := 1 to 6 do           // Para todas as teclas...
  85.     If KeyPres[temp] then         // se está pressionada...
  86.       If KeySoma[temp] >= KeyTime[temp] then  // e se o tempo já foi...
  87.       begin
  88.         Dec( KeySoma[temp], KeyTime[temp]);          // decrementa-se o tempo esperado
  89.         If StatusClock = 3 then KeySoma[temp] := 0;  // Se estava bloqueado libera-se o buffer do tempo excedido
  90.         ExecutaTeclas( temp);
  91.       end;
  92.  If StatusClock = 3 then StatusClock := 1;   // Se agora o status é saindo de bloqueio, no próximo será normal
  93. end;
  94.  
  95.  
  96. procedure TPrincipal.SetNormal;
  97. var
  98.   temp: integer;
  99. begin
  100.   For temp := 1 to 6 do
  101.   begin
  102.     KeyPres[temp] := False;
  103.     KeySoma[temp] := 0;        // Zera as teclas para evitar pressionamento indevido
  104.   end;
  105.   [...]
  106. end;
  107.  
  108.  
  109.  
  110. EXEMPLO DE (MAU) USO:
  111.  
  112. procedure TPrincipal.ExecutaTeclas(Numero: integer);
  113. var
  114.   Peca: TPeca1;
  115. begin
  116.   if not Jogando then exit;
  117.   if Pause then exit;
  118.  
  119.     Peca := TPeca1.Cria;
  120.     Case Numero of
  121.       1: begin
  122.            MProximo := not MProximo;    // A tecla enter muda o próximo
  123.            Mostradores;                 // E executa os mostradores
  124.          end;
  125.       2: begin
  126.            ExecutaCiclo1;               // O espaço obriga o jogo a executar ciclos rapidamente
  127.          end;
  128.       3: begin
  129.            if Jogo_1.SePossivel( Jogo_1.PAtual, Jogo_1.PosX-1, Jogo_1.PosY) then
  130.            begin
  131.              Jogo_1.PosX := Jogo_1.PosX-1;   // se for possível muda a peca para a esquerda
  132.              Refresca1;
  133.            end;
  134.          end;
  135.       4: begin
  136.            Peca.CopiaDe( Jogo_1.PAtual);
  137.            Peca.Gira( antihorario);   // copia e tenta girar a peca
  138.            if Jogo_1.SePossivel( Peca, Jogo_1.PosX, Jogo_1.PosY) then
  139.            begin
  140.              Jogo_1.PAtual.CopiaDe( Peca);  // se for possível muda a peca para no sentido antihorário
  141.              Refresca1;
  142.            end;
  143.          end;
  144.       5: begin
  145.            if Jogo_1.SePossivel( Jogo_1.PAtual, Jogo_1.PosX+1, Jogo_1.PosY) then
  146.            begin
  147.              Jogo_1.PosX := Jogo_1.PosX+1;   // se for possível muda a peca para a direita
  148.              Refresca1;
  149.            end;
  150.          end;
  151.     else
  152.       begin
  153.         Peca.CopiaDe( Jogo_1.PAtual);
  154.         Peca.Gira( horario);          // copia e tenta girar a peca
  155.         if Jogo_1.SePossivel( Peca, Jogo_1.PosX, Jogo_1.PosY) then
  156.         begin
  157.           Jogo_1.PAtual.CopiaDe( Peca);     // se for possível muda a peca para no sentido horário
  158.           Refresca1;
  159.         end;
  160.       end;
  161.     end;
  162.     Peca.Destroi;
  163.  
  164. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement