Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Implementing Ctrl+F hotkey, like in Notepad?

209 views
Skip to first unread message

Warrick Wilson

unread,
Sep 12, 2007, 1:13:31 PM9/12/07
to
I'm banging my head on this, and I'm slowly expanding my request for help.
I've got an application from which a user can trigger an operation that
generates a bunch of text data. I've got a second form (say Form2) that has
a Memo component (from TMS Software, but I don't think that's relevant). To
show the generated data, I give it to the Memo component and then do a
Form2.ShowModal. So far, so good.

I want to add Find capability. My quick and dirty approach was to drop a
panel with a button on it, aligned at the bottom of Form2. The
button.OnClick does a FindDialog.Execute call (again, it's another TMS
Software component, but again, I don't think it's relevant). This works,
too. The button even has a shortcut/accelerator key "&Find" on it. This is
getting to be major slickness here...

I'm using BDS 2006, btw, and I DO think that's relevant...

I'd like to add the capability to invoke the Find dialog by pressing Ctrl+F.
Like how Notepad and other editors do. I asked on the TMS newsgroups, and
the one reply I got directed me to codegearguru.com, specifically Movie #25.
So I watched that and learn about CMDialogKey, etc. I then did some Google
searches (both web and groups), and there are lots of examples about
handling the Tab key like and Enter key (and vice versa), but not much in
terms of what I'm tring to do. There's certainly the possibility that I'm
not using the proper terminology or keywords, and if I knew what they were,
then I'd find some more illuminating stuff.

What I've tried so far is to handle the CMDialogKey message on my Form2,
like this: (note, Form2.KeyPreview is False, based on something I saw in the
movie on codegearguru.com)

procedure TForm2.CMDialogKey(var Message: TCMDialogKey);
begin
// If the form is visible and Ctrl+F is pressed, execute the Find Dialog
if (ResultsMemo.Visible)
and (Char(Message.CharCode) = 'F')
and (ssCtrl in KeyDataToShiftState(Message.KeyData)) then
begin
AdvMemoFindDialog1.Execute;
end
else
inherited;
end;

But in the debugger, this isn't even getting called...

So can someone please point me to the "Idiots Guide to Key Handling in
Delphi", or something like that?

Thanks.


Mike Williams (TeamB)

unread,
Sep 12, 2007, 1:35:22 PM9/12/07
to
Warrick Wilson wrote:

> So can someone please point me to the "Idiots Guide to Key Handling
> in Delphi", or something like that?

My suggestion is to use a menu item with a Ctrl+F shortcut. You can
either use a popup menu attached to the memo or, if you already have a
main menu you can add it there. Since you're using TMS components you
can use their menu components.

Another approach, which will require more work but is likely to be
better in the long run is to start using action lists. You define an
action for find, give it the Ctrl+F shortcut, and then attach that
action to a menu item or button, etc. In fact, there's even a standard
action for "Find" which does the work for you. For an example:

Create a new VCL forms application
Add a memo component
Add an ActionManager component
Double-click the action manager to open the designer
From the "new item" menu choose "New standard action"
Under Search choose tSearchFind
Close the action manager designer

Run the application and press Ctrl+F

--
-Mike (TeamB)

Peter Below (TeamB)

unread,
Sep 12, 2007, 1:40:47 PM9/12/07
to
Warrick Wilson wrote:

> I'm banging my head on this, and I'm slowly expanding my request for
> help. I've got an application from which a user can trigger an
> operation that generates a bunch of text data. I've got a second form
> (say Form2) that has a Memo component (from TMS Software, but I don't
> think that's relevant). To show the generated data, I give it to the
> Memo component and then do a Form2.ShowModal. So far, so good.
>
> I want to add Find capability. My quick and dirty approach was to
> drop a panel with a button on it, aligned at the bottom of Form2. The
> button.OnClick does a FindDialog.Execute call (again, it's another
> TMS Software component, but again, I don't think it's relevant). This
> works, too. The button even has a shortcut/accelerator key "&Find" on
> it. This is getting to be major slickness here...
>
> I'm using BDS 2006, btw, and I DO think that's relevant...
>
> I'd like to add the capability to invoke the Find dialog by pressing
> Ctrl+F.

Learn about using actionlists and actions. Actions are tied to UI
controls like buttons and menus and allow you to define additional
shortcut keys. They also make it easy to execute the same action
through different UI elements (e.g. a main menu item, a toolbutton on a
toolbar, plus an entry in a controls popup menu).

Drop a TActionlist on the form, double-click on it, press the Ins key
to add a new action. Select it in the action editor if it is not
selected. Move to the OI, set the Caption property to your buttons
caption (including the &), click on the Shortcut property, select
Ctrl-F from the dropdown. Move to the OIs events page and select the
buttons onClick event handler for the actions OnExecute event. Select
the button on the form and use the OI to set the buttons Action
property to the action you created in the steps before. Compile and
run. Ctrl-F should now "click" the button (not visibly, but it executes
the original OnClick event).

--
Peter Below (TeamB)
Don't be a vampire (http://slash7.com/pages/vampires),
use the newsgroup archives :
http://www.tamaracka.com/search.htm
http://groups.google.com

Woody (TMW)

unread,
Sep 12, 2007, 3:32:07 PM9/12/07
to
"Warrick Wilson" <war...@cwwilson.com> wrote in message
news:46e81e78$1...@newsgroups.borland.com...

> But in the debugger, this isn't even getting called...
>
> So can someone please point me to the "Idiots Guide to Key Handling in
> Delphi", or something like that?
>

Rule #1: Set form's KeyPreview property to TRUE.
NOTE: not sure if this is relevant for dialog keys but certainly
is for trapping other key events. :)

HTH
Woody (TMW)


Warrick Wilson

unread,
Sep 12, 2007, 3:20:00 PM9/12/07
to
"Peter Below (TeamB)" <none> wrote in message
news:xn0fb4c6...@newsgroups.borland.com...

> Warrick Wilson wrote:
>
>
> Learn about using actionlists and actions. Actions are tied to UI
> controls like buttons and menus and allow you to define additional
> shortcut keys. They also make it easy to execute the same action
> through different UI elements (e.g. a main menu item, a toolbutton on a
> toolbar, plus an entry in a controls popup menu).

Outstanding! I've got it working already. Thanks to you, Peter, and to Mike
for the answers.

I'd not ever played with Actions and ActionLists before ... something to do
some more learning about...


0 new messages