怎样截获并模拟键盘输入(即键盘宏定义,如FPE)?(100分)

  • 主题发起人 主题发起人 typhoon
  • 开始时间 开始时间
截获:
做键盘钩子(Hook)

模拟:(举例)
//回车
keybd_event(13, MapVirtualKey(13, 0), 0, 0);
keybd_event(13, MapVirtualKey(13, 0), KEYEVENTF_KEYUP, 0);
//tab
keybd_event(9, MapVirtualKey(9, 0), 0, 0);
keybd_event(9, MapVirtualKey(9, 0), KEYEVENTF_KEYUP, 0);
//del
keybd_event(VK_Delete, MapVirtualKey(VK_Delete, 0), 0, 0);
keybd_event(VK_Delete, MapVirtualKey(VK_Delete, 0),
KEYEVENTF_KEYUP, 0);
 
在KeyPress或KeyDown事件中截获键盘消息,然后发送一个键盘消息,用
Sendmessage就可以。
 
按键模拟1:
keybd_event(VK_MENU,0,0,0);//ALT键
keybd_event(VK_MENU,0,KEYEVENTF_KEYUP,0)
按键模拟2:
procedure TForm1.DBGrid1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key = VK_RETURN then
begin
PostMessage(DBGrid1.Handle, WM_KEYDOWN, VK_TAB, 0);
end;
end;


提醒注意:
Delphi5中已经有所变化,在OnShortCut事件中直接进行处理,最好跟踪运行一遍.
procedure TForm1.FormShortCut(var Msg: TWMKey; var Handled: Boolean);
begin
if Msg.CharCode=13 then
begin
Msg.CharCode:=9;
Handled:=False;//消息没有处理.
end;

需要用回车键代替TAB键下移一个控件时,把KeyPress设为True,加入下列代码拦截击键:
Procedure TForm1.FormKeyPress(Sender:Tobject;Var Key:Char);
Begin
 if key=#13 then { 判断是按执行键}
 if not (ActiveControl is TDbgrid) Then
 Begin { 不是在TDbgrid控件内}
  key:=#0;
  perform(WM_NEXTDLGCTL,0,0);{移动到下一个控件}
 end else
 if (ActiveControl is TDbgrid) Then{是在 TDbgrid 控件内}
 begin
  With TDbgrid(ActiveControl) Do
  if Selectedindex<(FieldCount-1) then
  Selectedindex:=Selectedindex+1{ 移动到下一字段}
  else Selectedindex:=0;
 end;
End;
 
后退
顶部