delphi如何实现模拟组合按键,如发送Ctrl_F的按键消息  
官方Delphi 学习QQ群: 682628230(三千人)\n
频道

delphi如何实现模拟组合按键,如发送Ctrl_F的按键消息


利用 keybd_event函数可实现,如下面的代码用以实现在一个公共菜单中模拟Ctrl_F按钮以调用DBGridEH的查找对话框功能:
这是在一个ActionList中的某一Action的OnExecute事件代码。
view plaincopy to clipboardprint?
procedure TDM.act_LocateExecute(Sender: TObject);
var
myPopupMenu:TPopupMenu;
MyDBGridEH:TDBGridEh;
begin
//ShowMessage(TMenuItem(TAction(Sender).ActionComponent).GetParentComponent.Name);
if (TAction(Sender).ActionComponent).GetParentComponent is TPopupMenu then
myPopupMenu := (TAction(Sender).ActionComponent).GetParentComponent as TPopupMenu
else
Exit;

//ShowMessage(MyPopupMenu.PopupComponent.Name);
if (MyPopupMenu.PopupComponent is TDBGridEh) then
begin
MyDBGridEH := TDBGridEh(MyPopupMenu.PopupComponent);
MyDBGridEH.SetFocus;
keybd_event(VK_Control, MapVirtualKey(VK_Control, 0), 0, 0); //按下Ctrl键
keybd_event(ord('F'), MapVirtualKey(ord('F'), 0), 0, 0); //按下F键
keybd_event(ord('F'), MapVirtualKey(ord('F'), 0), KEYEVENTF_KEYUP, 0); //放开F键
keybd_event(VK_Control, MapVirtualKey(VK_Control, 0), KEYEVENTF_KEYUP, 0); //放开Ctrl键
end;
end;

推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.040337085723877 seconds