In my application, when I press CTRL + S
, my form (with Key Preview enabled) captures this and saves the document. But when the focus is in, for example, an edit control, I get an annoying "Ding" sound, or in general, windows sounds. How do I avoid this sound?
Here's my form's capture of this key event...
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
C: String;
begin
if not fChanging then
Modified;
if ssCtrl in Shift then begin
C:= LowerCase(Char(Key));
if C = 's' then begin
DoSave;
Key:= 0; //Tried this but didn't work
end else
if C = 'c' then begin
//Copy selected item(s)
end;
end;
end;
PS - Is there a more standard way of capturing these events? Because I'm sure I'm doing something wrong, and I'm sure there's another way I should be getting these key events without sounds.
A couple of things:
FormKeyPress
instead of FormKeyDown
. This will make the Key := 0;
code work... You will need to handle the CTRL checking manually though, by using something like GetKeyState()
(I originally had GetAsyncKeyState()
here, but as Rob Kennedy points out, GetKeyState() is a much better option)
. TActionList
on your form, double click on it, add an action and set it's hot key to CTRL-S. Add your save code to it's OnExecute
event handler. This is the "proper" way to do it I believe.Hope this helps.