Re: 'Del' shortcut of 'Delete' menu item and TEdit field

Giganews Newsgroups
Subject: Re: 'Del' shortcut of 'Delete' menu item and TEdit field
Posted by:  Peter Below (no…@nomail.please)
Date: Sat, 25 Sep 2010

Fred Fonar wrote:

> Hello, I have an issue of setting 'Del' as shortcut for 'Delete' menu
> item. I created simple example: just main menu with Delete item with
> Del shortcut and TEdit field. Hitting 'Del' key always intercepted by
> menu item even TEdit in focus, so I cannot delete selected
> characters. I've got on internet how Windows/VCL key handling
> mechanism works and it's stated that
>
> 1. You press a key.
> 2. WM_KEYDOWN or WM_SYSKEYDOWN message is put into the applications
> message queue.  3. The message loop code retrieves the message and,
> after some checks for special treatments (e.g. for MDI) hands it to a
> VCL function.  4. *The function converts the message to a CN_KEYDOWN
> message and sends it to the control with focus.* 5. The handler for
> this message in TWinControl first checks if the key is a keyboard
> shortcut for a menu item on the controls popup menu (if any), if not
> it checks all the popup menus of the controls parent controls and
> finally the forms main menu (if any). During this process the forms
> IsShortcut method is called and fires the OnShortcut event.
> IsShortcut also asks all actionlists on the form if one of their
> actions wants to handle the shortcut. IsShortcut and the OnShortcut
> event constitute the first ch ance to intercept the key at the form
> level ....  So, #5 comes before #4. Any explanations?

If you want the full picture, read this:
http://edn.embarcadero.com/article/38447

#5 is basically triggered from the processing the control does in #4
and it is done before the control does anything else with the message.
In the case of Windows standard controls (the edit control is one of
those) the actual action the key triggers does not happen in the CN_*
message handler. The CN_* messages are Delphi VCL messages, and the
native Windows control does not recognize them at all. The edit control
will handle the Del key when it receives the WM_KEYDOWN message, which
only happens after all the CN_KEYDOWN processing has been completed and
nobody set the key to handled on the way.

In your case you can override the form's IsShortcut method to resolve
the conflict:

{code: Delphi}
    function IsShortCut(var Message: TWMKey): Boolean; override;

function TForm1.IsShortCut(var Message: TWMKey): Boolean;
begin
  if (ActiveControl is TCustomEdit) and (Message.CharCode = VK_DELETE)
then
    Result := false
  else
    Result := inherited IsShortcut(Message);
end;

{code}

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

Replies

None

In response to

'Del' shortcut of 'Delete' menu item and TEdit field posted by Fred Fonar on Fri, 24 Sep 2010