INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Monitor Key Press

Monitor Key Press

(OP)
More a general question:

I have a form with an TEdit attached. I want to determine the Text value of the TEdit when an <Enter> key is pressed. The value returned will either be an integer or 'T' or 'F'. I don't have the code for this... it's in a prelim stage. I want to access the information from a procedure or function that may or may not reside in the form unit.

The generated procedure is of the form:
procedure TMain_ScreenF.AnswerEKeyPress(Sender: TObject; var Key: Char);

and I'll be testing it for (27 = ord(key)) and if (13 = ord(key)) as well as T/F; the number is generally an integer.

Is there a better way to do this? I'd like to have the coding in a separate procedure or function that could access the keys pressed.

Any suggestions?

Dik

RE: Monitor Key Press

You mention that you want to determine the value of an Edit field when <Enter> is pressed. Did you mean when <Enter> is pressed while that Edit control has focus, or did you mean when the user presses <Enter> no matter which component has focus? Depending on the answer, you would have to impement different solutions.

RE: Monitor Key Press

(OP)
I'm lookikng at recording the value in the TEdit.Text when the <Enter> key is pressed. The TEdit has the focus.

Also found the GetKeyState funcion using it with VK_Return and am trying that approach.

Dik

RE: Monitor Key Press

(OP)
I'm trying to do something like:

CODE -->

function GetAnswer(): string;
{  Function monitors Main_ScreenF for keystorkes.  When a <Enter> key or an
   <Esc> key is pressed, the function checks the input to see if it is a
   valid entry and returns the value input in the AnswerT.Text.

   Parameters:

   Returns:   String variable from AnswerE.Text
}
var
  TmpStr: string;

begin
  Main_ScreenF.AnswerE.SetFocus;

  while NOT EnterKeyPressed do
  begin
    Result := Main_ScreenF.AnswerE.Text
  end;
end; // procedure


function EnterKeyPressed: Boolean;
begin
  Result := GetKeyState(VK_Return) < 0;
end; 

The above does not seem to recognise the <Enter> key

Dik

RE: Monitor Key Press

Best approach to limit and check the contents of an Edit control would be with OnKeyPress or OnKeyDown and or OnKeyUp and possibly even OnChange, but that doesn't need the ENTER key to be pressed, these are triggered for every key pressed (and in the case of OnKeyUp when the key is released). If you want to only check the content when the ENTER key is pressed, then you'd need to use OnFormKeyDown, but to get that to work, you also need to set the Form's KeyPreview property to TRUE.

RE: Monitor Key Press

(OP)
That was the approach I'd used in my original posting, but I need to access the information from another procedure without using a global variable to store it temporarily. I was thinking that there had to be a more simple manner of doing that.

Dik

CODE -->

procedure TMainF.AnswerEKeyPress(Sender: TObject; var Key: Char);
var
  ButtonSelected: integer;
  TxtValue, TxtTest: integer;                               // submitted answer
  IntAns: integer;                                 // submitted answer: integer
//  StrAns: string;                                   // submitted answer: string
  TmpStr: string;
//  Right, Wrong: integer;                       // correct and incorrect answers

begin
  if (27 = ord(key)) then                               // <Escape> key pressed
  begin
    buttonSelected := MessageDlg('Are you sure you want to exit?', mtConfirmation, [mbYes, mbNo], 0);

    if buttonSelected = mrYes then
    begin
      application.Terminate;                          // PROVIDE PROPER CLOSURE
    end;
  end // if <Esc> key pressed
  else if (13 = ord(key)) then                           // <Enter> key pressed
  begin
    val(answerE.Text, TxtValue, TxtTest);                  // test for a number

    if TxtTest = 0 then                                   // answer is a number
    begin
      ShowMessage('Answer is:  ' + answerE.Text);
      IntAns := StrToInt(answerE.Text);
      application.Terminate
    end
    else                                              // answer is not a number
    begin
      TmpStr := AnsiUpperCase(Trim(AnswerE.Text));

      if (TmpStr = 'F') or (TmpStr = 'T') then        // True or False Selected
      begin
        ShowMessage('Answer is ' + TmpStr);
      end
      else
      begin
        answerE.Text := '';
        ShowMessage('Answer is NOT a Number and zeroed');
      end;
    end;
  end; // if <Enter> key pressed
end; // procedure 

Is it possible to write a function that accesses the information from the Edit.Text control from a procedure or function, monitoring the keys from the keypress action? I need the Edit box to only accept 0-9 and f, F, t, and T.

Dik

RE: Monitor Key Press

Quote:

Is it possible to write a function that accesses the information from the Edit.Text control from a procedure or function, monitoring the keys from the keypress action? I need the Edit box to only accept 0-9 and f, F, t, and T

