Jump to content

Grid KeyPress Event - Process SPACE Bar Press - How ?


andyhill

Recommended Posts

Trying to process Grid Keyboard SPACE pressed event.

      dbGridPurchases.ClientEvents.ExtEvents.Clear;
      s:= 'keypress=function keypress(e, t, eOpts)'#13#10 +
          '{ '#13#10 +
          '  ajaxRequest(sender, '#39'_KeyPress_'#39', ["key="+e.key]); '#13#10 +
          '} ';
      dbGridPurchases.ClientEvents.ExtEvents.Add(s);

Please advise - thanks in advance

 

Link to comment
Share on other sites

I suppose different solutions are possible (including monitoring Form KeyPress) BUT after seeing 

procedure dbGridInvoicesKeyPress(Sender: TObject; var Key: Char); not work

I then tried the JS way as shown above which also does not work ? I assume I have done something wrong ?

I would like to do this the JS way if you would be so kind and show me how please - thanks Sherzod.

Link to comment
Share on other sites

 

      dbGridPurchases.ClientEvents.ExtEvents.Clear;
      s:= 'keypress=function keypress(e, t, eOpts)'#13#10 +
          '{ '#13#10 +
          '  ajaxRequest(sender, '#39'_KeyPressed_'#39', ["key="+e.getKey()]); '#13#10 +
          '} ';
      dbGridPurchases.ClientEvents.ExtEvents.Add(s);

...

//////////////////////////////////////////////////////////////////////////////
procedure TfMain.dbGridPurchasesAjaxEvent(Sender: TComponent; EventName: string; Params: TUniStrings);

...

if EventName = '_KeyPressed_' then begin
  if Params.Values['key'] = ' ' then begin
    ReadOnly:= False;
    if UniMainModule.tblPurchases.ReadOnly = True then begin
      ReadOnly:= True;
      UniMainModule.tblPurchases.ReadOnly:= False;
    end;
    if UniMainModule.dsPurchases.DataSet.State in [dsInsert, dsEdit] then begin // DB
      // do nothing
    end else begin
      UniMainModule.tblPurchases.Edit;
    end;
    if UniMainModule.tblPurchases.FieldByName('Tag').AsBoolean = True then begin
      UniMainModule.tblPurchases.FieldByName('Tag').AsBoolean:= False;
    end else begin
      UniMainModule.tblPurchases.FieldByName('Tag').AsBoolean:= True;
    end;
    UniMainModule.tblPurchases.Post;
    if ReadOnly = True then begin
      UniMainModule.tblPurchases.ReadOnly:= True;
    end;
  end;
end;

 

 

Link to comment
Share on other sites

Sherzod, do you understand from my code above ?

The Grid is not in edit mode, user scrolls Grid Rows, user presses SPACE bar on a desired Row, I capture the Key Event and test for SPACE Key - if pressed I then flip the Table Row Field TAG to True or False accordingly.

Please advise

Link to comment
Share on other sites

8 hours ago, andyhill said:

The Grid is not in edit mode, user scrolls Grid Rows, user presses SPACE bar on a desired Row, I capture the Key Event and test for SPACE Key

Have you tried this?

procedure TMainForm.UniDBGrid1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = VK_SPACE then
    ShowMessage('spacebar pressed')
end;

 

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...