Jump to content
Fórum Script Brasil
  • 0

(Resolvido) Problema ao pegar teclas para macro


njorge

Question

Estou desenvolvendo um programa e percebi que ao capiturar as teclas eu obitive um problema, ao capiturar ele capiturava 2 vezes, exemplo: eu digitava "oi", ele capiturava "ooii" :x

Ainda to aprendendo api e limitei meu macro para capiturar apenas no meu programa por enquanto... segue o código:

var
MacroHook: HHook;
  function KeyboardHookProc(Code: Integer; WordParam: Word; LongParam: LongInt): LongInt; stdcall;
implementation
{$R *.dfm}


procedure TForm1.Button1Click(Sender: TObject);
begin
MacroHook:=SetWindowsHookEx(WH_KEYBOARD, @KeyboardHookProc, HInstance, GetCurrentThreadId());

end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
UnHookWindowsHookEx(MacroHook);
end;


 function KeyboardHookProc(Code: Integer; WordParam: Word; LongParam: LongInt) : LongInt;
 begin
  case WordParam of //apenas para ver o que o macro está capiturando
   vk_Space: form1.Memo1.Text := form1.Memo1.Text + ' ';
   vk_Up: form1.Memo1.Text := form1.Memo1.Text + 'UP';
  end;
  Result:=0;

 end;

Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0
percebi que ao capturar as teclas eu obtive um problema, ao capturar ele capturava 2 vezes

vi que voce não conseguiu a resposta em outros foruns ... para resolver o problema, voce deve desativar a interceptação de teclado, depois de teclar

exemplo:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    procedure FormDestroy(Sender: TObject);
    procedure Memo1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure Memo1KeyPress(Sender: TObject; var Key: Char);
    procedure Memo1KeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  MacroHook: HHook;
  function KeyboardHookProc(Code: Integer; WordParam: Word; LongParam: LongInt): LongInt; stdcall;

implementation

{$R *.dfm}

function KeyboardHookProc(Code: Integer; WordParam: Word; LongParam: LongInt) : LongInt;
begin
   case WordParam of
     vk_Return: Form1.Memo1.Lines.Add( 'vk_return' );
     vk_Space: Form1.Memo1.Lines.Add( 'vk_space' );
     vk_Right: Form1.Memo1.Lines.Add( 'vk_right' );
     vk_Left: Form1.Memo1.Lines.Add( 'vk_left' );
     vk_Up: Form1.Memo1.Lines.Add( 'vk_up' );
     vk_Down: Form1.Memo1.Lines.Add( 'vk_down' );
   else
     Form1.Memo1.Lines.Add( format( '[%d]',[WordParam] )+ ' ');
   end; {case}
   Result:=0;

end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
   UnHookWindowsHookEx(MacroHook);
end;

procedure TForm1.Memo1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
   UnHookWindowsHookEx(MacroHook);
end;

procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char);
begin
   UnHookWindowsHookEx(MacroHook);
end;

procedure TForm1.Memo1KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
   UnHookWindowsHookEx(MacroHook);
   MacroHook:=SetWindowsHookEx(WH_KEYBOARD, @KeyboardHookProc, HInstance, GetCurrentThreadId());
end;

end.

os links abaixo, vão ajudar a melhorar o código

http://www.ramosdainformatica.com.br/art_r...s01.php?CDA=452

http://en.allexperts.com/q/Delphi-1595/set...owsHookEx-1.htm

http://www.mail-archive.com/delphi@delphi....z/msg02198.html

http://www.delphifaq.com/faq/delphi_windows_API/f512.shtml

abraço

Link to comment
Share on other sites

  • 0

No exemplo anterior, a captura é feita em cima do Memo

nesse é feita em cima do form

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormKeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  MacroHook: HHook;
  function KeyboardHookProc(Code: Integer; WordParam: Word; LongParam: LongInt): LongInt; stdcall;

implementation

{$R *.dfm}

function KeyboardHookProc(Code: Integer; WordParam: Word; LongParam: LongInt) : LongInt;
begin
   case WordParam of
     vk_Return: Form1.Memo1.Lines.Add( 'vk_return' );
     vk_Space: Form1.Memo1.Lines.Add( 'vk_space' );
     vk_Right: Form1.Memo1.Lines.Add( 'vk_right' );
     vk_Left: Form1.Memo1.Lines.Add( 'vk_left' );
     vk_Up: Form1.Memo1.Lines.Add( 'vk_up' );
     vk_Down: Form1.Memo1.Lines.Add( 'vk_down' );
   else
     Form1.Memo1.Lines.Add( format( '[%d]',[WordParam] )+ ' ' + chr(WordParam));
   end; {case}
   Result:=0;

end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
   UnHookWindowsHookEx(MacroHook);
end;

procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
   UnHookWindowsHookEx(MacroHook);
   MacroHook:=SetWindowsHookEx(WH_KEYBOARD, @KeyboardHookProc, HInstance, GetCurrentThreadId());
end;

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
    UnHookWindowsHookEx(MacroHook);
end;

procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
    UnHookWindowsHookEx(MacroHook);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
   KeyPreview := true;
end;

end.

abraço

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.2k
    • Total Posts
      652k
×
×
  • Create New...