OnKeyDown
is designed to deal with virtual key codes (those that are referred to using the VK_
constants), not individual letter and number keystrokes.
Use OnKeyPress
to process individual characters, not OnKeyDown
.
procedure TFRMStuTakeTest.DBEDTWord01KeyPress(Sender: TObject; var Key: Char);
begin
if not CharInSet(Key, ['A'..'Z','a'..'z', #8]) then
Key := #0;
end;
Better still, use the EditMask
on the underlying TField
, and set a valid mask for alpha characters using something like 'LLLLL;0;_'
, which would require 5 letters between '['A'..'Z','a'..'z']', and will handle all of the validations, editing keystrokes, and so forth.
YourTable.FieldByName('Word1').EditMask := 'LLLLL;0;_';
See the documentation for TField.EditMask for more information, and follow the link at the bottom to TEditMask for details about the mask characters. (There's an editor for the EditMask
property in the Object Inspector; they're the same as those used by TMaskEdit
, so you can drop one of those on your form and click the ...
button on the right side of the EditMask
property to access it.)