When the cursor is e.g. in a Edit field, pressing and releasing the Alt key (without pressing any other key) causes the Edit field to lose the focus. This happens also with any other focused control. How can this be prevented in a Delphi program for any focused control?

有帮助吗?

解决方案

A better way to do this with fewer unintended consequences is to be extremely precise about it - I would suggest :

In your form, override WndProc :

TForm1 = class(TForm)
  Edit1: TEdit;
private
   FSuppress : boolean;
protected
   procedure WndProc(var Message : TMessage); override;
end;

And implement like this :

procedure TForm1.WndProc(var Message : TMessage);
begin
  if (Message.Msg = WM_SYSCOMMAND) and
     (Message.WParam = SC_KEYMENU) and
     FSuppress then Exit;

  inherited WndProc(Message);
end;

This is the windows message for a system command and the specific WParam that indicates it is for retrieving the menu triggered by keystroke. Set FSuppress on any controls you wish to keep focus :

procedure TForm1.Edit1Enter(Sender: TObject);
begin
  FSuppress := true;
end;

procedure TForm1.Edit1Exit(Sender: TObject);
begin
  FSuppress := false;
end;

This will not disable the ALT key, but will disable, specifically, the activation of the menu while Edit1 has focus. Critically, shortcuts like ALT + F4 to exit the program or ALT+TAB to switch windows will still work.

I agree with most of the comments, however, in that this is probably not the best solution to the LCD of your user base. You're essentially crippling the program for competent users to pander to the failings of the incompetent ones. Perhaps make it an option like Windows sticky-keys or accessibility options for variously disabled persons.

其他提示

procedure SendKey_ALT;
begin
    keybd_event(VK_MENU, MapVirtualKey(VK_MENU, 0), 0, 0);
    keybd_event(VK_MENU, MapVirtualKey(VK_MENU, 0), KEYEVENTF_KEYUP, 0);
end;

Call the above procedure in your FormCreate() method. This will solve the problem.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top