我想在form窗体中按F3键就弹出一个对话框,请问怎样实现?

pgz_007 2008-05-22 10:03:37
我在form的keypress中的代码:
if Key =VK_F3 then
begin
showmessage('11');
end;

可是报错incompatible types。我已经将Form的KeyPreview属性设为True了。

难道要uses什么东西吗?
...全文
146 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
blazingfire 2008-05-22
  • 打赏
  • 举报
回复
在KeyPress中
procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
//Key是Char类型的,VK_F3是整形的,类型不匹配。报错incompatible types!
if Key = VK_F3 then
showmessage('11');
end;
blazingfire 2008-05-22
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 pgz_007 的回复:]
我是楼主。僵哥,我在OnKeyUp事件当中, 加一个一条Shift := []

可是这样我在keypress中的代码:
if Key =VK_F3 then
begin
showmessage('11');
end;

还是报错incompatible types 啊

加上ord() 或者char()不报错了,但是按F3没反应。
[/Quote]

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当中,否则稍稍按的时间长一点就会重复执行.不能理解,针对此因为通过测试没有这样问题!
gzmhero 2008-05-22
  • 打赏
  • 举报
回复
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key =VK_F3 then
begin
showmessage('11');
end;
end;
pgz_007 2008-05-22
  • 打赏
  • 举报
回复
我是楼主。僵哥,我在OnKeyUp事件当中, 加一个一条Shift := []

可是这样我在keypress中的代码:
if Key =VK_F3 then
begin
showmessage('11');
end;

还是报错incompatible types 啊

加上ord() 或者char()不报错了,但是按F3没反应。
僵哥 2008-05-22
  • 打赏
  • 举报
回复
不要改在KeyDown当中,否则稍稍按的时间长一点就会重复执行.
另外最好是增加一个菜单项,其中设置快捷键为F3,这样子就不需要多余写代码了.
blazingfire 2008-05-22
  • 打赏
  • 举报
回复
不行的,要在KeyDown里面写代码的,KeyPress响应不了F3键的

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key = VK_F3 then
showmessage('11');
end;
gzmhero 2008-05-22
  • 打赏
  • 举报
回复
if Key=Char(VK_F3) then
...............
blazingfire 2008-05-22
  • 打赏
  • 举报
回复
if ORD(Key) =VK_F3 then
begin
showmessage('11');
end;
僵哥 2008-05-22
  • 打赏
  • 举报
回复
OnKeyUp事件当中,最好加一个一条Shift = []

16,749

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