Title: How to capture keyboard inputs?
Question: How to capture keyboard inputs?
Answer:
unit Unit4;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls,
Forms, Dialogs,
StdCtrls;
type
TForm4 = class(TForm)
Button1: TButton;
procedure FormShow(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
OldWndMeth, NewWndMeth: Pointer;
public
{ Public declarations }
end;
var
Form4: TForm4;
he:Thandle;
implementation
{$R *.DFM}
var
OldWndProc: Pointer = nil;
function NewWinProc (Handle: THandle;
Msg, wParam, lParam: LongInt): LongInt; stdcall;
begin
if Msg = wm_keydown then
begin
Beep;
SetWindowText (Handle,
PChar (Format (' %s', [char(lParam)])));
end;
// pass call to old window proc
Result := CallWindowProc (OldWndProc, Handle,
Msg, wParam, lParam);
end;
procedure TForm4.FormShow(Sender: TObject);
begin
he:=createwindow('edit','edit',ws_border or ws_visible or
ws_child,10,10,
100,40,handle,0,hinstance,0);
end;
procedure TForm4.Button1Click(Sender: TObject);
begin
OldWndProc := Pointer (SetWindowLong
(he, gwl_WndProc, LongInt (@NewWinProc)));
end;
end.