如何在OnKeyUp事件上验证按下的键

时间:2017-01-28 17:08:11

标签: pascal lazarus

我有很多行来检查edit1.text字段的类型内容,但是我需要使用OnKeyUp事件(由于某些原因),所以如何将此代码“转换”为Key:Word type(正常它的关键:OnKeyPress的Char?)

if not (key in ['0'..'9', #08]) then begin
  key:=#0;

1 个答案:

答案 0 :(得分:0)

使用Ord(Key)将键盘键的Word值转换为字符值。否则使用VK_值(如果可用)。

在你的片段中,你将char('0'..'9')与值(#08)混合在一起,我不确定你是否需要测试它。如果没有,那么(#08)需要在另一个比较中直接检查Key或Chr(Key)。

最好使用VK_UNKNOWN而不是#0。

这是我在Lazarus编写的一些工作代码中使用的KeyUp事件。 HTH。

    procedure TfrmMain.lbCmdLinesKeyUp( Sender : TObject; var Key : Word; Shift : TShiftState );
begin

  if ( Key = vk_return ) and ( lbCmdLines.ItemIndex > -1 ) then
  begin
    if ( Shift = [ ssCtrl ] ) then
    begin
      if btnLineEdit.Enabled then
        btnLineEdit.Click;
    end
    else
    begin
      if btnRun.Enabled then
        btnRun.Click;
    end;
  end;

  if ( Key = Ord( 'C' ) ) or ( Key = Ord( 'c' ) ) then
  begin
    if Shift = [ ssCtrl ] then
        actCopyClipCmdLine.Execute;
//order, apparently, doesn't matter, both work
    //if Shift = [ ssCtrl, ssShift ] then
    if Shift = [ ssShift, ssCtrl ] then
      mniCopyCLListClipClick( Self );
    //if ( ssCtrl in Shift ) and ( ssShift in Shift ) and ( ssAlt in Shift ) then
    if Shift = [ ssCtrl, ssShift, ssAlt ] then
      actCopyCmdLine.Execute;
  end;
end;
相关问题