2

Start a new VCL Application, drop a TEdit on the Form and assign this to OnKeyPress event:

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
 Caption:=Caption+' '+IntToStr(Ord(Key));
end;

When you run the application and press, for example, Ctrl+M or Ctrl+H you will receive control characters #13 (Enter) and #8 (Backspace) in Key OnKeyPress event. Is there a way to stop this annoying behavior ? I mean completely stop receiving OnKeyPress when I press shortcuts like Ctrl+M, not testing for Ctrl down and do nothing inside the event. I want my application to process Enter key when Enter key is pressed not Ctrl+M. I hope you understand what I'm saying...

Marus Gradinaru
  • 2,824
  • 1
  • 26
  • 55

1 Answers1

4

The OnKeyDown/Up events (WM_KEYDOWN/UP messages) receive individual keystrokes, reporting virtual key codes that have been translated from the keyboard's physical scan codes by the keyboard driver.

The OnKeyPress event (WM_CHAR message) receives translated characters after virtual keys and key combinations have been processed (if they translate into a character at all).

CTRL-M is translated into a carriage return (#13) character.

CTRL-H is translated into a backspace (#8) character.

This translation happens at the OS layer, when message loops pass keyboard messages to the TranslateMessage() function before calling DispatchMessage().

See the following table for how various CTRL combination are translated:

Control key - examples

Also see Character Messages for a more detailed explanation for how WM_KEYDOWN/UP messages get translated into WM_CHAR messages.

So, to differentiate between CTRL-M and ENTER in the OnKeyPress event, you have to either:

  1. use the OnKeyDown/Up events to keep track of when the CTRL (VK_CONTROL) and M (VK_M) virtual keys are being pressed versus when the ENTER (VK_RETURN) virtual key is being pressed.

  2. Use GeyKeyState() or GetAsyncKeyState() to detect if the CTRL-M keys are held down versus if the ENTER key is held down.

Otherwise, don't use OnKeyPress at all. Use OnKeyDown/Up instead and do your own keystroke handling as needed.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Better than the Wikipedia link might be [this](https://www-user.tu-chemnitz.de/~ygu/petzold/ch06d.htm). Only a few shortcuts is actually translated into `WM_CHAR` (they are listed there in the table). [will delete this comment soon] – TLama Jun 27 '15 at 15:42
  • That link has an untrusted security certificate. – Remy Lebeau Jun 27 '15 at 15:58
  • 1
    @RemyLebeau Certificate looks fine to me... I've seen some browsers flake out and reject perfectly good certificates from time to time, even from reputable and definitely well configured sites (apple.com, etc). Not sure the cause but it seems likely the case here. – J... Jun 27 '15 at 17:43
  • @J...: i am loading the page on a tablet. I have one browser that flat out refuses to load the page, not even an error message, just a timeout waiting for the page to load. And another browser that says the certificate is untrusted, I think it is signed by a chain of authorities that are not known by my tablet (I don't recognize them by name, either). – Remy Lebeau Jun 27 '15 at 22:51
  • I've had no problems with it. Here is [their CA](https://www.tu-chemnitz.de/urz/security/ca/ca-cert/). But anyway, what's behind is nothing more than a very old version of well known book Programming Windows by Charles Petzold. And just [this part](http://i.imgur.com/xuVoffM.png) might have been interesting for this thread (still valid after so many years). – TLama Jun 27 '15 at 23:28