Solved

Remove alert beep in treeview on KeyDown

Posted on 2004-04-19
6
1,550 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
indy Tmemory stream non latin charcter issue 2 121
find a node in VST 2 70
Firemonkey webbrowser scrollbars ? 1 49
How to build JSON File in Delphi 6 3 45
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

809 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