How to avoid Treeview beeps when handling special keys

Giganews Newsgroups
Subject:How to avoid Treeview beeps when handling special keys
Posted by: Gerrit Beuze (gerrit[at][-nospam]modelmakertools[dot]com)
Date:Fri, 25 Jul 2008

Hi all,

I'm (optionally) handling a few Ctrl+Key combinations in a TTreeview descendant
like Ctrl+A = select all.
Implemented basically like:

procedure TTreeViewEx.KeyDown(var Key: Word; Shift: TShiftState);
begin
  if (Shift = [ssCtrl]) and (Key = Ord('A') then
  begin
    Key := 0;
    SelectAll;
  end;
  inherited KeyDown(Key, Shift);
end;

procedure TTreeViewEx.KeyPress(var Key: Char);
begin
  // Avoid beep
  if (Key = 'A') then
      // This breakpoint never gets hit
      if (KeyboardStateToShiftState = [ssCtrl]) then
        Key := #0;
  inherited KeyPress(Key);
end;

This works fine, however, my normal technique to suppress the beep for unhandled keys
using KeyPress to clear the key does not work: the treeview beeps anyway.
In fact: setting a breakpoint after the if Key = 'A' line never hits.

I tried several other methods: override WMChar, WMKeyPress. Nothing works.

Any suggestions?

Thanks in advance,

Gerrit Beuze
ModelMaker Tools

Replies