Empty TMemo with Ctrl + Enter

What I'm trying to accomplish:

  • User enters text in the TMemo field
  • If they press Enter, it creates a new line
  • If they press Ctrl + Enter , it moves the text to another field and empties TMemo

I am using this code [KeyPreview is True]:

procedure TFMsg.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if (Shift = [ssCtrl]) and (Key = $0D) then begin Key := 0; btnSendClick(Sender); //this moves the text and empties the TMemo box end; end; 

What is actually going on:

  • Ctrl + Enter sends text to another block
  • TMemo is blank, but seems to accept an Enter key when the cursor sits on the second line

Any help gratefully received. Thanks!

+6
source share
3 answers

The best way to handle this is as follows:

  • Create an action list or action manager or reuse an existing one.
  • Add an action that clears the note and moves on to the next. You will need to verify that the active control is indeed a note.
  • Give the action the shortcut you want, CTRL + ENTER .

Note that you do not need to apply the action to anything. This simple presence is enough to ensure that the label is processed.

For complex keyboard actions using modifier keys, it is always easier to use the action shortcut and, therefore, maintain the arm length from the lower level keyboard processing code.

Your action handler might look like this:

 if ActiveControl is TMemo then begin Memo := TMemo(ActiveControl); Text := Memo.Text; Memo.Clear; SelectNext(Memo, True, True); if ActiveControl is TMemo then begin Memo := TMemo(ActiveControl); Memo.Text := Text; end; end; 

In this code, I assume that there are several notes, and the text moves from one note to the next in tab order. But your needs can be very different. In this case, I'm sure it will be obvious what you need to do for your scenario.

+7
source

Use the Memo OnKeyPress event to do this:

 procedure TFMsg.Memo1KeyPress(Sender: TObject; var Key: Char); begin if (key=#10) and (GetKeyState(VK_CONTROL)<0) then begin key:=#0; btnSendClick(Sender); end; end; 

Please note that you need to check the feed line (# 10), not the carriage return (No. 13).

0
source
 property WantReturns: Boolean; 
-1
source

Source: https://habr.com/ru/post/950168/


All Articles