Solved

Trapping the arrow keys

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

740 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