C# - trigger key down event for active control -


i found command system.windows.forms.sendkeys.send() sending keypress key. function work if open external app notepad , set focus , see key printed in text field. how same key down event, system.windows.forms.sendkeys.senddown("a");, example?

i tried call in timer command system.windows.forms.sendkeys.send() have runtime error associated fast taped.

you can't use sendkeys class that, unfortunately. need go lower level api.

poking window keydown message

in windows, keyboard events sent windows , controls via windows message pump. piece of code using postmessage should trick:

[dllimport("user32.dll")] static extern bool postmessage(intptr hwnd, uint msg, int wparam, int lparam);  const uint wm_keydown = 0x0100;  void sendkeydowntoprocess(string processname, system.windows.forms.keys key) {     process p = process.getprocessesbyname(processname).firstordefault();     if (p != null)     {         postmessage(p.mainwindowhandle, wm_keydown, (int)key, 0);     } } 

note application receiving these events may not until corresponding wm_keyup received. can other message constants here.

poking control other main window

the above code send keydown "mainwindowhandle." if need send else (e.g. active control) need call postmessage handle other p.mainwindowhandle. question is... how handle?

this involved... need temporarily attach thread window's message input , poke figure out handle is. can work if current thread exists in windows forms application , has active message loop.

an explanation can found here, example:

using system.runtime.interopservices;  public partial class formmain : form {     [dllimport("user32.dll")]     static extern intptr getforegroundwindow();      [dllimport("user32.dll")]     static extern intptr getwindowthreadprocessid(intptr hwnd, intptr processid);      [dllimport("user32.dll")]     static extern intptr attachthreadinput(intptr idattach,                           intptr idattachto, bool fattach);      [dllimport("user32.dll")]     static extern intptr getfocus();      public formmain()     {         initializecomponent();     }      private void timerupdate_tick(object sender, eventargs e)     {         labelhandle.text = "hwnd: " +                             focusedcontrolinactivewindow().tostring();     }      private intptr focusedcontrolinactivewindow()     {         intptr activewindowhandle = getforegroundwindow();          intptr activewindowthread =            getwindowthreadprocessid(activewindowhandle, intptr.zero);         intptr thiswindowthread = getwindowthreadprocessid(this.handle, intptr.zero);          attachthreadinput(activewindowthread, thiswindowthread, true);         intptr focusedcontrolhandle = getfocus();         attachthreadinput(activewindowthread, thiswindowthread, false);          return focusedcontrolhandle;     } } 

the news-- if sendkeys worked you, might not need this-- sendkeys sends messages main window handle.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -