Solved

Trapping the arrow keys

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month3 days, 15 hours left to enroll

630 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