Solved

Remove alert beep in treeview on KeyDown

Posted on 2004-04-19
6
1,537 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 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

14 Experts available now in Live!

Get 1:1 Help Now