0

so I've made a simple little calculator and I want to add a little more functionality to it by allowing the user to use the Numpad on their keyboard instead of using a mouse to interact with the GUI.

How can I receive input from the keyboard?

Thanks in advance!
PrimeBeat

PrimeBeat
  • 373
  • 2
  • 10
  • 3
    One way would be to set the Form's `KeyPreview` property to true, and then use the Form's `OnKey(Down|Up|Press)` events to look for keypad key strokes as needed. – Remy Lebeau Jan 04 '21 at 17:24
  • 1
    What's wrong with the normal ways of capturing keyboard input? Or is your question more properly `How can I receive input from the keyboard?`? – J... Jan 04 '21 at 18:28
  • 1
    Related : [How can my form detect KeyDown events when another control has the focus?](https://stackoverflow.com/q/26761084/327083) – J... Jan 04 '21 at 18:36

1 Answers1

-3

I have done that. I mean a simple calculator application.

To have keyboard user input, I put a TEdit on the form. This is the only component on the form getting keyboard input. Then at runtime I move the edit out of view:

procedure TCalculatorForm.FormResize(Sender: TObject);
begin
    EntryEdit.Left := ClientWidth + 10;
end;

This makes the TEdit invisible but it stay active. When the user type on the keyboard, this TEdit receive the characters. I use the OnKeyPress event to get hand on it and act in the calculation like the user had clicked in the buttons.

fpiette
  • 9,052
  • 1
  • 19
  • 32
  • 3
    If this is the only windowed control on the form, then surely you can just use the form's `OnKey*` events instead, even without setting `KeyPreview` to `True`? – Andreas Rejbrand Jan 04 '21 at 18:06