Link to home
Start Free TrialLog in
Avatar of gaona
gaona

asked on

How to use the keys DOWN, UP and ENTER in the same way that in Clipper?

How to use the keys DOWN, UP and ENTER in the same way that in Clipper?  
 
In a program done in Clipper, same when he is publishing a field, the key DOWN it is going to the next field; the key UP is going to the previous field and the key ENTER it works like TAB (it is also going to the next field).  
Please show a routine that makes this.  
 
Thank you  
 
Gaona
Avatar of dwwang
dwwang

Write some code in the OnKeyPress/OnKeydown event of the components:

procedure Tmyform.edit1keypress(Sender: TObject; var Key: Char);
begin
     if Key=#13 then      //Enter pressed
        begin
        key:=#0;
        SelectNext(Sender as TWinControl,True,True);
        end;
end;

up/down can be processed similarly.
gaona

dwwang is totllay correct but if it's in a dbgrid as I see you are referenceing to field etc. the code needs to change here is a simple example

procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);

{ You should also set the Form's KeyPreview property to True }

begin
  if Key = #13 then                              
    if not (ActiveControl is TDBGrid) then begin
      Key := #0;                                
      Perform(WM_NEXTDLGCTL, 0, 0);              
    end
    else if (ActiveControl is TDBGrid) then      
      with TDBGrid(ActiveControl) do
        if selectedindex < (fieldcount -1) then  
          selectedindex := selectedindex +1
        else
          selectedindex := 0;
end;

for the up down stuff

procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);

{ You should also set the Form's KeyPreview property to True }

begin
  if Key = VK_DOWN then                              
    if not (ActiveControl is TDBGrid) then begin
      Key := #0;                                
      Perform(WM_NEXTDLGCTL, 0, 0);              
    end
    else if (ActiveControl is TDBGrid) then      
      with TDBGrid(ActiveControl) do
        if selectedindex < (fieldcount -1) then  
          selectedindex := selectedindex +1
        else
          selectedindex := 0;
end;

Etc.

Later
BoRiS
Avatar of gaona

ASKER

Dear Boris and  Dwwang
 
Its answer works partially. I suppose that I should also test the keys VK_UP and VK_DOWN to go to the previous field and the next field. Even so, I have three problems:  
 - The property alone OnKeyPress not accepts the change of behavior of the keys UP and DOWN when I am publishing the field.  
- To use the property OnKeyDown, I will have to redraft the same function, and even so, the key UP won't be recognized.  
- In a DBGrid, when I press DOWN or UP, it would like that the focus of the table, passed for the first field of DBGrid and not for the next field as it happens.  
 
Thankful  
 
Gaona
Would you give some mre details why it doew not work?

By the way, onkeypress and onkeydown are different, one treat key as a char and another treat it as Integer.

and for not to define onkeypress for every control, you can write a event procedure:

procedure keyprocess(sender: tobject; var key :char);
begin
     if key=#13 then ...
     if key=#(UP key character value) then ....
end;

Put this procedure among those event hanlers and then
in the property editer point the OnkeyPress event to this
procedure;
ASKER CERTIFIED SOLUTION
Avatar of dwwang
dwwang

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial