Solved

Trapping the arrow keys

Posted on 1998-10-08
5
387 Views
Last Modified: 2010-04-06
I have a form with a Panel on it, and I want the Panel to move when I press an arrow key.  I have set the form's KeyPreview property to true, and I am using the KeyDown event to try to trap the arrow keys.

Originally I tried using character keys instead, but that causes the character to be added into the DBGrid at the bottom of my form.

I have tried using WMGetDlgCode, but it doesn't seem to help (unless I have put it in the wrong place??)

I am using D1 and WFW 3.11
0
Comment
Question by:jdthedj
  • 2
  • 2
5 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1342311
hi jdthedj,

solve this your problem?

it's D3 code and i don't know does it works with D1

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
const step = 2;
begin
  if (key >= 37) and (key <= 40) then
  begin
    case key of
      37 : panel1.left := panel1.left - step;
      38 : panel1.top := panel1.top - step;
      39 : panel1.left := panel1.left + step;
      40 : panel1.top := panel1.top + step;
    end;
  end;
end;

procedure TForm1.StringGrid1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (key >= 37) and (key <= 40) then
  begin
    FormKeyDown(self, key, shift);
    key := 0;
  end;
end;


meikl
0
 
LVL 4

Accepted Solution

by:
BoRiS earned 100 total points
ID: 1342312
jdthedj

Here is some code that resizers a panel at runtime when the shift  + cursor keys(arrow keys) are pushed, you can just change the width to left and height to top...

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
 if ((Shift = [ssShift]) and (key = VK_LEFT)) then
  Panel1.width := Panel1.width -1;
   if ((Shift = [ssShift]) and (key = VK_RIGHT)) then
    Panel1.width := Panel1.width +1;
     if ((Shift = [ssShift]) and (key = VK_UP)) then
      Panel1.height := Panel1.height -1;
       if ((Shift = [ssShift]) and (key = VK_DOWN)) then
        Panel1.height := Panel1.height +1;
end;

Later
BoRiS
0
 
LVL 3

Author Comment

by:jdthedj
ID: 1342313
Hi BoRis and Kretzschmar

I have tried the code from both of you "as is" with a TPanel on a form.  Nothing happens!!

Any other ideas?
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1342314
hi jdthedj,

the property keypreview in the form ignores cursorkeys, so you must redirect the event from the active control (see my second procedure).

to trap the shift-state is a good idea BoRis

meikl
0
 
LVL 3

Author Comment

by:jdthedj
ID: 1342315
Hi Kretzschmar - thanks for the latest reply... I now understand why the second routine.... it works!
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question