Autor Beitrag
k-weddige
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 49

Win Vista Business
C# (VS 2008 Professional)
BeitragVerfasst: So 14.09.03 18:11 
Ich möchte Tasten an ein anderes Programm senden. Wie kann ich das am einfachsten machen?

k-weddige

_________________
Meine Programme sind perfekt, der Computer versteht sie bloß nicht.
MSCH
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1448
Erhaltene Danke: 3

W7 64
XE2, SQL, DevExpress, DevArt, Oracle, SQLServer
BeitragVerfasst: So 14.09.03 20:04 
mit Postmessage(Fensterhandle, wm_keyDown,<virtual_key_code>,<lKeyData>);
schau in die WIn32 Hilfe, da sind die Parameter beschrieben.
grez
msch

_________________
ist das politisch, wenn ich linksdrehenden Joghurt haben möchte?
k-weddige Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 49

Win Vista Business
C# (VS 2008 Professional)
BeitragVerfasst: Di 16.09.03 18:46 
Giebt es nicht einen Befehl, bei dem ich einen ganzen Text an ein Programm senden kann?

k-weddige

_________________
Meine Programme sind perfekt, der Computer versteht sie bloß nicht.
MSCH
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1448
Erhaltene Danke: 3

W7 64
XE2, SQL, DevExpress, DevArt, Oracle, SQLServer
BeitragVerfasst: Di 16.09.03 18:55 
imho nein.
grez msch

_________________
ist das politisch, wenn ich linksdrehenden Joghurt haben möchte?
CreepA.S.A.P
Hält's aus hier
Beiträge: 13

Win98 - Winxp
D7 Enterprise Ger
BeitragVerfasst: Di 16.09.03 23:58 
DOch das geht ! Wie weiss ich auch nich genau aber vieleicht hilft dir das hier: (nich von mir)
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
{************************************************************}
{1. PostKeyEx32 function} 

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! 
// Code by P. Below 
************************************************************}
 
type 
  TShiftKeyInfo = record 
    shift: Byte; 
    vkey: Byte; 
  end
  byteset = set of 0..7
const 
  shiftkeys: array [1..3of 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), 00); 
  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 } 


// Example: 

procedure TForm1.Button1Click(Sender: TObject); 
begin 
  //Pressing the Left Windows Key 
  PostKeyEx32(VK_LWIN, [], False); 

  //Pressing the letter D 
  PostKeyEx32(Ord('D'), [], False); 

  //Pressing Ctrl-Alt-C 
  PostKeyEx32(Ord('C'), [ssctrl, ssAlt], False); 
end


{************************************************************} 
{2. With keybd_event API} 

procedure TForm1.Button1Click(Sender: TObject); 
begin 
  {or you can also try this simple example to send any 
   amount of keystrokes at the same time. }
 

  {Pressing the A Key and showing it in the Edit1.Text} 

  Edit1.SetFocus; 
  keybd_event(VK_SHIFT, 000); 
  keybd_event(Ord('A'), 000); 
  keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0); 

  {Presses the Left Window Key and starts the Run} 
  keybd_event(VK_LWIN, 000); 
  keybd_event(Ord('R'), 000); 
  keybd_event(VK_LWIN, 0, KEYEVENTF_KEYUP, 0); 
end


{***********************************************************} 
{3. With keybd_event API} 

procedure PostKeyExHWND(hWindow: HWnd; key: Word; const shift: TShiftState; 
  specialkey: Boolean); 
{************************************************************ 
 * Procedure PostKeyEx 
 * 
 * Parameters: 
 *  hWindow: target window to be send the keystroke 
 *  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. 
 *           If this parameter is true, bit 24 of the lparam for 
 *           the posted WM_KEY* messages will be set. 
 * Description: 
 *  This procedure sets up Windows key state array to correctly 
 *  reflect the requested pattern of modifier keys and then posts 
 *  a WM_KEYDOWN/WM_KEYUP message pair to the target window. Then 
 *  Application.ProcessMessages is called to process the messages 
 *  before the keyboard state is restored. 
 * Error Conditions: 
 *  May fail due to lack of memory for the two key state buffers. 
 *  Will raise an exception in this case. 
 * NOTE: 
 *  Setting the keyboard state will not work across applications 
 *  running in different memory spaces on Win32 unless AttachThreadInput 
 *  is used to connect to the target thread first. 
 *Created: 02/21/96 16:39:00 by P. Below 
 ************************************************************}
 
