Error: Types of actual and formal var parameters must be identical
unit unAutoKeypress;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Memo2: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure SimulateKeyDown(Key:byte);
begin
keybd_event(Key,0,0,0);
end;
procedure SimulateKeyUp(Key:byte);
begin
keybd_event(Key,0,KEYEVENTF_KEYUP,0);
end;
procedure doKeyPress(var KeyValue:byte);
begin
SimulateKeyDown(KeyValue);
SimulateKeyUp(KeyValue);
end;
procedure TForm1.Button1Click(Sender: TObject);
const test = 'merry Christmas!';
var m: byte;
begin
Memo2.SetFocus();
m:=$13;
doKeyPress(m); // THIS IS WHERE ERROR
end;
end.
always error in function doKeyPress(m);
a simple question, why?
I know something wrong with types, but all types are similar, everywhere is byte, strange
for me and I cant run a program.
The problem is that
doKeyPressis a method ofTForm1(inherited fromTWinControl) and so when you writedoKeyPressinside aTForm1method the compiler wants to useTForm1.doKeyPressrather than the local function. The class scope is nearer than the local function scope.Possible solutions include:
unAutoKeypress.doKeyPress.The former is a better solution in my opinion.