Solved

Trapping the arrow keys

Posted on 1998-10-08
5
383 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video discusses moving either the default database or any database to a new volume.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now