Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to eat keydown message to prevent beep?

541 views
Skip to first unread message

Mike Hubbard

unread,
Feb 8, 2005, 2:46:37 PM2/8/05
to
I need the OnKeyDown event handler of a form to be executed, but how
do I eat the message to prevent the beep? I can use the OnShortCut
event handler and set handled to true, but I don't know how to test
for Ctrl-F in that event handler since there is no ShiftState
parameter.

Thanks

Mike

Liz

unread,
Feb 8, 2005, 3:11:18 PM2/8/05
to
Mike Hubbard wrote:

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);

??

Mine has shift

So,

if (ssctrl in shift) and (chr(key)='F') then showmessage('Ctrl+F
detected');

same with keyup

set the key to 0 to cancel its action.

Mike Hubbard

unread,
Feb 8, 2005, 3:53:34 PM2/8/05
to

>procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
> Shift: TShiftState);
>
>??
>
>Mine has shift
>

Yeah, my keydown declaration is the same, and I know how to test the
shiftstate, but how do I prevent the beep? That was the question.


>
>set the key to 0 to cancel its action.

I tried this and the beep still occurs.

Mike

Liz

unread,
Feb 8, 2005, 4:17:29 PM2/8/05
to
Mike Hubbard wrote:

> I tried this and the beep still occurs.

Well ctrl+F doesnt cause a beep by default, so, what exactly is causing
the beep? normally the beep is pressing enter..

Iain Macmillan

unread,
Feb 9, 2005, 11:33:54 AM2/9/05
to

In article <42092c69$1...@newsgroups.borland.com>, "Liz"
<liz_want...@xcalibur.nospam.co.uk> wrote:

>> I tried this and the beep still occurs.
>
> Well ctrl+F doesnt cause a beep by default, so, what exactly is causing
> the beep?

eating the keydown. <g>

He'll have to eat ctrl-F in keyup too.

if you were told someone had come back in when as far as you knew they
hadn't gone out, wouldn't you beep too? %).

Mike Hubbard

unread,
Feb 9, 2005, 1:00:44 PM2/9/05
to
On 8 Feb 2005 13:17:29 -0800, "Liz"
<liz_want...@xcalibur.nospam.co.uk> wrote:


>Well ctrl+F doesnt cause a beep by default, so, what exactly is causing
>the beep? normally the beep is pressing enter..

It looks to me like the Ctrl-F is causing the beep. It is a WM_CHAR
message that gets dispatched and then some assembly code results in
the beep. What else do I need to look at?

Mike

Mike Hubbard

unread,
Feb 9, 2005, 1:01:45 PM2/9/05
to
>> Well ctrl+F doesnt cause a beep by default, so, what exactly is causing
>> the beep?
>eating the keydown. <g>
>
>He'll have to eat ctrl-F in keyup too.
>
It beeps even if I don't do anything to eat the keydown.

Mike

Mike Williams (TeamB)

unread,
Feb 9, 2005, 1:29:36 PM2/9/05
to
Mike Hubbard wrote:

> In searching more, I found that if I enter any Ctrl + SomeChar in any
> control that has focus such as an edit box in a modal form, I get a
> beep. What is causing this?

That's just standard windows behavior. It's letting the user know that
they pressed a key that didn't actually do anything useful. You'll
notice this if you simply create a new project in Delphi and drop a
tEdit on it. Ctrl+F will cause a beep. Ctrl+A will cause a beep.
Ctrl+C, Ctrl+X, Ctrl+V, etc. will not cause a beep since those
keystrokes are consumed at a lower level for clipboard operations.

--
-Mike (TeamB)

Mike Hubbard

unread,
Feb 9, 2005, 1:20:11 PM2/9/05
to

>Well ctrl+F doesnt cause a beep by default, so, what exactly is causing
>the beep? normally the beep is pressing enter..

In searching more, I found that if I enter any Ctrl + SomeChar in any


control that has focus such as an edit box in a modal form, I get a
beep. What is causing this?

Mike

Iain Macmillan

unread,
Feb 9, 2005, 2:20:16 PM2/9/05
to

In article <r0lk0190poj5ceigr...@4ax.com>, Mike Hubbard
<mmhu...@socal.rr.com> wrote:


>>Well ctrl+F doesnt cause a beep by default,

Hmm, take that back Liz!
I agree with the next para, at least if somechar is 'F'. (or ABDEGI...)

