How to send virtual keys to other application using delphi 2010? -


i need send several virtual keys (vk_return) delphi application (myapp.exe) application (target.exe). eg : send vk_return twice , myapp.exe , target.exe

the os use windows 7 64 bit , windows xp.

i read : how send "enter" key press application? , send ctrl+key 3rd party application (did not work me) , other previous asked question. still i'm getting confused.

how set focus target application ?

how send virtual keys targeted application ?

simple example : want send vk_return twice notepad.exe or calc.exe (already loaded) or other program delphi application. how ?

the simplest way in delphi 2010, please...

ps : tried sndkey32.pass http://delphi.about.com/od/adptips2004/a/bltip1104_3.htm , got error : [dcc error] sndkey32.pas(420): e2010 incompatible types: 'char' , 'ansichar'

  if (length(keystring)=1) mkey:=vkkeyscan(keystring[1]) 

if target application isn't foreground window, need use postmessage send keystrokes window handle. can window handle using findwindow. code below sends enter key text area in running instance of notepad (note uses additional findwindowex locate memo area first). tested using both delphi 2007 , delphi xe4 (32-bit target) on windows 7 64.

procedure tform1.button1click(sender: tobject); var   npwnd, npedit: hwnd; begin   npwnd := findwindow('notepad', nil);   if npwnd <> 0   begin     npedit := findwindowex(npwnd, 0, 'edit', nil);     if npedit <> 0     begin       postmessage(npedit, wm_keydown, vk_return, 0);       postmessage(npedit, wm_keyup, vk_return, 0);      end;   end; end; 

to find window title (caption) instead, can use second parameter findwindow. finds new instance of notepad default 'untitled' file open:

npwnd := findwindow(nil, 'untitled - notepad'); 

note requires exact match on window title. space before or after -, instance, cause match fail , window handle not retrieved.

you can use both window class , title if have multiple instances running. find copy of notepad running readme.txt loaded, use

npwnd := findwindow('notepad', 'readme.txt - notepad'); 

to find other applications, you'll need use winspy or winsight find window class names. (there others also, such winspector or windowse (both of written in delphi).)

your comment mentions calculator; according winspector, calculator main window in window class called calcframe on windows 7, , area numbers displayed in static window (meaning doesn't seem receive keystrokes directly). buttons called button, you'd have loop through them using enumchildwindows looking individual buttons identify them in order obtain handles.

(how enumerate child windows separate question; can find example searching here or via google. if can't, post new, separate question , can try answer.)

here's quick example of sending keys calculator after finding window class. doesn't useful, because needs time spent identify different buttons , keys each responds (and proper combination of messages). code sends 11numpad+22 calculator window (a quick test showed received , displayed, , that's time wanted spend on process).

procedure tform1.button1click(sender: tobject); var   npwnd: hwnd; begin   npwnd := findwindow('calcframe',  nil);   if npwnd <> 0   begin     postmessage(npwnd, wm_keydown, vk_numpad1, 0);     postmessage(npwnd, wm_keydown, vk_add, 0);     postmessage(npwnd, wm_keydown, vk_numpad2, 0);   end; end; 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -