zuluman
zuluman

Reputation: 49

Delphi remap key to multi characters

I want that when I press a M or m character, 000000 gets input on a specific TEdit box:

procedure Tfrm.FormKeyPress(Sender: TObject; var Key: Char) ;
var
  i : integer;
begin
  if Key in ['m'] + ['M'] then Key := '0';
end;

With this code, I can just remap 'M' key to single character. How can I remap 'M' to multiple characters for a TEdit box?

Upvotes: 1

Views: 371

Answers (3)

moskito-x
moskito-x

Reputation: 11968

If you wish to equip all TEdits with this behavior, then set all other TEdits KeyPress events to Edit1KeyPress

procedure Tfrm.Edit1KeyPress(Sender: TObject; var Key: Char);
var
xEdit: TEdit;
begin
if Key in ['m','M'] then begin
       Key := #0;
       xEdit := Sender as TEdit;
       xEdit.Text := xEdit.Text +'000000';
    end;
end;

Or short version

procedure Tfrm.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if Key in ['m','M'] then begin
   Key := #0;
   TEdit(Sender).Text := TEdit(Sender).Text+'000000';
end;
end;

Upvotes: 1

Remy Lebeau
Remy Lebeau

Reputation: 598194

Use the OnKeyPress event of the TEdit itself, not the OnKeyPress event of the parent TForm. Set the Key parameter to #0 to swallow it, and then insert 6 individual '0' characters into the TEdit:

procedure Tfrm.Edit1KeyPress(Sender: TObject; var Key: Char);
var
  i : integer;
begin
  if Key in ['m', 'M'] then
  begin
    Key := #0;
    for I := 1 to 6 do
      Edit1.Perform(WM_CHAR, Ord('0'), $80000001);
  end;
end;

Alternatively:

procedure Tfrm.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if Key in ['m', 'M'] then
  begin
    Key := #0;
    Edit1.SelText := '000000';
  end;
end;

Upvotes: 4

Arioch 'The
Arioch 'The

Reputation: 16065

you can not "remap" indeed

but you can kill that specific char (setting it to #0) and inject the needed zeroes using standard Windows Messaging API (SendMessage, not PostMessage)

Something along those lines:

procedure Tfrm.FormKeyPress(Sender: TObject; var Key: Char);
var i : integer;
begin
    if ActiveControl = Edit1 then
      if Key in ['m'] + ['M'] then begin 
         Key := #0; // zero-out the real key so it would not be handled by the editbox
         for i := 1 to 6 do
             ActiveControl.Perform( WM_CHAR, Ord( '0' ), $80000001);
             // or you may reference the specific editbox directly
             //  like Edit1.Perform(....);
      end;
end;

This would also require setting the form to intercept the keys of its controls.

http://docwiki.embarcadero.com/Libraries/XE2/en/Vcl.Forms.TCustomForm.KeyPreview

This makes sense if you want to hijack several editboxes at once. If not you better you the events of the editbox itself, not ones of the form.

procedure Tfrm.Edit1KeyPress(Sender: TObject; var Key: Char);
var i : integer;
begin
   if Key in ['m'] + ['M'] then begin 
      Key := ^@; // zero-out the real key so it would not be handled by the editbox
      for i := 1 to 6 do
          Edit1.Perform( WM_CHAR, Ord( '0' ), $80000001);
          // or you may reference the specific editbox directly
          //  like Edit1.Perform(....);
   end;
end;

Upvotes: 2

Related Questions