Solved

Handling key presses on a TEdit

Posted on 2008-10-25
5
221 Views
Last Modified: 2013-11-23
Hi all,

Just a question I can't figure out.

I've got a TEdit which must handle the - and + of the numeric keypad, but not the - and + of the alphanumeri part of the keyboard.
When I put a break point on the OnKeyDown event I see that it makes a difference on the numeric and alphanumeric part of the - sign (#189 and #109).

But how can I code that when the subtract key is pressed on the numeric keypad that it will not put the - in the TEdit box and will do when the alphanumeric - is pressed?

Thanks in advance,

JeePeeTee
0
Comment
Question by:JeePeeTee
[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
  • 3
  • 2
5 Comments
 
LVL 28

Accepted Solution

by:
2266180 earned 250 total points
ID: 22802915
something like this?
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
 
type
  TForm1 = class(TForm)
    Edit1: TEdit;
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
    procedure Edit1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
    pressed:word;
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if (key='-') and (pressed=109) then
    key:=#0;
end;
 
procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  pressed:=key;
end;
 
end.

Open in new window

0
 
LVL 4

Author Comment

by:JeePeeTee
ID: 22803034
Thanks,

Kinda weird that you can't do it without declaring a pricate symbol.
But ah, well, it works!

0
 
LVL 4

Author Closing Comment

by:JeePeeTee
ID: 31509932
It works, though it's weird that it must be done with a private symbol.
0
 
LVL 28

Expert Comment

by:2266180
ID: 22803073
Why a B grade? see grading tips here: http://www.experts-exchange.com/help.jsp#hi403
0
 
LVL 4

Author Comment

by:JeePeeTee
ID: 22812682
It's a B-grade because the solution is good, but not excellent. Like I mentioned, the solution works, but I think it's kinda weird that I need a private symbol to pass trhough two seprate OnKey events.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Tidtcpserver listening on multiports? 1 55
Wincontrol not (correctly) drawn 15 59
Breakpoint doesn't stop in my variable 3 48
Get weeknumber and year from date 4 56
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…
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…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 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