我在form的keypress中的代码:
if Key =VK_F3 then
   begin
     showmessage('11');
   end;可是报错incompatible types。我已经将Form的KeyPreview属性设为True了。难道要uses什么东西吗?

解决方案 »

  1.   

    OnKeyUp事件当中,最好加一个一条Shift = []
      

  2.   

    if ORD(Key) =VK_F3 then 
       begin 
         showmessage('11'); 
       end; 
      

  3.   

        if Key=Char(VK_F3) then
               ...............
      

  4.   

    不行的,要在KeyDown里面写代码的,KeyPress响应不了F3键的procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    begin
      if Key = VK_F3 then
        showmessage('11');
    end;
      

  5.   

    不要改在KeyDown当中,否则稍稍按的时间长一点就会重复执行.
    另外最好是增加一个菜单项,其中设置快捷键为F3,这样子就不需要多余写代码了.
      

  6.   

    我是楼主。僵哥,我在OnKeyUp事件当中, 加一个一条Shift := []可是这样我在keypress中的代码: 
    if Key =VK_F3 then 
       begin 
         showmessage('11'); 
       end; 还是报错incompatible types 啊加上ord() 或者char()不报错了,但是按F3没反应。
      

  7.   

    procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    begin
        if Key =VK_F3 then
        begin
            showmessage('11');
        end;
    end;
      

  8.   


    1)加一个一条Shift := [],肯定是解决不了报错的问题的!报错是语法的问题
    2)没有反应是事件用得不对,KeyPress只能捕获按字符键事件,对于功能键你要在OnKeyDown里来写代码,这是Delphi的帮助:
     a)The OnKeyPress event is the simplest of the three events, in that it returns only a single character the user presses
     b)Use the OnKeyDown and OnKeyUp events when you want to interpret key combinations such as whether the SHIFT, CTRL, or ALT key is pressed at the time the active control receives the key event; and to handle keys that have no ASCII equivalent, such as function keys. The F1 key, for example, does not get captured by the OnKeyPress event because it has no alphanumeric value.
    3)僵哥不要改在KeyDown当中,否则稍稍按的时间长一点就会重复执行.不能理解,针对此因为通过测试没有这样问题!
      

  9.   

    在KeyPress中
    procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
    begin
      //Key是Char类型的,VK_F3是整形的,类型不匹配。报错incompatible types!
      if Key = VK_F3 then
        showmessage('11');
    end;