Discussion:
RichEdit: How to Get Rid of a Keypress?
(too old to reply)
Fay Kalyus
2004-01-31 16:39:09 UTC
Permalink
Using RichEdit, I am creating a file viewer and I would like to allow users
to backtrack to a previous point. Following the logic of web browsers, I
would like to use the backspace key for this, and have it work whether or
not the RichEdit control has focus.

Accordingly, then, I set the form's KeyPreview property to True and did
something like this:

procedure TMyForm.FormKeyPress(Sender: TObject; var Key: Char);
begin
case Ord(Key) of
8 : DoSomeStuff; { Backspace key }
13 : DoSomeOtherStuff; { Enter key }
27 : Close; { Esc key closes the window }
6,70,102 : DoFind; { Ctrl-F or F and Shift-F }
end;
Key := #0; { Attempt to destroy to the key }
end;

For the most part, this works perfectly! Unfortunately, after I trap and
act on the Backspace (decimal character #8) in FormKeyPress, Delphi merrily
passes the #8 to RichEdit, which beeps (since ReadOnly is set to True). It
doesn't notice that I set Key to #0.

Even stranger: setting the Key to #0 works fine for all of the other keys.
What works for character #6 (Ctrl-F) doesn't work for character #8.
Apparently it is a "special case".

I also tried resetting the Key value in the KeyPress and KeyUp procedures
for the form and in RichEdit. It didn't help.

My question, then, is this ...

How can I get a form to detect a key, then tell Windows to forget about it?
J French
2004-01-31 17:12:49 UTC
Permalink
Try the KeyDown Event of the Form
Post by Fay Kalyus
Using RichEdit, I am creating a file viewer and I would like to allow users
to backtrack to a previous point. Following the logic of web browsers, I
would like to use the backspace key for this, and have it work whether or
not the RichEdit control has focus.
Accordingly, then, I set the form's KeyPreview property to True and did
procedure TMyForm.FormKeyPress(Sender: TObject; var Key: Char);
begin
case Ord(Key) of
8 : DoSomeStuff; { Backspace key }
13 : DoSomeOtherStuff; { Enter key }
27 : Close; { Esc key closes the window }
6,70,102 : DoFind; { Ctrl-F or F and Shift-F }
end;
Key := #0; { Attempt to destroy to the key }
end;
For the most part, this works perfectly! Unfortunately, after I trap and
act on the Backspace (decimal character #8) in FormKeyPress, Delphi merrily
passes the #8 to RichEdit, which beeps (since ReadOnly is set to True). It
doesn't notice that I set Key to #0.
Even stranger: setting the Key to #0 works fine for all of the other keys.
What works for character #6 (Ctrl-F) doesn't work for character #8.
Apparently it is a "special case".
I also tried resetting the Key value in the KeyPress and KeyUp procedures
for the form and in RichEdit. It didn't help.
My question, then, is this ...
How can I get a form to detect a key, then tell Windows to forget about it?
AlanGLLoyd
2004-01-31 19:20:00 UTC
Permalink
Post by Fay Kalyus
Accordingly, then, I set the form's KeyPreview property to True and did
procedure TMyForm.FormKeyPress(Sender: TObject; var Key: Char);
begin
case Ord(Key) of
8 : DoSomeStuff; { Backspace key }
13 : DoSomeOtherStuff; { Enter key }
27 : Close; { Esc key closes the window }
6,70,102 : DoFind; { Ctrl-F or F and Shift-F }
end;
Key := #0; { Attempt to destroy to the key }
end;
For the most part, this works perfectly! Unfortunately, after I trap and
act on the Backspace (decimal character #8) in FormKeyPress, Delphi merrily
passes the #8 to RichEdit, which beeps (since ReadOnly is set to True). It
doesn't notice that I set Key to #0.
You need to put it (and preferably all the others) in OnKeyDown.

case Key
VK_BACK : DoSomeStuff; { Backspace key }
VK_RETURN : DoSomeOtherStuff; { Enter key }
VK_ESCAPE : Close; { Esc key closes the window }
VK_F : DoFind; {F}
end;
Key := 0; { Attempt to destroy to the key }

if you specifically want Ctrl-F then you need to check for Ctrl pressed
(GetKeyState(VK_CONTROL) < 0).

Note that VK_F may not be declared in Delphi. its value is $46.

Alan Lloyd
***@aol.com
Fay Kalyus
2004-01-31 20:20:03 UTC
Permalink
You're a genius! That worked like a charm!

I don't HAVE to use Ctrl-F, by the way. I have a "Find" key, which the user
can click, or they use the shortcut Alt-F. However, just to be nice I also
let them use F, Shift-F, and Ctrl-F. I mean, I use lots of different
programs that have different key combos for "Find", and I sometimes hit the
wrong one. (NoteTab, my text editor, uses Ctrl-F for Find.)

To this very day I STILL sometimes press Ctrl-H when I want to backspace.
Remember that? And Ctrl-QF to initiate a Find. Hmm, maybe I'd better allow
Ctrl-Q as well as Ctrl-F!

Just for fun, I tried pressing Ctrl-H in my Outlook editor (where I'm typing
this). It just ignores me.
Post by AlanGLLoyd
Post by Fay Kalyus
Accordingly, then, I set the form's KeyPreview property to True and did
procedure TMyForm.FormKeyPress(Sender: TObject; var Key: Char);
begin
case Ord(Key) of
8 : DoSomeStuff; { Backspace key }
13 : DoSomeOtherStuff; { Enter key }
27 : Close; { Esc key closes the window }
6,70,102 : DoFind; { Ctrl-F or F and Shift-F }
end;
Key := #0; { Attempt to destroy to the key }
end;
For the most part, this works perfectly! Unfortunately, after I trap and
act on the Backspace (decimal character #8) in FormKeyPress, Delphi merrily
passes the #8 to RichEdit, which beeps (since ReadOnly is set to True).
It
Post by AlanGLLoyd
Post by Fay Kalyus
doesn't notice that I set Key to #0.
You need to put it (and preferably all the others) in OnKeyDown.
case Key
VK_BACK : DoSomeStuff; { Backspace key }
VK_RETURN : DoSomeOtherStuff; { Enter key }
VK_ESCAPE : Close; { Esc key closes the window }
VK_F : DoFind; {F}
end;
Key := 0; { Attempt to destroy to the key }
if you specifically want Ctrl-F then you need to check for Ctrl pressed
(GetKeyState(VK_CONTROL) < 0).
Note that VK_F may not be declared in Delphi. its value is $46.
Alan Lloyd
AlanGLLoyd
2004-01-31 22:08:24 UTC
Permalink
Post by Fay Kalyus
However, just to be nice I also
let them use F, Shift-F, and Ctrl-F
VK_F means the key marked F - so it does not matter what modifier you use it
will send that Key value to OnKeyDown. As I said, if you specifically want to
separate Ctrl-F from Shift-F or Alt-F, you will have to use GetKeyState() to
check what is also being pressed.

I have a small app which reports what the effect of downing, pressing, or
upping any key or mouse button is in these event handlers, also what the
GetKeyState() returns for modifier or toggled keys. EMail me if you would like
a copy.

I note that we still have the Wordstar Ctrl-Y to delete a line <g>.

Alan Lloyd
***@aol.com
Fay Kalyus
2004-02-01 00:40:58 UTC
Permalink
Thanks for the offer, but I've managed to get it working exactly as I
wanted.

So here are all the keys I've defined for "Find":

Alt-F (Since it's the underlined key on my form's "Find" button)
Ctrl-F (This is "Find" in Internet Explorer and many other programs)
F and Shift-F (sure, why not?)
Ctrl-Q (for people like me who still occasionally use an editor with
WordStar mappings)
Slash (for those of us who used Vernon Buerg's LIST program for years)
Backslash (for those who once used LIST but can't remember which key it
was)

I think that covers the bases pretty well!

Following the same logic, "Repeat Find" is Alt-R, Ctrl-R, R, Shift-R, Ctrl-L
and F3 -- I can't tell you how many times I've hit F3 in Internet Explorer
trying to get it to repeat the last Find! Grrr.
Post by AlanGLLoyd
Post by Fay Kalyus
However, just to be nice I also
let them use F, Shift-F, and Ctrl-F
VK_F means the key marked F - so it does not matter what modifier you use it
will send that Key value to OnKeyDown. As I said, if you specifically want to
separate Ctrl-F from Shift-F or Alt-F, you will have to use GetKeyState() to
check what is also being pressed.
I have a small app which reports what the effect of downing, pressing, or
upping any key or mouse button is in these event handlers, also what the
GetKeyState() returns for modifier or toggled keys. EMail me if you would like
a copy.
I note that we still have the Wordstar Ctrl-Y to delete a line <g>.
Alan Lloyd
Loading...