Identificando as teclas pressionadas

 

A cada vez que precisava verificar se as teclas ALT, CTRL e Shift estão pressionadas, eu escrevia uma rotina, é bem simples e curta, dependendo do evento até temos uma variável que auxilia, mas ontem resolvi escrever uma unit para padronizar a verificação de estado destas teclas, e aproveitei para incluir também rotinas para testar se a tecla pressionada é a da direita ou esquerda do teclado:

// Any side keys
function IsAltKeyDown: boolean;
function IsCtrlKeyDown: boolean;
function IsShiftKeyDown: boolean;

// Left side keys
function IsLeftAltKeyDown: boolean;
function IsLeftCtrlKeyDown: boolean;
function IsLeftShiftKeyDown: boolean;

// right side keys
function IsRightAltKeyDown: boolean;
function IsRightCtrlKeyDown: boolean;
function IsRightShiftKeyDown: boolean;

Chamei esta unit de JazzKeysUtils.pas, e nela incluirei todas as rotinas relacionadas a manipulação de teclas e teclado.

Outra necessidade imediata, é de testar se uma tecla pressionada é de um determinado caracter, sem a necessidade de testar o valor correspondente maiúsculo ou minúsculo, bem como poder passar como Char ou o valor correspondende em Word:

// Test if a key is the desired char ignoring case
function IsCharKey(Key, Value: Char): boolean; overload;
function IsCharKey(Key: Word; Value: Char): boolean; overload;

No exemplo a seguir, vou testar se a tecla “CTRL + C” ou “CTRL + A”:

procedure TFormAppMain.EditXMLDataKeyUp(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  if IsCtrlKeyDown then
  begin
    if IsCharKey(Key, 'C') then
      Clipboard.AsText:= EditXMLData.Field.AsString
    else
    if IsCharKey(Key, 'A') then
      EditXMLData.SelectAll;
  end;
end;

Veja a listagem completa da unit JazzKeysUtils.pas aqui.

 
 
 

0 Comments

 

You can be the first one to leave a comment.

 

Leave a Comment

 




XHTML: You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>