possmessage如何给当前系统最前面的form发送2个组合键盘按下的组合信息,具有独立性最好,我的程序需要模拟键盘信息,是给焦点所在form发crtl+a
和crtl+c,2个组合,最好不影响用户自己的剪贴板操作

解决方案 »

  1.   

    > I want to control a simple application from my Delphi program. I need to be> able to simulate the pressing of keys. Is there a package or unit available> that does this kind of thing?
    you can feed the foreground application using these routines:
    {************************************************************ * 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 ************************************************************}Procedure PostKeyEx32( key: Word; Const shift: TShiftState;                     specialkey: Boolean );  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 SendText( S: String );  Procedure SendRawCharacter( ch : Char );    Var      i: Integer;      numStr: String;    Begin      numStr := Format('%4.4d',[Ord(ch)]);      keybd_event( VK_MENU, MapVirtualKey(VK_MENU, 0),                   0, 0);      for i:= 1 to Length(numStr) do        PostKeyEx32( VK_NUMPAD0 + Ord(numstr[i])-Ord('0'), [], false );      keybd_event( VK_MENU, MapVirtualKey(Paulo Oliveira wrote: > > I have a set of buttons,(caption ='0'..'9') and I'd like to simulate the down position of thebutton, when the user presses the corresponding key. I.e. if user presses key '1' the button goes down on screen. How can Ido this, without a new Tbutton component? 
    No problem Paulo:
    You'll probably want to be using 10 TSpeedButton controls, or an array of them, since this button provides a "Down" property. Anyhow, set the "KeyPreview"property of your main form to "True". Then, in your "OnKeyDown" event handler, write something like this...
         case Key of
            VK_NUMPAD0: btn0.Down := True;
            VK_NUMPAD1: btn1.Down := True;
            VK_NUMPAD2: btn2.Down := True;
            VK_NUMPAD3: btn3.Down := True;
            VK_NUMPAD4: btn4.Down := True;
            VK_NUMPAD5: btn5.Down := True;
            VK_NUMPAD6: btn6.Down := True;
            VK_NUMPAD7: btn7.Down := True;
            VK_NUMPAD8: btn8.Down := True;
            VK_NUMPAD9: btn9.Down := True;
            end;And, in your "OnKeyUp" event handler, write something like...
         case Key of
            VK_NUMPAD0: btn0.Down := False;
            VK_NUMPAD1: btn1.Down := False;
            VK_NUMPAD2: btn2.Down := False;
            VK_NUMPAD3: btn3.Down := False;
            VK_NUMPAD4: btn4.Down := False;
            VK_NUMPAD5: btn5.Down := False;
            VK_NUMPAD6: btn6.Down := False;
            VK_NUMPAD7: btn7.Down := False;
            VK_NUMPAD8: btn8.Down := False;
            VK_NUMPAD9: btn9.Down := False;
            end;You'll want to experiment with the "AllowAllUp" property and the "GroupIndex" property to get the button response/effect you like.
    Again, an array of TSpeedButtons would be the most elegant solution to this problem, since you could use the VK_ constant as the index, and make both eventhandlers a one line call to Button[VK_x].Down := True 这个要求因为是连续的和2个按键同时一共要4个键盘信息所以比较麻烦找到一些例子来看看如何修改符合要求
      

  2.   

    postmessage(GetTopWindow(nil),WM_KEYDOWN,VK_XXX{表示键盘码,楼上说了一些},附加键)其中,附加键的表示方法:
    $20000000;表示在按键的同时有alt
    类似的:
    Ctrl : $10000000;
    Shift: $08000000;
      

  3.   

    crtl+a
    和crtl+c,2个组合同时在一个语句里可以吗?应该怎么写呢?
    postmessage(GetTopWindow(nil),WM_KEYDOWN,VK_XXX{表示键盘码,楼上说了一些},
      

  4.   

    两个组合怎么能写在一起呢,这样分开写:
    postmessage(GetTopWindow(nil),WM_KEYDOWN,$41,$20000000)
    postmessage(GetTopWindow(nil),WM_KEYDOWN,$43,$20000000)