type 
  TBuffers = array [0..1of TKeyboardState; 
var 
  pKeyBuffers: ^TBuffers; 
  lParam: LongInt; 
begin 
  (* check if the target window exists *) 
  if IsWindow(hWindow) then 
  begin 
    (* set local variables to default values *) 
    pKeyBuffers := nil
    lParam := MakeLong(0, MapVirtualKey(key, 0)); 

    (* modify lparam if special key requested *) 
    if specialkey then 
      lParam := lParam or $1000000

    (* allocate space for the key state buffers *) 
    New(pKeyBuffers); 
    try 
      (* Fill buffer 1 with current state so we can later restore it. 
         Null out buffer 0 to get a "no key pressed" state. *)
 
      GetKeyboardState(pKeyBuffers^[1]); 
      FillChar(pKeyBuffers^[0], SizeOf(TKeyboardState), 0); 

      (* set the requested modifier keys to "down" state in the buffer*) 
      if ssShift in shift then 
        pKeyBuffers^[0][VK_SHIFT] := $80
      if ssAlt in shift then 
      begin 
        (* Alt needs special treatment since a bit in lparam needs also be set *) 
        pKeyBuffers^[0][VK_MENU] := $80
        lParam := lParam or $20000000
      end
      if ssCtrl in shift then 
        pKeyBuffers^[0][VK_CONTROL] := $80
      if ssLeft in shift then 
        pKeyBuffers^[0][VK_LBUTTON] := $80
      if ssRight in shift then 
        pKeyBuffers^[0][VK_RBUTTON] := $80
      if ssMiddle in shift then 
        pKeyBuffers^[0][VK_MBUTTON] := $80

      (* make out new key state array the active key state map *) 
      SetKeyboardState(pKeyBuffers^[0]); 
      (* post the key messages *) 
      if ssAlt in Shift then 
      begin 
        PostMessage(hWindow, WM_SYSKEYDOWN, key, lParam); 
        PostMessage(hWindow, WM_SYSKEYUP, key, lParam or $C0000000); 
      end 
      else 
      begin 
        PostMessage(hWindow, WM_KEYDOWN, key, lParam); 
        PostMessage(hWindow, WM_KEYUP, key, lParam or $C0000000); 
      end
      (* process the messages *) 
      Application.ProcessMessages; 

      (* restore the old key state map *) 
      SetKeyboardState(pKeyBuffers^[1]); 
    finally 
      (* free the memory for the key state buffers *) 
      if pKeyBuffers <> nil then 
        Dispose(pKeyBuffers); 
    end{ If } 
  end
end{ PostKeyEx } 

// Example: 

procedure TForm1.Button1Click(Sender: TObject); 
var 
  targetWnd: HWND; 
begin 
  targetWnd := FindWindow('notepad'nil
    if targetWnd <> 0 then 
    begin 
      PostKeyExHWND(targetWnd, Ord('I'), [ssAlt], False); 
  end
end

{***********************************************************} 
{3. With SendInput API} 

// Example: Send text 
procedure TForm1.Button1Click(Sender: TObject); 
const 
   Str: string = 'writing writing writing'
var 
  Inp: TInput; 
  I: Integer; 
begin 
  Edit1.SetFocus; 

  for I := 1 to Length(Str) do 
  begin 
    // press 
    Inp.Itype := INPUT_KEYBOARD; 
    Inp.ki.wVk := Ord(UpCase(Str[i])); 
    Inp.ki.dwFlags := 0
    SendInput(1, Inp, SizeOf(Inp)); 

    // release 
    Inp.Itype := INPUT_KEYBOARD; 
    Inp.ki.wVk := Ord(UpCase(Str[i])); 
    Inp.ki.dwFlags := KEYEVENTF_KEYUP; 
    SendInput(1, Inp, SizeOf(Inp)); 

    Application.ProcessMessages; 
    Sleep(80); 
  end
end

// Example: Simulate Alt+Tab 
procedure SendAltTab; 
var 
  KeyInputs: array of TInput; 
  KeyInputCount: Integer; 

  procedure KeybdInput(VKey: Byte; Flags: DWORD); 
  begin 
    Inc(KeyInputCount); 
    SetLength(KeyInputs, KeyInputCount); 
    KeyInputs[KeyInputCount - 1].Itype := INPUT_KEYBOARD; 
    with  KeyInputs[KeyInputCount - 1].ki do 
    begin 
      wVk := VKey; 
      wScan := MapVirtualKey(wVk, 0); 
      dwFlags := KEYEVENTF_EXTENDEDKEY; 
      dwFlags := Flags or dwFlags; 
      time := 0
      dwExtraInfo := 0
    end
  end
begin 
  KeybdInput(VK_MENU, 0);                // Alt 
  KeybdInput(VK_TAB, 0);                 // Tab 
  KeybdInput(VK_TAB, KEYEVENTF_KEYUP);   // Tab 
  KeybdInput(VK_MENU, KEYEVENTF_KEYUP); // Alt 
  SendInput(KeyInputCount, KeyInputs[0], SizeOf(KeyInputs[0])); 
end;

Musst dir mal das vorletzte Beispiel genauer angucken.

Moderiert von user profile iconTino: Code- durch Delphi-Tags ersetzt.
MSCH
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1448
Erhaltene Danke: 3

W7 64
XE2, SQL, DevExpress, DevArt, Oracle, SQLServer
BeitragVerfasst: Mi 17.09.03 18:23 
auch das progg sendet keine Zeichenkette sondern einzelne Zeichen.
das geht . aber auch nur dass.
grez
msch

_________________
ist das politisch, wenn ich linksdrehenden Joghurt haben möchte?
CreepA.S.A.P
Hält's aus hier
Beiträge: 13

Win98 - Winxp
D7 Enterprise Ger
BeitragVerfasst: Mi 17.09.03 20:31 
Doch! Habs ausprobiert!
Ich hab das mit dem verletzten beispiel ausprobiert und es ging.
Ich hab das ganze auf nen Button gelegt und dann hat das prog
das "writing writing writing" in ein edit geschrieben!
Dann muss man "nur" noch das fenster angeben wo man das hinhaben will. Wie das genau geht probier ich gerade heraus zu finden :P
Adrian
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 314



BeitragVerfasst: Do 18.09.03 08:17 
Servus!

Um das Ziel anzugeben, kannst Du das benutzen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
implementation 
{$R *.dfm} 
procedure TForm1.Button1Click(Sender: TObject);  
//...
var
  hOE: hWnd;
//...
begin 
   hOE:=FindWindow(nil,'virtualdub 1.4.13 - capture mode');//Finde das Fenster mit VirtualDub 
   SetForegroundWindow(hOE);// Bringe VirtualDub in den Vordergrund

Natürlich mußt Du den Fenstertitel Deines entsprechenden Programms bei FindWindow eingeben.

Gruß,
Adrian

Moderiert von user profile iconTino: Code- durch Delphi-Tags ersetzt.
Tana´Ri
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 230



BeitragVerfasst: Do 18.09.03 09:11 
wie wärs mit WM_SETTEXT (für text)
für zeichen ist glaub ich WM_CHAR geeignet.

ausblenden Delphi-Quelltext
1:
2:
SendMessage(hTarget,WM_SETTEXT,0,Integer(@String));
PostMessage(hTarget,WM_CHAR,Ord('C'),0);

_________________
mfg
Tana´Ri