Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I detect the Enter key in an edit box?

I have a UDP messenger with a Send button, but it annoys me that I have to press the button instead of just hitting Enter. So I have created procedure TForm1.Edit2KeyPress. Now I have no idea how to define the Enter button in if (enter is pressed) then {code for sending message}.

After answering i have new problem. Anything I type is sended , letter by letter.. here is my code unit chat1;

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, 4+Length(Edit2.Text), length(x));

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


end;

end.
like image 922
user1601731 Avatar asked Aug 21 '12 15:08

user1601731


People also ask

How to detect if Enter key is pressed in a text input field?

Answer: Use the keypress event To check whether a user has pressed Enter key while on specific input, you can use the keypress event in combination with the enter key code 13 .

How can I tell if a key is entered pressed?

To check whether user pressed ENTER key on webpage or on any input element, you can bind keypress or keydown event to that element or document object itself. Then in bind() function check the keycode of pressed key whether it's value is 13 is not.

How do you check if Enter key is pressed in JavaScript?

Using JavaScript In plain JavaScript, you can use the EventTarget. addEventListener() method to listen for keyup event. When it occurs, check the keyCode 's value to see if an Enter key is pressed.

How do you press Enter in JavaScript?

To trigger a click button on ENTER key, We can use any of the keyup(), keydown() and keypress() events of jQuery. keyup(): This event occurs when a keyboard key is released. The method either triggers the keyup event, or to run a function when a keyup event occurs.


1 Answers

Set the Default property of the Send button to True. Its OnClick event will fire automatically when the user presses Enter.

like image 107
mj2008 Avatar answered Oct 25 '22 08:10

mj2008