delphi – 如何在编辑框中检测Enter键?

前端之家收集整理的这篇文章主要介绍了delphi – 如何在编辑框中检测Enter键?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个带有发送按钮的UDP信使,但是我不得不按下按钮而不是按Enter键.所以我创建了程序TForm1.Edit2KeyPress.现在我不知道如何在if(按下输入)然后{code for sending message}中定义Enter按钮.

回答后我有新问题.我输入的任何内容都是逐字母发送的.这是我的代码
单位聊天1;

interface

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

type
  TForm1 = class(TForm)
   Button1: TButton;
   Memo1: TMemo;
Edit1: TEdit;
Button2: TButton;
Edit2: TEdit;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Edit2KeyPress(Sender: TObject; var Key: Char);
private
Activated: Boolean;
procedure SearchEvent(ResultIP,ResultName: String);
procedure UDPRead(Sender: TObject; AData: TStream;
  ABinding: TIdSocketHandle);
procedure UDPException(Sender: TObject);
public
end;

var
Form1: TForm1;
ss:string  ;

implementation

uses UDP;

{$R *.dfm}



procedure TForm1.Button1Click(Sender: TObject);
begin
  UDPSearchForm.SearchEvent := SearchEvent;
  UDPSearchForm.Left := Left;
  UDPSearchForm.Top := Top;
  UDPSearchForm.AktIP := Edit1.Text;
UDPSearchForm.SearchPartner;
end;

procedure TForm1.SearchEvent(ResultIP,ResultName: String);
begin
  Edit1.Text := ResultIP;
  Label1.Caption := ResultName;
end;

procedure TForm1.FormActivate(Sender: TObject);
var
  s,s2: String;
begin
  if Activated then exit;
  Memo1.Clear;
  Activated := true;
  UDPSearchForm.OnUDPRead := UDPRead;
  UDPSearchForm.OnException := UDPException;
  UDPSearchForm.Active := true;
  s := UDPSearchForm.LocalAddress;
  s2 := UDPSearchForm.WSGetHostByAddr(s);
  Memo1.Lines.Add('I''m (' + s + ') ' + s2);
end;

procedure TForm1.UDPRead(Sender: TObject; AData: TStream;
   ABinding: TIdSocketHandle);
var
  Buffer: Array [0..2047] of Byte;
  count: Integer;
  PeerIP: String;
  PeerPort: Integer;
  s: String;
  i: Integer;
begin
  PeerIP := ABinding.PeerIP;
  PeerPort:= ABinding.PeerPort;
  count := AData.Size;
  if count > Length(Buffer) then begin
    exit;
  end;
  AData.Read(Buffer,count);
  if (Buffer[0] <> $00) and  (Buffer[0] <> $01) then begin  // not search
    Edit1.Text:= PeerIP;
  end;
  case Buffer[0] of
   $00: begin   // search request
    case count of
     4: begin
      case Buffer[1] of
       0: begin
        Buffer[0] := $01;
         UDPSearchForm.Host := PeerIP;
         UDPSearchForm.DoSend(Buffer,4,Length(Buffer));
        Memo1.Lines.Add('Inquiry [' + UDPSearchForm.WSGetHostByAddr(PeerIP) + '(' +     PeerIP + ')' +          ' Port: ' + IntToStr(PeerPort) +
      ']');
   end;
  end;
 end;
end;
   end;
   $01: begin // Search Reply
    case count of
     4: begin
      case Buffer[1] of 0:
        begin
        ss := UDPSearchForm.WSGetHostByAddr(PeerIP);
    s := '[' + ss + '(' + PeerIP + ')' +
      ' Client Port: ' + IntToStr(PeerPort) +
      ']';
    Memo1.Lines.Add('Inquiry Reply ' + s);
    if PeerIp = UDPSearchForm.LocalAddress then begin
      ss := '<myself>' + ss;
    end;
    UDPSearchForm.Add(PeerIP,ss);
    end;
   end;
  end;
 end;
end;
    $10: begin // Text
     case Buffer[1] of
      0: begin
       s := '';
           for i := 4 to count-1 do begin
             s := s + char(Buffer[i]);
           end;
       Memo1.Lines.Add(ss+'  says: ' + s);
      end;
     end;
    end;
  end;
end;

procedure TForm1.UDPException(Sender: TObject);
begin
  //nothing
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  x: Array[0..100] of Byte;
  i: Integer;
begin
  UDPSearchForm.Host := Edit1.Text;
  UDPSearchForm.Active := true;
  x[0] := $10; // Text
  x[1] := 0;   // Type 0
  for i := 1 to Length(Edit2.Text) do begin
    x[i+3] := Byte(Edit2.Text[i]);
  end;
  UDPSearchForm.DoSend(x,4+Length(Edit2.Text),length(x));

   Memo1.Text:=Memo1.Text+Edit2.Text+#13#10;
end;

procedure TForm1.Edit2KeyPress(Sender: TObject; var Key: Char);
var
  x: Array[0..100] of Byte;
  i: Integer;
begin
if Ord(Key) = VK_RETURN then
  UDPSearchForm.Host := Edit1.Text;
  UDPSearchForm.Active := true;
  x[0] := $10; // Text
  x[1] := 0;   // Type 0
  for i := 1 to Length(Edit2.Text) do begin
    x[i+3] := Byte(Edit2.Text[i]);
  end;
  UDPSearchForm.DoSend(x,length(x));

   Memo1.Text:=Memo1.Text+Edit2.Text+#13#10;


end;

end.

解决方法

将“发送”按钮的 Default属性设置为“True”.当用户按Enter键时,它的OnClick事件将自动触发.
原文链接:https://www.f2er.com/delphi/102319.html

猜你在找的Delphi相关文章