KeyPreview:=True;procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
    if Key=27 then
    begin
        while (GetKeyState(27)<>0) And (GetKeyState(27)<>1) do
        begin
            Application.ProcessMessages;
        end;
        showmessage('Esc_Key Up');
    end;
end;如果不加Application.ProcessMessages;
程序进入死循环,加了一直按下ESC键长点时间,执行多次showmessage
按键后再判断键弹起,再执行后面的模块.不要在FormKeyUp中实现

解决方案 »

  1.   

    就是在FormKeyDown中等待被按的键弹起后,才再执行后面的
    if Key=27 then
        begin
            while (GetKeyState(27)<>0) And (GetKeyState(27)<>1) do
            begin
                Application.ProcessMessages;
            end;
            aa;
        end;aa过程中我也要判断ESC的状态,如果esc按时间长点,aa过程中马上就有消息
    while True do
            begin
                if PeekMessage(Msg,0,WM_KEYFIRST,WM_KEYLAST,PM_REMOVE) then
                begin
                    if msg.wParam=27 then
                    begin
                        ...
                        Result:=1; exit;
                    end;
                end;
                Application.ProcessMessages;
                ......
            end;
      

  2.   

    如果esc按时间长点,aa过程中马上就有消息,其实就是在FormKeyDown中的消息,而不是aa过程中实际按ESC键,所以想先判断按键弹起
      

  3.   

    为什么要将问题复杂化?放到OnKeyUp事件里面就可以了。
      

  4.   

    因为我有很多键一按下就要有反应的,所以放在OnKeyDown事件里,再说也想知道为什么不行?
    加Application.ProcessMessages;按下ESC键长点时间,执行多次showmessage还可理解,不加为什么是死循环?