Yes, all this is possible, I'll try to put together something for you later, but first, which version of Delphi are you using?

RE: Monitor Key Press

(OP)
10.1... used to have a registered copy of Delphi 5. The 10.1 was a promotional 'freeby', I had it for about 6 months before I decided to try it out. I am trying to port a program I wrote in BASIC 7.2 about 30 years ago to Windows... and have been away from programming for a long time. Just got the hang of how to set up functions, procedures and variables. I'm trying another tack to see if I can get the keys to register correctly... key down is much better than key up or key press... more features.

Dik

RE: Monitor Key Press

Since there are two forms (at least that what I've understood) here is the code to two different forms.

Form1 talks to Form2 via a windows message, and form2 calls a PUBLIC function defined in Form1 to get the contents of the Edit Control

CODE

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages,
  System.SysUtils, System.Variants, System.Classes,
  Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Unit2;
  //make sure you add reference to FORM2 form HERE for main form.

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    function GetEdit1Content: String;//add this line manually then hold Shift-Control-C and 
	                                 //Delphi will add the function stub for you to fill in
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
   Form2.Show;
end;

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if not (CharInSet(Key, ['t','T','f','F','0'..'9', #13, #9, #8])) then
    Key := #0; //clear the key to null if it isn't one of your acceptable values (added backspace #8, tab #9 and Enter #13 as acceptable)
  if Key = #13 then //sends the message only if Enter is clicked while Edit1 has focus.
    SendMessage(Form2.Handle, WM_ENTERKEY_HIT, 0, 0);
end;

function TForm1.GetEdit1Content: String;
begin
   Result := Edit1.Text;//this is public function callable from your other forms
end;

end. 

CODE

unit Unit2;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

const
  WM_ENTERKEY_HIT = WM_USER + 1;
//add the message constant
type
  TForm2 = class(TForm)
    Memo1: TMemo;
  private
    { Private declarations }
  public
    procedure WMENTERKEYHIT(var Msg: TMessage); message WM_ENTERKEY_HIT;//add this click Shift-Control-C, Delphi creates stub proc
  end;

var
  Form2: TForm2;

implementation

Uses Unit1;//make sure you add reference to FORM1's unit AFTER implementation section (you need to also add 'Uses' not just the unit name

{$R *.dfm}

{ TForm2 }

procedure TForm2.WMENTERKEYHIT(var Msg: TMessage);
begin
  Memo1.Text := Form1.GetEdit1Content;
end;

end. 

RE: Monitor Key Press

(OP)
Thanks so much... I'll take a look at them tonight. Looking at starting the program from formshow with the initial problem and then doing everything from the edit.keydown... maybe another solution.

Dik

RE: Monitor Key Press

i used to do similar things a while ago

the content ( string ) of an Edit field was checked by specific routines, so to verify if the content verifies a mask

this could occur :
... on EditExit
... on general request, like when verifing each input field of a record, before adding the entity to the record
... on Enter

so build masks : i have some that detects if a content ( string ) is number, real number, number in range, alpanumeric chars, etc

after that, call those routines as you like

kindly !

RE: Monitor Key Press

(OP)
Thanks... majlumbo has also been incredibly helpful... with a little tinkering the source he provided is very helpful.

Yup... just a matter of becoming more familiar with the program again... Something like:

if (Key in [8, 13, 27, 48..57, 96..105, 70, 84]) then
begin

thanks again, Dik

RE: Monitor Key Press

more than likely you didn't catch my comment when I provided this code,

Quote:


sends the message only if Enter is clicked while Edit1 has focus.

if you want it to send the message ANY TIME the enter key is pressed, then you don't want to use Edit1's OnKeypress, you would use the FORM's OnKeyPress but then you would need to set the FORM's KeyPreview to True.

CODE

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if NOT (CharInSet(Key, ['t', 'T', 'f', 'F', '0'..'9', #8, #9, #13])) then
  begin
    Key := #0;
  end;
end;

procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    SendMessage(Form2.Handle, WM_ENTERKEY_HIT, 0, 0);
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  KeyPreview := True;
end; 

Now anytime the ENTER key is clicked, it'll send the message.

RE: Monitor Key Press

(OP)
Thanks... that would explain the intermittent actions... If the form has focus, then, do all objects on the form notify the form that an activity has occurred or is the form notified that an activity has occurred only if it has happened over the form alone?

Dik

RE: Monitor Key Press

The event is now associated with the entire form, so if the enter key is clicked while that form has focus, it will kick off the event handler. If you want it to happen no matter which form has focus, that is a bit trickier.

RE: Monitor Key Press

(OP)
Thanks... you've been an immense help.

Dik

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close