keydown和KeyPress事件有何不同

KEYPRESS
When a windowed control receives a key-press message (WM_CHAR) from Windows, its message handler calls the DoKeyPress method.
说明:响应WM_CHAR消息,不包括一些功能键,如:F1,SHIFT键等

KEYDOWN
When a windowed control receives a key-down message (WM_KEYDOWN) from Windows, its message handler calls the DoKeyDown method. 

响应WM_KEYDOWN消息


一个是按下去,一个是按下放上来时


procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
  //只能触发单键事件
end;

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  //在keyDown事件里可以触发shift+A等组合键事件
end;


Drate回答正确,其它人都没搞清楚,那是OnKeyUp!其实响应WM_CHAR就是按下字符键时激发,而按下其他功能键无效!

procedure WMChar(var Message: TWMChar); message WM_CHAR;

procedure TWinControl.WMChar(var Message: TWMChar);
begin
  if not DoKeyPress(Message) then inherited;
end;

procedure WMKeyDown(var Message: TWMKeyDown); message WM_KEYDOWN;

procedure TWinControl.WMKeyDown(var Message: TWMKeyDown);
begin
  if not DoKeyDown(Message) then inherited;
end;

猜你喜欢

转载自www.cnblogs.com/jijm123/p/10351562.html