Board index » delphi » Keypreview problem ??

Keypreview problem ??

I use the the keypreview := true on Form1 and Form2 where you can use the
f10 button to activate a procedure. Now I use other function keys with no
problem at all but when I use the F10 key to make something work I have the
following problem the next key pressed is never recorded ??? but if I use f9
it work everytime. Because my program is quite complex I made a sample
program to test if there was an issue and the same thing ??? The only thing
I did to either form is add a label and
to form1 (unit1) I added a button that made Form2 visible.

Have a Look at my code and see if you work it out.. I think there must be
some special consideration when using the F10 key or something ??? I hope
someone knows ??

Thanks

Aaron Miles

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  count : Integer;
implementation

uses Unit2;

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Count := 0;
end;

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if key = vk_f10
    then begin
     LAbel1.Caption := IntToStr(Count);
     Inc(Count)
         end;
  if key = vk_f9
    then begin
     LAbel1.Caption := IntToStr(Count);
     Inc(Count)
         end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Form1.Enabled := False;
  Form2.Show;
end;

end.

unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Unit1;

type
  TForm2 = class(TForm)
    Label1: TLabel;
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.DFM}

procedure TForm2.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if key = vk_f10
    then begin
     LAbel1.Caption := IntToStr(Unit1.Count);
     Inc(Count);
         end;
  if key = vk_f9
    then begin
     LAbel1.Caption := IntToStr(Count);
     Inc(Count)
         end;
  if key = vk_f8
    then begin
     LAbel1.Caption := IntToStr(Count);
     Inc(Count)
         end;
end;

procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Form1.Enabled := True;
  Form2.Hide;
end;

end.

 

Re:Keypreview problem ??


Quote
QBD The Bookshop wrote:

> procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
>   Shift: TShiftState);
> begin
>   if key = vk_f10
>     then begin
>      LAbel1.Caption := IntToStr(Count);
>      Inc(Count)
>          end;
>   if key = vk_f9
>     then begin
>      LAbel1.Caption := IntToStr(Count);
>      Inc(Count)
>          end;
> end;

How about just eating the F10 away, and after that make your code
to do what ever you wish to happen.

   if key = vk_f10
     then begin
      Key := 0;
      LAbel1.Caption := IntToStr(Count);
      Inc(Count)
          end;

Re:Keypreview problem ??


On Wed, 29 Nov 2000 14:47:40 +0200, Markku Nevalainen <m...@iki.fi>
wrote:
Quote

>How about just eating the F10 away, and after that make your code
>to do what ever you wish to happen.

>   if key = vk_f10
>     then begin
>      Key := 0;
>      LAbel1.Caption := IntToStr(Count);
>      Inc(Count)
>          end;

Markku,

Your solution indeed works. For the curious, the reason for the
strange behaviour reported by Aaron's post is that F10 is the Windows
shortcut to activate the current window's menu bar.

You can verify this by reading the OLH, or reproducing the so called
problem, and hitting in secuence F10 then Enter.

Or just launch a standard program such as MS Word and do the same.

Therefore my advice to Aaron Miles would be to use another function
key, as F10 is already used by Windows, or if not possible be aware
that his program will not have a standard behaviour in this respect.

Pierre Gemis

Re:Keypreview problem ??


Quote
Pierre Gemis wrote:

>  as F10 is already used by Windows, or if not possible be aware
> that his program will not have a standard behaviour in this respect.

Sometimes, to get your app usable and working, you just can't get also
the 'standard Windows GUI behaviour' in.

For instance some Point Of Sale (POS) app, the cashier does not
want to use slow mouse to click in the actions. He is more happy when
he/she can quickly do most of the actions with keyboard.

It's a big help if you can give him all the Function -keys, also the
F10, and hide some useful action behind it too.

Markku Nevalainen

Re:Keypreview problem ??


On Mon, 04 Dec 2000 00:11:10 +0200, Markku Nevalainen <m...@iki.fi>
wrote:
Quote

>Sometimes, to get your app usable and working, you just can't get also
>the 'standard Windows GUI behaviour' in.

>For instance some Point Of Sale (POS) app, the cashier does not
>want to use slow mouse to click in the actions. He is more happy when
>he/she can quickly do most of the actions with keyboard.

>It's a big help if you can give him all the Function -keys, also the
>F10, and hide some useful action behind it too.

>Markku Nevalainen

I fully agree. I was just trying to say that, if you for some good
reason decide to make an application not following standard (without
quotes) Windows GUI's behaviour, better you are aware of the fact and
mention it somewhere in the documentation.

Obviously Aaron wasn't aware, and now that he is, depending on his
application's requirement, he may decide to stick with F10 using you
excellent tip, or to use another function key.

Pierre Gemis

Other Threads