Solved

Remove alert beep in treeview on KeyDown

Posted on 2004-04-19
6
1,531 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now