views:

175

answers:

1

When I set the property KeyPreview = TRUE on my main form (MDI Parent) the OnKeyDown, OnKeyUp or OnKeyPress events don't get fired. When pressing keys, I can navigate the main menu (TActionMenuBar). It seems the menu bar has the control over the key events.

How can I fire an event, when the user is pressing a key (e.g. the VK_RETURN key)?

I am using Delphi 2009

A: 

From the comments I see that you want to do a certain action (eg. open a form) when a certain key is pressed.

For this, you can assign your shortcut key to an TAction and write the appropriate code in the TAction.OnExecute

Steps:

  1. Drop a TActionManager (or TActionList)
  2. Double click on it (the TActionManager's editor appear). Press 'New Action' button.
  3. Set the ShortCut property to the desired value.
  4. Write the appropriate code in OnExecute event.

Note that on step 3. (perhaps) you (and most probably your users) will have trouble assigning the Enter key to something so uncommon like opening a form. 'Enter' has a very defined meaning in Windows.

I'd suggest to assign a functional key (F2-F9 usually) and/or Ctrl+[the first letter of your form's name]

HTH

Is it possible to set the ENTER key for as ShortCut? I cannot see it in the TAction.ShortCut list. FYI: In our application we use the ENTER key a lot.
max
Yes. Just write it. :-) Go to the `Shortcut` property and type 'Enter' (without the quotes). Press [Enter] (the key). In order to test it, go to `OnExecute` event and write some testing code like `ShowMessage('Boo!');` or similar. You will see that code being executed.The reason for which the 'Enter' isn't included in that list is that, as I said, its usage is discouraged for custom actions. This isn't (un)fortunately my choice or Delphi's but a Windows convention.But if it fits for your application, go ahead, even if sometimes Windows will hook it. Test and if it works...
hm ... I cannot set the ENTER key in the ShortCut property in D2009. I get the error "Inalid Property Value". But it works in D7?
max
Don't know for D7 Just typing `Enter` in D2010 works. And imho it should work also in D2009 `TextToShortCut` (the underlying engine for this) is a rather old Delphi function. Try type, for example, `A+Ctrl` inside of `ShortCut` property. It works? (The IDE should accept it and change it to Ctrl+A)