Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Trapping the arrow keys

Posted on 1998-10-08
5
Medium Priority
?
392 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 400 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

722 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