Senden Tastendruck auf die Anwendung in c# (sendkeys, postmessage, sendmessage alle nicht arbeiten)

Ich versuche zu tun, eine der folgenden
1. öffnen Sie das gewünschte Programm und drücken Sie eine Taste, programmgesteuert
2. finden Sie Fenster öffnen von Programm und drücken Sie eine Taste, programmgesteuert
(entweder ist in Ordnung)

Ich habe versucht zahlreiche Implementierungen von SendKeys.SendWait(), PostMessage () und SendMessage() erfolglos. Hier sind meine code-snippets

    //included all these for attempts 
    [DllImport("User32.dll")] 
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);  

    [DllImport("User32.dll")] 
    static extern int SetForegroundWindow(IntPtr hWnd);

    [DllImport("User32.Dll", EntryPoint = "PostMessageA")]
    static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, int lParam);

    [DllImport("user32.dll")]
    static extern byte VkKeyScan(char ch);

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

Get Griff von Fenster, Variablen, die durch sendmessage/postmessage/sendkeys

IntPtr ptrOBS = proc.Handle;//this works properly, proc is instantiated properly
//IntPtr ptrOBS = FindWindow(null, "Open Broadcaster Software v0.472b");
SetForegroundWindow(ptrOBS);
const UInt32 WM_CHAR = 0x0102;
const uint WM_KEYDOWN = 0x100;
const int VK_R = 0x52; //taken from http://msdn.microsoft.com/en-us/library/dd375731(v=vs.85).aspx
const int VK_S = 0x53;

SendMessage Versuch:

SendMessage(ptrOBS, WM_KEYDOWN, (IntPtr)VK_R, (IntPtr)1);//tried both WM_CHAR and WM_KEYDOWN

PostMessage versuchen:

string message = "rs";
bool sent = PostMessage(ptrOBS, WM_KEYDOWN, VkKeyScan(message[0]), 0);

SendKeys Versuch:

SendKeys.SendWait("{r}");

Versucht SetFocus auf das übergeordnete Fenster (Anwendung) und untergeordnete Fenster (Schaltfläche ausgelöst durch Tastendruck im, der versucht zu senden):

static void SetFocus(IntPtr hwndTarget, string childClassName)
    {
        //hwndTarget is the other app's main window 
        //...
        IntPtr targetThreadID = GetWindowThreadProcessId(hwndTarget, IntPtr.Zero); //target thread id
        IntPtr myThreadID = GetCurrentThread(); //calling thread id, our thread id
        try
        {
            bool lRet = AttachThreadInput(myThreadID, targetThreadID, -1); //attach current thread id to target window

            //if it's not already in the foreground...
            lRet = BringWindowToTop(hwndTarget);
            SetForegroundWindow(hwndTarget);

            //if you know the child win class name do something like this (enumerate windows using Win API again)...
            IntPtr hwndChild = (IntPtr)1183492;//(IntPtr)EnumAllWindows(hwndTarget, childClassName).FirstOrDefault();

            if (hwndChild == IntPtr.Zero)
            {
                //or use keyboard etc. to focus, i.e. send keys/input...
                //SendInput (...);
                return;
            }

            //you can use also the edit control's hwnd or some child window (of target) here
            SetFocus(hwndChild); //hwndTarget);
            SendKeys.SendWait("{r}");
        }
        finally
        {
            SendKeys.SendWait("{r}");
            bool lRet = AttachThreadInput(myThreadID, targetThreadID, 0); //detach from foreground window
            SendKeys.SendWait("{r}");
        }
    }

Für NSGaga:

    string windowName = "Open Broadcaster Software v0.472b";
IntPtr outerPtr = FindWindow(null, windowName);
IntPtr ptrOBS = (IntPtr)527814;//button that im trying to trigger keypress on
SetForegroundWindow(outerPtr);
SetForegroundWindow(ptrOBS);
SetFocus(outerPtr, "OBSWindowClass");//SetFocus(ptrOBS, "Button");
const UInt32 WM_CHAR = 0x0102;
const int VK_R = 0x52; //taken from http://msdn.microsoft.com/en-us/library/dd375731(v=vs.85).aspx
const int VK_S = 0x53;

//SetForegroundWindow(ptrOBS);
System.Threading.Thread.Sleep(3000);
SendKeys.SendWait("{r}");
SendMessage(outerPtr, WM_KEYDOWN, (IntPtr)VK_R, (IntPtr)1);
PostMessage(outerPtr, WM_KEYDOWN, VkKeyScan('r'), 0);
Ist der soll-Prozess erhöht?
heh, ich hätte gestanzt die Wand, wenn das der Fall war, nicht einen Unterschied machen.
Siehe: stackoverflow.com/questions/4944621/...

InformationsquelleAutor SPillai | 2013-03-20

Schreibe einen Kommentar