软件注册站
热情软件屋

 
Delphi怎么传递按键给其它程序
编号: QA001198    
建立日期: 1999年6月18日 最后修改日期: 2004年9月4日
所属类别: Delphi - 其他方面
   
    Delphi4.0
    win98
    VB能把ALT+TAB和CTRL+m等键传给其它程序,而DELPHI4.0如何实现?(djs)
   
    可以利用Windows API的keybd_event函数,它可以产生键盘消息。例子:
    procedure TForm1.Button1Click(Sender: TObject);
    begin
     {按下A键}
     Edit1.SetFocus;
     keybd_event(VK_SHIFT, 0, 0, 0);
     keybd_event(ord('A'), 0, 0, 0);
     keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
    
     {按下左Window键然后选择“运行”}
     keybd_event(VK_LWIN, 0, 0, 0);
     keybd_event(ord('R'), 0, 0, 0);
     keybd_event(VK_LWIN, 0, KEYEVENTF_KEYUP, 0);
    end;
    

    下面是一个封装好的函数,用起来更方便一些:
    Procedure PostKeyEx32( key: Word; Const shift: TShiftState; specialkey: Boolean );
    {************************************************************
    * Procedure PostKeyEx32
    *
    * Parameters:
    * key : virtual keycode of the key to send. For printable
    * keys this is simply the ANSI code (Ord(character)).
    * shift : state of the modifier keys. This is a set, so you
    * can set several of these keys (shift, control, alt,
    * mouse buttons) in tandem. The TShiftState type is
    * declared in the Classes Unit.
    * specialkey: normally this should be False. Set it to True to
    * specify a key on the numeric keypad, for example.
    * Description:
    * Uses keybd_event to manufacture a series of key events matching
    * the passed parameters. The events go to the control with focus.
    * Note that for characters key is always the upper-case version of
    * the character. Sending without any modifier keys will result in
    * a lower-case character, sending it with [ssShift] will result
    * in an upper-case character!
    *Created: 17.7.98 by P. Below
    ************************************************************}
    
    Type
     TShiftKeyInfo = Record
     shift: Byte;
     vkey : Byte;
     End;
     byteset = Set of 0..7;
    Const
     shiftkeys: Array [1..3] of TShiftKeyInfo =
     ((shift: Ord(ssCtrl); vkey: VK_CONTROL ),
     (shift: Ord(ssShift); vkey: VK_SHIFT ),
     (shift: Ord(ssAlt); vkey: VK_MENU ));
    Var
     flag: DWORD;
     bShift: ByteSet absolute shift;
     i: Integer;
    Begin
     For i := 1 To 3 Do Begin
     If shiftkeys[i].shift In bShift Then
     keybd_event( shiftkeys[i].vkey, MapVirtualKey(shiftkeys[i].vkey, 0), 0, 0);
     End; { For }
     If specialkey Then
     flag := KEYEVENTF_EXTENDEDKEY
     Else
     flag := 0;
    
     keybd_event( key, MapvirtualKey( key, 0 ), flag, 0 );
     flag := flag or KEYEVENTF_KEYUP;
     keybd_event( key, MapvirtualKey( key, 0 ), flag, 0 );
    
     For i := 3 DownTo 1 Do Begin
     If shiftkeys[i].shift In bShift Then
     keybd_event( shiftkeys[i].vkey, MapVirtualKey(shiftkeys[i].vkey, 0), KEYEVENTF_KEYUP, 0);
     End; { For }
    End; { PostKeyEx32 }
    
    例子:
    procedure TForm1.Button1Click(Sender: TObject);
    begin
     // 按下左Window键
     PostKeyEx32(VK_LWIN, [], FALSE);
    
     // 按下D键
     PostKeyEx32(ORD('D'), [], FALSE);
    
     // 按下Ctrl-Alt-C
     PostKeyEx32(ORD('C'), [ssctrl, ssAlt], FALSE);
    end;
    
    

    
    相关问题:
    QA004895 "用keybd_event如何模拟[{, ]}, -_, =+等"

    

此问题由李海回答。

附加关键字:编程, 源程序, programming, source code, Delphi, VCL, Borland, 其他方面,

   
 
把这个问题推荐给朋友
   
 
   
您的意见类别
您的名字
您的电子邮件
您的建议(请尽可能详细)
 
 

版权所有 1997-2008 热情软件屋
如果您有任何建议和意见, 请给我发个电子邮件 askpro@china-askpro.com
Web Designed by ZebraStudio