‎07-08-2009 04:47 AM
Hi All,
I am trying to pass the simulated keyboard events to notepad but when notepad is not my active window?
If notepad window is active I am able to pass the simulated keyboard events to application
I have searched the hole website but not got the solution when application is inactive
Can you please suggest the way to resolve this issue.
‎07-08-2009 05:34 AM
Hi kpraveen,
i searched the internet and found something, but it's in Delphi.
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..1] of 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;
From this, it should be possible with a postmessage function and some other API calls.
Mike
‎07-08-2009 09:35 AM
Thanks Mike,
I had a look, this code uses postmessge which takes the keyboard events into the buffer and executes it. So possibility of loosing the keyboard events is there. So "Sendmessage" function is the one to be used where we wont be loosing any events generated by the keyboard.
I have already tried in a different way but using the "postmessage" itself but it works only when the window is an active window even if the window handle is given. It does not write any message or character if the window is not my active window.
Regards,
Praveen.
‎07-08-2009 09:44 AM
Hi Praveen,
i'll check the provided code later in the evening.
Hope it helps
Mike
‎07-08-2009 01:18 PM
Hi Praveen,
sorry, unfortunately it also doesn't work for me. Do you have to send keyboard events to it or do you only want to write text into the editor?
Do you need it for the editor or was it only an example program? Which program do you like to operate by remote control?
Mike
‎07-08-2009 10:49 PM
Here are some of the keyboard examples which i got it through forums. But all these codes work only when window is active except "Executable". I t is specific to Calucator.
Notpad is just an example. I want to control another application.
‎07-09-2009 12:10 AM
I want to control an application which does not have source code, only exe is there. So i want to control it BY SIMULATING keyboard events. This exe requires some test messages, tab control ,etc in order to control.
‎07-09-2009 12:37 AM
Hi Praveen,
why isn't it an option to bring this application to front?
Mike
‎07-09-2009 04:58 AM