2008. szeptember 27., szombat

How to turn menu accelerators on and off


Problem/Question/Abstract:

How can I turn off menu accelerators? It's not done with MenuItem.enabled:=false. I need that because I have set the accelerators 'Delete' and 'Insert' to menu items, but in some situations those keys need to be "free" for other purposes (when the user is editing an EditBox, in my case).

Answer:

Your message implies, but doesn't state, that you are using standard windows shortcuts for Delete (Del key) and Insert (Ins key) features. You want to use them as the standard when the active control is an edit box but not under other circumstances.

I had a similar situation and resolved the problem using a TActionList. Use the OnExecute event to determine if you are using your own or windows processing. Unfortunately, the windows processing doesn't occur automatically and you have to handle the action yourself. Here's my solution for Delete:

procedure TfrmBuildQuery.alExpressionExecute(Action: TBasicAction; var Handled:
  Boolean);
begin
  if Action = aDelete then
    if eValue.Focused then
      {This is the edit control I needed to have windows-like changes}
    begin
      Handled := True; {Stops it from going to the delete action}
      with eValue do
      begin
        {If no selection, then select the char to the right of the cursor.}
        if SelLength = 0 then
          SelLength := 1;
        {Do the delete}
        SelText := '';
      end;
    end
    else
      Handled := False
  else
    Handled := False;
end;

Nincsenek megjegyzések:

Megjegyzés küldése