winapi - how to send key's to any application that is currently running from background program (c++) -


my question how send key's application application running in background? let's made shortcut left arrow key alt+s, , want whenever i'm in application , when press alt+s background application response shortcut , send opened program left arrow key. here code made in embarcadero c++ 2010 simulate arrow left when pressing alt+s every 200 milisecond's:

void __fastcall tform1::timer1timer(tobject *sender) {      bool bbothpressed = getasynckeystate(vk_menu)  && getasynckeystate(0x53);     if(bbothpressed){         // showmessage("control!");         keybd_event(vk_left, 0, 0, 0);     } }  

... , work fine application, want know how include code global scope (any application), not application.

i encourage use registerhotkey instead of getasynckeystate. way won't need loop nor timer, making application more reliable , responsive.

to simulate keypresses application/window, need to:

a) focus specific window:

bringwindowtotop(hwnd); setforegroundwindow(hwnd); setfocus(hwnd); 

b) send keydown + keyup events:

keybd_event(keycode, 0, keyeventf_keydown, 0); keybd_event(keycode, 0, keyeventf_keyup, 0); 

this should suffice. however, keep in mind user might pressing 1 of keys you're simulating, result might not you're expecting. avoid that, need check if keys being pressed (use getasynckeystate here), send keyup before doing b.

update:

if wish send keypresses whatever application on foreground (being focused), can skip a.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -