Inno Setup restrict some special characters on key press

Viewed 305

I am trying to restrict key press of some special characters in the input box for my requirement and I am using the below procedure to do the same.

procedure RestrictKeyPress(Sender: TObject; var Key: Char);
var
  KeyCode: Integer;
begin
  { Restrict special characters @, ^, *, \ }
  KeyCode := Ord(Key);
  if ((KeyCode = 32) or (KeyCode >= 64) or (KeyCode <= 94) or (KeyCode <= 42) or (KeyCode <= 92)) then
    Key := #0;
end;

I am calling this procedure in the InitializeWizard like this

PageConfig.Edits[1].OnKeyPress := @RestrictKeyPress;

But when I test this, key press is not working for any keys. I am trying to restrict only the keys mentioned below and space.

@, ^, *, \

1 Answers

Your logic is entirely wrong. :-) Let's take a look:

if ((KeyCode = 32)          { Ok so far }
  or (KeyCode >= 64)        { Oops. Killing every key above 63 }
  or (KeyCode <= 94)        { And every key below 95 }
  or (KeyCode <= 42)        { And (redundantly) every key below 43 }
  or (KeyCode <= 92)) then  { And (redundantly) every key below 93 }

You also don't need to convert Key to a numeric.

Use a simple set instead:

if (Key in ['@', '^', '*', '\', #32]) then  { #32 is space }
  Key := #0;
Related