Recent

Author Topic: Keyboard input tough problem here (actual code shown).  (Read 15113 times)

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: [SOLVED] Keyboard input tough problem here.
« Reply #15 on: August 07, 2011, 05:47:08 pm »
No problem. That's why it helps to post actual code  :)
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Elmug

  • Hero Member
  • *****
  • Posts: 849
Keyboard input tough problem here (actual code shown)
« Reply #16 on: August 08, 2011, 02:23:07 am »
Thank you, again, BigChimp, User137, and others.

Speaking of showing the code, I am showing the actual total code, of a minimalistic project that couldn't be any smaller, and I just made, that demonstrates the behavior that I called "tough problem" and still is, to me, since that issue is still not solved, and I am still very much interested in clearing it up.

The minimalistic project consists only of Form1 with EDit1 (TEdit type box) and Button1 (TButton type).

Compiles readily, and text can be readily typed in Edit1.

Pushing Button1 clears the text typed just fine, as intended.

Now, by the attached code, when user types, if the letter 'q' is pressed, the same method that was created for Button1 is fired.

Now, one would expect the Edit1 box to be clear of text, since that's what Button1 does, and we are calling its method.

But no, the text DOES CLEAR, but the character 'q' ends up showing in the TEdit box!!!!

Now, below is the ENTIRE code of the project, and I would certainly appreciate anyone's help in why this unexpected behavior takes place:

Code: [Select]
UNIT Unit1;

{$mode objfpc}{$H+}

INTERFACE

USES
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  keyboard;

TYPE

  { TForm1 }

  TForm1 = CLASS(TForm)
    Button1: TButton;
    Edit1: TEdit;
    PROCEDURE Button1Click(Sender: TObject);
    PROCEDURE KeyBoard(Sender: TObject; VAR PCKey: Char);

  private
    { private declarations }
  public
    { public declarations }
  END;

VAR
  Form1: TForm1;

IMPLEMENTATION

{$R *.lfm}

PROCEDURE TForm1.KeyBoard(Sender: TObject; VAR PCKey: Char);
BEGIN
  IF PCKey = 'q' THEN
  BEGIN
    Button1Click(nil);
  END;
END;

PROCEDURE TForm1.Button1Click(Sender: TObject);
BEGIN
  Form1.Edit1.Text := '';
  Form1.Edit1.SetFocus; {optional line, does not matter}
END;

END.
/////////////////////////////////////////////////////////////////     

I also hope that to your good understanding that I wouldn't seem an irresponsible one to have brought this issue with no care.

What I said about the character 'q' pressed going on his way to the Edit box, and not being stoppable is demonstrated in the  simple project. Just create a new project, add a TEdit box and a TButton. Nothing fancy. Easy to do.

For the Form, set  KeyPreview ON, and at events specify OnKeyPress to fire the method "Keyboard". Put in USES "Keyboard", as seen in the code.

The actual code one needs to demo the issue, is like four lines (not counting the usual declarations that Lazarus puts for procedures anyway).

I am adding this new message because I am still interested in resolving the issue, and others might as well.

Thanks anyone.
« Last Edit: August 08, 2011, 02:27:46 am by Elmug »

Dick, from the internet

  • Full Member
  • ***
  • Posts: 198
Re: Keyboard input tough problem here (actual code shown).
« Reply #17 on: August 08, 2011, 04:01:33 am »
Try this:
Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure Edit1KeyPress(Sender: TObject; var Key: char);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }


procedure TForm1.Button1Click(Sender: TObject);
begin
  Form1.Edit1.Text := '';
  Form1.Edit1.SetFocus; {optional line, does not matter}
end;
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: char);
begin
  if UpperCase(Key) = 'Q' then
    begin
      Edit1.Text := '';
      Key := #20;
    end;
end;

end.

 

TinyPortal © 2005-2018