pjhayward's Pascal Tutorial


pjhayward.net Home

Got a question?

Tutorial Home
Preparation
Lesson One - Sample Program
Lesson Two - Program Structure
Lesson Three - Data Types
and Constants
Lesson Four - Variables
Lesson Five - Text I/O
Lesson Six - Subroutines
Lesson Seven - Conditional
Statements
Lesson Eight - Arrays
Lesson Nine - Loops
Lesson Ten - Units
How do I use the arrow keys in pascal?
Unfortunately, I only know how to answer this for DOS and Windows. Under Linux I would presume you'd want the ncurses library, but I personally don't know how to use it. Moving on.

DOS

The simplest method for using the arrow keys is to use the readkey function from the CRT unit. The trick is that the arrow keys (and a number of other keys) send a two-character code. Here's a typical loop for using arrow keys in DOS:
uses crt;

var
  key:char;
  runloop:boolean;
begin
  runloop:=true;
  while runloop do begin
  ... do some stuff ...
  if keypressed then begin
    key:=readkey;
    if keypressed then key:=readkey; { catch the second part of double character codes }
    case key of
      'K': ... do something for the left key ...
      'M': ... do something for the right key ...
      'H': ... do something for the up key ...
      'P': ... do something for the down key ...
      #27:runloop:=false; { #27 = ESC key }
    end; {case key of}
  end;  {if keypressed}
  .. do more stuff ..
  end; {while runloop}
end.
The key part of that code is that you capture the second character of the double character code, then interpret that to find out which key was pressed. This particular bit of code would also permit a user pressing the same characters on the rest of the keyboard to simulate the arrow keys. That could be avoided by putting the case key of... segment inside a begin..end block that started with the second keypressed call, like this:
  ... do some stuff ...
  if keypressed then begin
    key:=readkey;
    if keypressed then begin
      key:=readkey; { catch the second part of double character codes }
      case key of
        'K': ... do something for the left key ...
        'M': ... do something for the right key ...
        'H': ... do something for the up key ...
        'P': ... do something for the down key ...
      end; {case key of}
    end;  {if keypressed 2}
    if key=#27 then runloop:=false; { #27 = ESC key }
  end; {if keypressed}
  .. do more stuff ..
You could also define some constants to make the code less confusing, like this:
const
  Left_Key='K';
  Right_Key='M';
  Up_Key='H';
  Down_Key='P';

Windows

Capturing the arrow keys under windows is somewhat simpler, assuming you're using Delphi. You just create an OnKeyDown procedure for the control you want to watch. One of the parameters of the procedure is named Key, and it's a word value. The virtual key codes for Windows are stored in the windows.pas file (under Program Files\Borland\Delphi \source\Rtl\win), but the arrow keys are pretty straightforward. Here's a generalized procedure:
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  case Key of
    VK_Left: .. do something for the left key..
    VK_Right: .. do something for the right key..
    VK_Up: .. do something for the up key..
    VK_Down: .. do something for the down key..
  end;
end;
Back to Question/Answer list