I have an interesting problem that is asked often and yet, I have found that the solutions they give don't work...
I'm using Delphi XE3 on Win 7.
I had always understood that in FormKeyDown or FormKeyPress, you can set the Key to 0 or #0 to indicate that the key has been handled (and so it should not make the "ding" sound for invalid key presses).
I did the following:
- new VCL application
- Form KeyPreview:=true
- add some event handlers (see below)
- run
- Alt+T (ding sound occurs)
I successfully detect Alt+T and set Key to 0. Form OnKeyPress and Form OnKeyUp do not fire. this is not surprising to me since I had set the Key to 0.
the surprise is that it still makes the "ding" sound after OnKeyDown completes.
how can I stop it from doing that?
In some debugging, it would seem as though a message WM_SYSKEYDOWN is causing this to occur.
thanks!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TForm1 = class(TForm)
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure FormKeyPress(Sender: TObject; var Key: Char);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if (key=ord('T')) and (shift=[ssAlt]) then
key:=0;
end;
procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
showmessage('keypress');
end;
procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
showmessage('keyup');
end;
end.
object Form1: TForm1
Left = 267
Top = 163
Caption = 'Form1'
ClientHeight = 565
ClientWidth = 654
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
KeyPreview = True
OldCreateOrder = False
Position = poScreenCenter
ShowHint = True
OnKeyDown = FormKeyDown
OnKeyPress = FormKeyPress
OnKeyUp = FormKeyUp
PixelsPerInch = 96
TextHeight = 13
end
WM_SYKEY...
messages?WM_KEY...
andWM_SYSKEY...
are separate sets of messages. The event handlers you are using are triggered forWM_KEY...
messages, notWM_SYSKEY...
messages. – Remy Lebeau