> In searching more, I found that if I enter any Ctrl + SomeChar in any
> control that has focus such as an edit box in a modal form, I get a
> beep.

So they do.
Most of my edits have a restriction of some kind in OnKeyPress, which says
what chars I will allow (e.g. numbers) so they don't beep as ctrl-F is not a
number.. so clearly eating it here works.

Despite what it says in help on OnKeyPress about handling extended chars in
OnKeyDown I'd expect therefore that an OnKey*Press* handler reading
if key=chr(6) then key:=chr(0);
would suppress Ctrl-F.

> What is causing this?
.. what Mike W. says. %).

Mike Hubbard

unread,
Feb 9, 2005, 2:35:23 PM2/9/05
to
On 9 Feb 2005 10:29:36 -0800, "Mike Williams (TeamB)"
<mlwil...@gmail.com> wrote:


>That's just standard windows behavior. It's letting the user know that
>they pressed a key that didn't actually do anything useful. You'll
>notice this if you simply create a new project in Delphi and drop a
>tEdit on it. Ctrl+F will cause a beep. Ctrl+A will cause a beep.
>Ctrl+C, Ctrl+X, Ctrl+V, etc. will not cause a beep since those
>keystrokes are consumed at a lower level for clipboard operations.

That makes sense to me, but when I trap for the Ctrl-F to execute a
buttons event handler, how can I prevent the beep from occurring? The
KeyDown event handler code is as follows:

// control-f is shortcut for find button
if (btnFind.Enabled and (ssCtrl in Shift) and
((Key = Ord('f')) or (Key = Ord('F')))) then
btnFindClick(Sender);

This code works fine, but the beep is confusing to the user.

Thanks for all the help.

Mike

Mike Williams (TeamB)

unread,
Feb 9, 2005, 3:12:17 PM2/9/05
to
Mike Hubbard wrote:

> That makes sense to me, but when I trap for the Ctrl-F to execute a
> buttons event handler, how can I prevent the beep from occurring? The
> KeyDown event handler code is as follows:
>
> // control-f is shortcut for find button
> if (btnFind.Enabled and (ssCtrl in Shift) and
> ((Key = Ord('f')) or (Key = Ord('F')))) then
> btnFindClick(Sender);
>
> This code works fine, but the beep is confusing to the user.
>
> Thanks for all the help.

I think you're making it too hard on yourself. How is the user
supposed to know to press Ctrl+F anyhow? The normal way to do this is
to use a menu (either a main menu or popup menu) with a Ctrl+F
shortcut. When you do that the sound goes away anyhow and you don't
have to write code like the above ...

--
-Mike (TeamB)

Mike Hubbard

unread,
Feb 9, 2005, 3:52:13 PM2/9/05
to

>I think you're making it too hard on yourself. How is the user
>supposed to know to press Ctrl+F anyhow? The normal way to do this is
>to use a menu (either a main menu or popup menu) with a Ctrl+F
>shortcut. When you do that the sound goes away anyhow and you don't
>have to write code like the above ...

The form is a modal form, and the reason for this is that it what the
user requested. So how can I prevent the beep?

Thanks

Mike

Patrick

unread,
Feb 9, 2005, 4:05:04 PM2/9/05
to
> The form is a modal form, and the reason for this is that it what the
> user requested. So how can I prevent the beep?
On XP, you could get out the sledgehammer and stop the beep service (sc stop
beep) and start it again (sc start beep).
Ick.


Patrick

unread,
Feb 9, 2005, 4:15:49 PM2/9/05
to
Looks like you can do this programatically via the WinSvc unit. I should
have confirmed this prior to posting my previous statement.


Mike Williams (TeamB)

unread,
Feb 9, 2005, 4:50:20 PM2/9/05
to
Mike Hubbard wrote:

> The form is a modal form, and the reason for this is that it what the
> user requested. So how can I prevent the beep?

Like I said, add popup menus with the appropriate shortcuts. It's
*supposed* to beep for any other key.

--
-Mike (TeamB)

Mike Hubbard

unread,
Feb 9, 2005, 5:02:43 PM2/9/05
to
>Like I said, add popup menus with the appropriate shortcuts. It's
>*supposed* to beep for any other key.

Thanks! I get it now. I hadn't thought of using a popup menu for
the form with AutoPopup set to False. That is the right way to do it.

Mike

0 new messages