Search code examples
delphidelphi-11-alexandria

Differentiate between ENTER and RETURN in Delphi


This question tells how to do so in JavaScript, but how does one do it in a Delphi program?


Solution

  • Windows actually gives you this information in the WM_KEYDOWN message.

    To try this, create a new VCL application and add a WM_KEYDOWN message procedure to the main form:

    procedure WMKeyDown(var Message: TWMKeyDown); message WM_KEYDOWN;
    

    implemented as

    procedure TForm1.WMKeyDown(var Message: TWMKeyDown);
    begin
      case Message.CharCode of
        VK_RETURN:
          begin
            if Message.KeyData shr 16 and KF_EXTENDED <> 0 then
              ShowMessage('Enter (numeric keypad) down')
            else
              ShowMessage('Return down')
          end;
      end;
    end;
    

    Indeed, the documentation for WM_KEYDOWN links to Keyboard Input Overview and then the Extended-Key Flag section:

    The extended-key flag indicates whether the keystroke message originated from one of the additional keys on the Enhanced 101/102-key keyboard. The extended keys consist of [...] and ENTER keys in the numeric keypad.