Solved

Remove alert beep in treeview on KeyDown

Posted on 2004-04-19
6
1,547 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

773 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