Delphi中如何实现模拟组合按键,如发送Ctrl+F的按键
利用 keybd_event函数可实现,如下面的代码用以实现在一个公共菜单中模拟Ctrl_F按钮以调用DBGridEH的查找对话框功能:
这是在一个ActionList中的某一Action的OnExecute事件代码。
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;