-2

I have a form with a TDBGrid and TClientDatabase.

I want to allow using the UpArrow or DownArrow to navigate in the database no matter which control has focus.

I set the Form.KeyPreview := true

This is the Form.OnKeyPress

procedure TfrmMain.FormKeyPress(Sender: TObject; var Key: Char); 
var Direction : Integer; 
begin    
   Direction := -1;    
   if Key = VK_UP then Direction := 1; {Previous}    
   if Key = VK_DOWN then Direction := 0; {Next}       
   if Direction <> -1 then    
   begin
      if Direction = 0 then cds1.Next else cds1.Prior;
      NextRecord; {Processes AfterScroll event}    
   end; 
end;

This gives me an error E2008 Incompatible types

What am I doing wrong?

ChuckO
  • 2,543
  • 6
  • 34
  • 41
  • On which line do you get the error? – MartynA Dec 20 '15 at 17:06
  • Using `FormShortCut` seems to work well: `if ((Msg.CharCode = VK_UP) or (Msg.CharCode = VK_DOWN)) then begin SelectNext(ActiveControl, (Msg.CharCode = VK_DOWN), True); Handled := True; end` – Ron Maupin Dec 20 '15 at 17:25

1 Answers1

2
  1. Key is Char, but VK_UP is integer constant. You have to use these constants in KeyDown event handler that has parameter Key:Word

  2. Moreover, arrow keys don't generate OnKeyPress event (it is intended for alphanumeric keys)

  3. From help: Navigation keys (Tab, BackTab, the arrow keys, and so on) are unaffected by KeyPreview because they do not generate keyboard events

MBo
  • 77,366
  • 5
  • 53
  • 86