Delphi の Edit(TEdit) で Alt+Enter 押下時の音(Beep音)を消す
Enter, Ctrl+Enter, Shift+Enter あたりは、OnKeyDown や OnKeyPress などで処理可能ですが
- procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
- begin
- if (Key = VK_RETURN) and ( (Shift = [ssShift]) or (Shift = [ssCtrl]) ) then begin
- Key := 0;
- Shift := [];
- end;
- end;
-
- procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
- begin
- if (Key = #13) or (Key = #10) then begin
- Key := #0;
- end;
- end;
procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
if (Key = VK_RETURN) and ( (Shift = [ssShift]) or (Shift = [ssCtrl]) ) then begin
Key := 0;
Shift := [];
end;
end;
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if (Key = #13) or (Key = #10) then begin
Key := #0;
end;
end;
Alt+Enter は押下を補足するものの書き換えは無視されてしまう
- procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
- begin
- if (Key = VK_RETURN) and (Shift = [ssAlt]) then begin
- Key := 0;
- Shift := [];
- end;
- end;
procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
if (Key = VK_RETURN) and (Shift = [ssAlt]) then begin
Key := 0;
Shift := [];
end;
end;
今回は TApplicationEvents を利用して、Alt+Enter を補足 + 無効化
フォームに TApplicationEvents(ApplicationEvents1)

コンポーネントを置いて、OnMessage で補足
※対象は TEdit(Edit1)
- procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);
- begin
- if (Msg.message = WM_SYSKEYDOWN) then begin
- if (Msg.wParam = VK_RETURN) and (GetKeyState(VK_MENU) < 0) then begin
- if (Msg.hwnd = Edit1.Handle) then begin
-
- Handled := True;
- end;
- end;
- end;
- end;
procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);
begin
if (Msg.message = WM_SYSKEYDOWN) then begin
if (Msg.wParam = VK_RETURN) and (GetKeyState(VK_MENU) < 0) then begin
if (Msg.hwnd = Edit1.Handle) then begin
// Alt+Enter で行いたい処理
Handled := True;
end;
end;
end;
end;
参考文献
コメント