43

Let me explain by an example. In Delphi, you can write

procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = ^C then
    ShowMessage('The user wants to copy something.')
  else if Key = ^V then
    ShowMessage('The user wants to paste.')
end;

to check for Ctrl+C and Ctrl+V keyboard commands. In fact, the same syntax works for Ctrl+A, where A is any character, and -- of course -- you can also use a case statement instead of ifs. You can even do ShowMessage(^A), so, apparently, ^A is considered a char.

However, when browsing the official Delphi documentation, I cannot find any reference to this syntax. But maybe the ^A syntax is so common that it is understood as a part of the underlying plain text file format? Or is it simply an undocumented feature of the Delphi programming language? (Notice that the above constructions are actually used in the RTL/VCL source code. But, of course, Embarcadero, and Embarcadero alone, is allowed to use undocumented features, if any such exists.)

Sathyajith Bhat
  • 21,321
  • 22
  • 95
  • 134
Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
  • 2
    anyone can use undocumented features – David Heffernan Feb 06 '11 at 20:51
  • @David: OK, I agree that the problem isn't nearly as bad as in the case of the Windows API, for instance. – Andreas Rejbrand Feb 06 '11 at 21:03
  • 6
    Some of them, error insight don't like, like `^[` (Esc), `^@` (NUL).. – Sertac Akyuz Feb 06 '11 at 21:43
  • 9
    Yet again I learn something new from SO (after 13 using Delphi!) – Gerry Coll Feb 06 '11 at 21:57
  • 11
    [Pascal ISO 7185:1990](http://www.moorecad.com/standardpascal/iso7185.pdf) has [ISO/IEC 646](http://en.wikipedia.org/wiki/ISO_646) as a normative reference (page 7), which originally defined [C0 control codes](http://en.wikipedia.org/wiki/C0_and_C1_control_codes). Maybe that's a way to say that it could be implied. – Sertac Akyuz Feb 06 '11 at 22:09
  • @Gerry, don't worry. You'r not alone. Funny thing is that I see them all over the place when using vim but had no idea it could be used in Delphi. – Lieven Keersmaekers Feb 07 '11 at 08:01
  • @Sertac: did you QC those issues? I think you should make that ISO thing an answer too; I'll definitely vote for that. – Jeroen Wiert Pluimers Feb 07 '11 at 10:24
  • @Jeroen - Nope, I don't QC for a few years now... As for answering the question, IMO yours is better since AFAIK Borland never claimed that their pascal adhered to any standard at all. – Sertac Akyuz Feb 07 '11 at 13:59

2 Answers2

33

This is from long ago as an escape character to enable you to have consts for control characters in a more readable way.

const
  CtrlC = ^C;
begin
  Write(Ord(CtrlC));
end.

This defines a Char constant with value #3, then writes 3 in Borland Pascal 7, and I remember seeing it years before that too.

I just checked the Turbo Pascal 5.0 and Borland Pascal 7.0 languages guides, but could not find it, so it seems undocumented.

Edit: I do remember this was a Borland thing, and just checked: it is not part of the ISO Pascal standard (formerly this was ANSI Pascal Standard, thanks Sertac for noticing this).

It is documented in the Free Pascal documentation.

SGI uses the backslash as escape character, as per their docs.

More Edit: I found it documented in Delphi in a Nutshell and the Delphi Basics site.

Found it: Just found it on page 37 of the Turbo Pascal 3 Reference Manual.

--jeroen

Jeroen Wiert Pluimers
  • 23,965
  • 9
  • 74
  • 154
  • 1
    There's no ANSI standard on the pascal language since 1993, when ANSI terminated it on behalf of the ISO standard. Regarding the ISO standard see my comment to Andreas' question. – Sertac Akyuz Feb 06 '11 at 22:56
  • Afaik when there were talks about simplifying the (Delphi) compiler a few years back, this feature, together with stuff like (. .) was used as examples – Marco van de Voort Feb 08 '11 at 13:02
  • 1
    Edited comment that provides FPC documentation link (IT) – Marco van de Voort Feb 08 '11 at 13:06
  • @MarcovandeVoort did those FPC pages change? As currently, they only document the `#` syntax to create control characters. – Jeroen Wiert Pluimers Dec 17 '17 at 12:46
  • 1
    Yes, In case topics move or get split, links can die. I had to go "up" and then to the topic/section before. Updated link. – Marco van de Voort Dec 17 '17 at 17:17
  • 1
    It's also mentioned on page 45 in [Turbo Pascal Version 2.0 Reference](http://bitsavers.informatik.uni-stuttgart.de/pdf/borland/turbo_pascal/Turbo_Pascal_Version_2.0_Reference_1984.pdf). – LU RD Dec 13 '21 at 23:28
3

This is a known undocumented feature. But then again, the latest official syntax documentation is from delphi 7.

Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
  • could you describe it. What is the type of ^A? – David Heffernan Feb 06 '11 at 21:35
  • Right, it's a Char and ord(^A)=1, ^G is the bell! – David Heffernan Feb 06 '11 at 22:07
  • 2
    @David, its type is "string literal," the same as if you'd used `#1`. The compiler will treat it as a char or string as context dictates. It's valid in all versions of Delphi, and many, if not all, versions of Turbo Pascal. – Rob Kennedy Feb 07 '11 at 00:42
  • @Rob Kennedy: +1, for from Turbo Pascal it has been inherited. I remember it very well being described in TP/BP's help system. – Andriy M Feb 07 '11 at 06:54
  • 1
    I used it in turbo pascal 5.5 some hundred years ago... it was useful to write(^G) at that times to notify the user about wrong things... you can still go to cmd.exe and "execute" the ctrl+G. :p @David, you made me remember a lot of old things... hehe. :D. BTW, it must be AnsiChar not char... right? – jachguate Feb 09 '11 at 00:37