Solved

Remove alert beep in treeview on KeyDown

Posted on 2004-04-19
6
1,557 Views
Last Modified: 2010-07-27
I have a form with just a treeview on it.  I am trying to add ctrl+a to select all nodes.  Each time I press ctrl+a, I get an alert beep.

I have looked through EE and found multiple references to setting Key := Ord(#0); but that doesn't help.

Below is the KeyDown code that I am using.

Thanks,

DD

procedure TForm1.TreeView1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (ssCTRL IN SHIFT) and ((Key = Ord('a')) or (Key = Ord('A')))then
  begin
    //  Do my selection stuff here.
    Key := Ord(#0);   //  EE line to remove beep - doesn't work
    Shift := Shift - [ssCTRL];   //  I tried this to see if it would work too.  Nope.
  end;
end;
0
Comment
Question by:devilduck
6 Comments
 
LVL 26

Accepted Solution

by:
Russell Libby earned 250 total points
ID: 10863970
Its actually the KeyPress event that is causing the beep, so all you need to do is zero the (#0) char value for key in that event. You can also check for the control state, and conditionally clear it if you wanted to as well (code below)

Regards,
Russell



procedure TForm1.TreeView1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin

  if (ssCtrl in Shift) and (Key in [65, 97]) then
     ShowMessage('Control+A');

end;

procedure TForm1.TreeView1KeyPress(Sender: TObject; var Key: Char);
var  intKey:        SHORT;
begin

  if (HiByte(GetAsyncKeyState(VK_CONTROL)) <> 0) then Key:=#0;

end;
0
 
LVL 6

Expert Comment

by:DaFox
ID: 10864061
Hi devilduck,

try

var
  Mgs: TMsg;
begin
  {...}
  PeekMessage(Mgs, 0, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE);
  {...}
end;

instead "Key := 0;"

Regards,
Markus
0
 

Author Comment

by:devilduck
ID: 10864596
Wow, Russell you are fast.  I am doing all of my keydown checks in the treeview keydown event.  I just created a keypress event and added Key:=#0;

procedure TmainForm.TreeView1KeyPress(Sender: TObject; var Key: Char);
begin
  Key:=#0;
end;

1. Will this cause me interaction problems with keydown?
2. Is the call to GetAsyncKeyState necessary considering I am doing everything on keydown?
3. I didn't have a keypress event defined for my treeview (or anywhere else) I am going to assume that it is a windows event that occurs whether I define it in delphi or not.

Thanks,

DD
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Expert Comment

by:Russell Libby
ID: 10864656
DD,

1.) No. It won't cause problems, as the keydown happens before the WM_CHAR (KeyPress event)
2.) GetAsyncKeyState was just to show you how to get the control state in the KeyPress (vs KeyDown/KeyUp). If your not doing anything with keypress in the tree, then "Key:=#0" is fine 'as is'.

Russell
0
 

Author Comment

by:devilduck
ID: 10869326
Cool.  Thanks again.

devilduck
0
 

Expert Comment

by:BSCPaul
ID: 34295785
Another option is to check for the key in the FormKeyPress evert and then clear it (KeyPreview must be true):

procedure Tfrorm.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if key=#27
  then begin
    key:=#0;
    close;
  end;
end;
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi 2 77
control image tags in a string ? 12 153
Problem working with dynamic array - help 2 35
Delphi Firemonkey : user drawing in window 3 17
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question