3

I was trying to detect Ctrl+V from a TEdit's OnKeyPress event and I've noticed that the Key parameter assumes an unusual value when pressing Ctrl+AnyKey.

Example:

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  ShowMessage(IntToStr(Ord(Key)));
end;

It produces the following results:

  • Ctrl+Q 17
  • Ctrl+W 23
  • Ctrl+E 5
  • Ctrl+R 18
  • ...
  • Ctrl+A 1
  • Ctrl+Z 26
  • Ctrl+C 3
  • Ctrl+V 22
  • Ctrl+X 24

I don't understand how keys are translated, what does these codes mean?

It seems it has nothing to do with the ASCII table:

enter image description here

Could anyone shed some light on this?

Fabrizio
  • 7,603
  • 6
  • 44
  • 104
  • 1
    This sounds like the wrong solution. Shouldn't you listen for WM_PASTE. Then program will work with users that paste user other methods. Like the context menu. Or Ctrl+Insert. – David Heffernan Mar 15 '18 at 13:15
  • 1
    And if you ever did need to catch Ctrl+key presses then OnKeyPress is the wrong way to do it. So it looks very much like this question has been asked with two incorrect premises. – David Heffernan Mar 15 '18 at 13:22
  • 3
    You are getting the [ascii control character](http://jkorpela.fi/chars/c0.html) values. – LU RD Mar 15 '18 at 13:22
  • @DavidHeffernan: My initial goal was that to set `Key` parameter to `#0` if `Clipboard.AsText` doesn't contain only number characters but maybe I'll ask another question about that. I've created this question because I'm curious about the way keys are translated into chars for the `OnKeyPress` event – Fabrizio Mar 15 '18 at 13:23
  • This could be helpful: http://edn.embarcadero.com/article/38447 – nil Mar 15 '18 at 13:25
  • This is of academic interest then, and @LURD tells you what is going on. But it won't be of any use solving your problem as I outlined. I don't think you need to ask a new for that though. I think I already told you what to do. – David Heffernan Mar 15 '18 at 13:41
  • 1
    You can study [this](https://stackoverflow.com/a/10158921/576719) example, how to intercept the `WM_PASTE` message in a control. – LU RD Mar 15 '18 at 14:07
  • @LURD: Thanks, your comments have been very helpful – Fabrizio Mar 15 '18 at 14:10
  • @LURD: Your first comment answers to my question, if you post it with a little explanation I'll be glad to accept it – Fabrizio Mar 15 '18 at 14:19

1 Answers1

6

I don't understand how keys are translated, what does these codes mean?

The values you get with the Ctrl+AnyKey combinations are Ascii control codes. They emanate from the need to enter non-printing (control) characters from the keyboard. The (typical) values for the control characters are below 32 plus the del character (127).

enter image description here

Some more information about the history behind and standards could be found here and here.

LU RD
  • 34,438
  • 5
  • 88
  • 296